Moving a Spaceship to its Destination

  • Thread starter Thread starter Canning
  • Start date Start date
  • Tags Tags
    Spaceship
AI Thread Summary
The discussion focuses on programming a 2D spaceship movement in a game, specifically how to move the spaceship to a destination coordinate while managing its current motion. Key points include using vector mathematics to adjust the ship's velocity and acceleration based on its facing angle and thrust. The conversation suggests that when the ship is already moving, it should turn to face the new direction and apply thrust accordingly, rather than abruptly changing its velocity. The implementation of vector variables for acceleration and velocity is recommended for smoother motion transitions. The user aims to refine their code to allow the ship to stop, rotate, and then move to a selected destination accurately.
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

2
Replies
96
Views
10K
2
Replies
52
Views
7K
Replies
7
Views
3K
2
Replies
83
Views
21K
Replies
4
Views
3K
Replies
8
Views
5K
Replies
19
Views
4K
Replies
16
Views
4K
Back
Top