Moving a Spaceship to its Destination

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

This discussion focuses on implementing movement mechanics for a 2D spaceship in a computer game using Visual Basic (VB6). Key variables include acceleration (ACCEL), rotation rate (ROTATION_RATE), and ship coordinates (msngX, msngY). The conversation emphasizes the importance of using velocity and acceleration vectors for smooth movement and turning, particularly when transitioning to a new destination. The final implementation involves calculating the ship's velocity and acceleration based on its heading and facing angle, ensuring realistic motion dynamics.

PREREQUISITES
  • Understanding of 2D physics principles, particularly velocity and acceleration vectors.
  • Familiarity with Visual Basic 6 (VB6) programming language.
  • Knowledge of trigonometric functions (Sin, Cos) for calculating movement directions.
  • Basic concepts of coordinate systems, specifically Cartesian coordinates.
NEXT STEPS
  • Implement vector mathematics for smoother spaceship movement in VB.NET.
  • Research the physics of motion to refine acceleration and deceleration mechanics.
  • Explore user input handling for destination selection and movement initiation.
  • Learn about interpolation techniques for smoother transitions between movement states.
USEFUL FOR

Game developers, particularly those working on 2D games, programmers interested in physics simulations, and anyone looking to enhance their understanding of movement mechanics in game design.

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 96 ·
4
Replies
96
Views
11K
  • · Replies 52 ·
2
Replies
52
Views
7K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 83 ·
3
Replies
83
Views
22K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 19 ·
Replies
19
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 16 ·
Replies
16
Views
4K