Moving a Spaceship to its Destination

  • Context: Undergrad 
  • Thread starter Thread starter Canning
  • Start date Start date
  • Tags Tags
    Spaceship
Click For Summary

Discussion Overview

The discussion revolves around the programming and physics involved in moving a spaceship within a 2D computer game. Participants explore concepts related to velocity, acceleration, and the mechanics of changing direction while in motion, as well as the implementation of these ideas in Visual Basic (VB) code.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes the initial setup of variables for a spaceship's movement, including acceleration and rotation rates.
  • Another participant suggests adjusting the direction vector if the ship is already moving, proposing to turn the ship to point in the opposite direction of motion and apply thrust accordingly.
  • A participant shares code snippets demonstrating how to calculate resultant velocity and acceleration components based on thrust and heading.
  • There is a discussion about converting variables into velocity and acceleration vectors, with one participant questioning the correctness of their calculations for X and Y components.
  • Another participant mentions implementing a method where the ship stops, rotates to a new destination, and then moves towards it, referencing a game for comparison.
  • Some participants emphasize the importance of smooth transitions in motion rather than abrupt changes in direction when adjusting velocities.

Areas of Agreement / Disagreement

Participants express various approaches to handling the movement of the spaceship, with some agreeing on the need for vector calculations while others propose different methods. The discussion remains unresolved regarding the best implementation strategy and the specifics of the calculations.

Contextual Notes

Participants note the potential for abrupt changes in direction if velocities are set directly, suggesting that smoother transitions should be prioritized. There are also references to specific programming practices in VB and VB.NET, indicating a focus on practical coding solutions.

Who May Find This Useful

Game developers, programmers interested in physics simulations, and those working with 2D graphics in Visual Basic may find this discussion relevant.

Canning
Messages
7
Reaction score
0
I am writing a 2d computer game and need some help.

I have a spaceship that is moving(or stationary) and I want to move it to a destination coordinate.

Here are the main variables:
Code:
Const PI = 3.14159          'Mmmm.. Pi
Const ACCEL = 0.1           'Rate of increase of speed
Const ROTATION_RATE = 15    'Rotation speed
Const SHIP_RADIUS = 10      'Distance from center of triangle to any vertex
Const MS_DELAY = 25         'Milliseconds per frame (25 = 40 frames per second)

Dim msngFacing As Single    'Angle the ship is facing (ok, ok, it's a triangle, not a ship! Shut it!)
Dim msngHeading As Single   'Current direction in which ship is moving
Dim msngSpeed As Single     'Current speed with which ship is moving
Dim msngX As Single         'Current X coordinate of ship within form
Dim msngY As Single         'Current Y coordinate of ship within form
 
Physics news on Phys.org
If my ship is not moving, it is pretty easy, just rotate to the target and apply thrust. My problem is if the ship is already moving.

Can I please have some help with the physics ideas behind this and/or some advice on formulas?

thanks
 
This looks like it might belong in the Computer and Technology forum since it's programming. Perhaps an admin will move it.
 
If the ship is already moving then why not just adjust the direction vector?
 
Canning said:
If my ship is not moving, it is pretty easy, just rotate to the target and apply thrust. My problem is if the ship is already moving.

Can I please have some help with the physics ideas behind this and/or some advice on formulas?

thanks

If the ship is already moving nothing changes. Just turn the ship so it points in the opposite direction of the motion and apply thrust. Just add the thrust to the current velocity vector. Also you should use x and y coordinates for velocity and acceleration vectors. Only use polar coordinates for the direction the ship is pointing.

So what you end up with is this:

dx is the x velocity
dy is the y velocity
ddx is the x acceleration
ddy is the y acceleration

x = x + dx;
y = y + dy;
dx = dx + ddx;
dy = dy + ddy;

All you have to do now is calculate your accelerations based on the direction your ship is pointing and the thrust and adjust ddx and ddy accordingly. If there's no thrust just set ddx and ddy to 0. All you need to do is change ddx and ddy and the motions and velocities will work out automatically.
 
OK, I will play around with my vb code to see if I can implement it.

Dr Morbius, do you have any experience with vb?
 
Are you using VB or VB .NET?
 
I am testing it in VB6 at the moment, but am going to convert it to VB.NET when I have some time.

The link to the vb6 file is: http://canning.co.nz/shpshoot.zip

It is a simple physics demo, about moving a 2d ship with some physics.
 
First things first, I will convert my variables into velocity and acceleration vectors. I will get that fixed, then work on the moving to coordinate problem.

Am I corect in saying that the Xvelocity = speed * Sin(Heading) and Yvelocity = speed * Cos(Heading)?

and the Xacceleration = ACCEL * Sin(Facing angle) and Yacceleration = ACCEL * Cos(Facing angle)?
 
Last edited:
  • #10
This is actually already done in the Physics subroutine. Here is the code:

Code:
Private Sub Physics()

Dim sngXComp As Single  'Resultant X and Y components
Dim sngYComp As Single
Dim i As Integer
        
    'Thrust
    If mblnUpKey Then
        mblnUpKey = False
        'Determine the X and Y components of the resultant vector
        sngXComp = Ship.msngSpeed * Sin(Ship.msngHeading) + Ship.ACCEL * Sin(Ship.msngFacing)
        sngYComp = Ship.msngSpeed * Cos(Ship.msngHeading) + Ship.ACCEL * Cos(Ship.msngFacing)
        'Determine the resultant speed
        Ship.msngSpeed = Sqr(sngXComp ^ 2 + sngYComp ^ 2)
        If Ship.msngSpeed > 5 Then Ship.msngSpeed = 5
        'Calculate the resultant heading, and adjust for arctangent by adding Pi if necessary
        If sngYComp > 0 Then Ship.msngHeading = Atn(sngXComp / sngYComp)
        If sngYComp < 0 Then Ship.msngHeading = Atn(sngXComp / sngYComp) + PI
    End If
    
    Ship.msngX = Ship.msngX + Ship.msngSpeed * Sin(Ship.msngHeading)
    Ship.msngY = Ship.msngY - Ship.msngSpeed * Cos(Ship.msngHeading)
    
    If Ship.msngX < 0 Then Ship.msngX = ScreenWidth
    If Ship.msngY < 0 Then Ship.msngY = ScreenHeight
    If Ship.msngX > ScreenWidth Then Ship.msngX = 0
    If Ship.msngY > ScreenHeight Then Ship.msngY = 0End Sub

Should I keep this code as it is, or implement some vector variables?
 
Last edited:
  • #11
OK, I had some spare time so I added some vector variables. Here is the code now:

Code:
Private Sub Physics()

Dim sngXComp As Single  'Resultant X and Y components
Dim sngYComp As Single
Dim i As Integer
                    
    'Thrust
    If mblnUpKey Then
        mblnUpKey = False
        
        Ship.Velocity.x = Ship.msngSpeed * Sin(Ship.msngHeading)
        Ship.Velocity.y = Ship.msngSpeed * Cos(Ship.msngHeading)
        
        Ship.Acceleration.x = Ship.ACCEL * Sin(Ship.msngFacing)
        Ship.Acceleration.y = Ship.ACCEL * Cos(Ship.msngFacing)

        sngXComp = Ship.Velocity.x + Ship.Acceleration.x
        sngYComp = Ship.Velocity.y + Ship.Acceleration.y
        
        Ship.msngSpeed = Sqr(sngXComp ^ 2 + sngYComp ^ 2)
        If Ship.msngSpeed > 5 Then Ship.msngSpeed = 5
        'Calculate the resultant heading, and adjust for arctangent by adding Pi if necessary
        If sngYComp > 0 Then Ship.msngHeading = Atn(sngXComp / sngYComp)
        If sngYComp < 0 Then Ship.msngHeading = Atn(sngXComp / sngYComp) + PI
    End If
    
    Ship.msngX = Ship.msngX + Ship.Velocity.x
    Ship.msngY = Ship.msngY - Ship.Velocity.y
    
    If Ship.msngX < 0 Then Ship.msngX = ScreenWidth
    If Ship.msngY < 0 Then Ship.msngY = ScreenHeight
    If Ship.msngX > ScreenWidth Then Ship.msngX = 0
    If Ship.msngY > ScreenHeight Then Ship.msngY = 0End Sub

I just had a look at Star Sonata, and this is the way they do it: When a new destination is selected (with the mouse), the ship comes to a stop, rotates to the new coordinate and then moves to it. I would like to implement this style if possible.

I have got the ship to stop, now I want to turn to the destination, thrust towards it and then stop exactly on it.
 
Last edited:
  • #12
Canning said:
First things first, I will convert my variables into velocity and acceleration vectors. I will get that fixed, then work on the moving to coordinate problem.

Am I corect in saying that the Xvelocity = speed * Sin(Heading) and Yvelocity = speed * Cos(Heading)?

and the Xacceleration = ACCEL * Sin(Facing angle) and Yacceleration = ACCEL * Cos(Facing angle)?

Use that only if you want to set the ship's velocity to a specific value but if you do that while your ship's already moving in another direction it will look like your ship suddenly changes direction. If you instead change the direction by changing the accelerations the motion will look more like a smooth turn.

ddx = thrust * cos(heading), ddy = thrust * sin(heading)

http://en.wikipedia.org/wiki/Polar_...rting_between_polar_and_Cartesian_coordinates
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 96 ·
4
Replies
96
Views
12K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 52 ·
2
Replies
52
Views
8K
  • · Replies 144 ·
5
Replies
144
Views
12K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 83 ·
3
Replies
83
Views
22K
  • · Replies 15 ·
Replies
15
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K