Moving a Spaceship to its Destination

  • Thread starter Canning
  • Start date
  • Tags
    Spaceship
In summary: End SubThis is done in the OnGUI function. Here is the code:Private Sub OnGUI()If mblnUpKey Then'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
  • #1
Canning
7
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
  • #2
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
 
  • #3
This looks like it might belong in the Computer and Technology forum since it's programming. Perhaps an admin will move it.
 
  • #4
If the ship is already moving then why not just adjust the direction vector?
 
  • #5
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.
 
  • #6
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?
 
  • #7
Are you using VB or VB .NET?
 
  • #8
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.
 
  • #9
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
 

FAQ: Moving a Spaceship to its Destination

How does a spaceship move through space?

A spaceship moves through space using thrusters or engines that generate thrust, which propels the spacecraft forward. Depending on the type of spacecraft, the thrust can be generated by burning fuel or by using electric or nuclear power.

What factors affect the speed and trajectory of a spacecraft?

The speed and trajectory of a spacecraft are affected by several factors, including the amount of thrust generated by its engines, the mass of the spacecraft, and the gravitational pull of nearby objects. Other factors such as atmospheric drag and gravitational slingshots can also influence the spacecraft's speed and trajectory.

How do scientists determine the most efficient route for a spaceship to reach its destination?

Scientists use complex mathematical equations and computer simulations to determine the most efficient route for a spaceship to reach its destination. They take into account factors such as the gravitational pull of planets and other objects, the amount of fuel needed, and the time it takes to reach the destination.

What challenges do scientists face when moving a spaceship to its destination?

One of the main challenges scientists face is ensuring that the spaceship has enough fuel to reach its destination. They also have to carefully calculate the trajectory and make adjustments along the way to avoid any potential collisions with other objects in space. Additionally, long-distance space travel can also have adverse effects on the health of astronauts, so measures need to be taken to mitigate these risks.

How do scientists navigate a spaceship to its destination?

Scientists use various methods to navigate a spaceship to its destination. For short distances, they may use manual control, but for longer distances, they rely on automated systems that use sensors and data from Earth to make necessary adjustments to the spacecraft's trajectory. They also use stars and other celestial objects as reference points for navigation.

Similar threads

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