VB:Tutorials:Spaceship Physics

From GPWiki
Jump to: navigation, search

"An object in motion remains in motion unless acted upon by an outside force." Thank-you Sir Isaac Newton! At first this may seem to be contrary to what you'd expect, after all, if I roll a ball along the floor, it comes to a stop eventually, doesn't it? Well yes, that is true, but there's an outside force at work: friction. The force of friction between the ground and the ball (and also the air and the ball) acts to decrease the speed of the ball, causing it to come to rest at some point.

Now, if we take this discussion out into the vacuum of space (my favourite place!), we will no longer have this friction to deal with! Alright.. if there are any die-hards out there, they're probably thinking, "Ryan's stupid! There are still particles and friction in space!" Ok, well you just shut-up :) For our purposes the 3 particles per cubic meter of space (deep space) will cause negligible friction!

Where was I before those die-hards so rudely interrupted me? Oh yes, frictionless space. In space, when an object is given a little push it will continue to travel in that direction infinitely (unless acted upon by ANOTHER force)! Cool, huh? How do we calculate the resultant speed and direction? A couple of physics equations are all we need:

F = ma

"Force equals mass times acceleration." A force can be anything from gravity (which could cause our object to orbit!), to thrust from an engine (SPACESHIPS! WOOOO!).

Vf = Vi + at

"Final velocity equals initial velocity, plus acceleration times time." We will assume, for now, that the force and initial velocity have the same direction. So, do you notice any similarities between our two equations? They both contain "a" or "acceleration"... we can combine the two! Use the force!

Vf = Vi + (F/m)t

So now, if we know the magnitude and duration of the force, the mass of the object, and the object's initial velocity, we can calculate the final velocity. Let's say we have a 10000kg spaceship capable of producing 20000N (Newtons) of thrust for 5 seconds, and the spaceship is already travelling at 5m/s:

Vf = (5) + (20000/10000) * 5 = 15m/s

Our ship would end up travelling at 15m/s (meters per second). Note that this is essentially the same as adding two velocities.. the initial velocity, and the velocity induced by the force. However, if these two velocities do not have the same direction, we're in a bit of trouble! Bring on the trigonometry!

Suppose that our ship is travelling with an initial speed of Si at an angle of Theta (with zero being straight "up"). Suppose also that our ship (mass = m) is facing an angle of Phi and is about to thrust with a force of F in that direction. The two resultant velocity vectors may not be parallel, we will have to sum their components (X and Y constituent segments) in order to determine the final velocity of the ship. Let ViSegX and ViSegY represent the component segments of the initial velocity, and FSegX and FSegY represent the component segments of the velocity due to thrust:

ViSegX = sin(Theta) * Si
ViSegY = cos(Theta) * Si
FSegX = sin(Phi) * (F/m)
FSegY = cos(Phi) * (F/m)

Hey baby, what's your sine? (I do love a good math joke!) Now, we can add these values together to get the components of the final velocity vector, and use this to determine the final speed and direction:

Speed = Sqr((ViSegX + FSegX)^2 + (ViSegY + FSegY)^2)

Pythagoras rules! (Sqr = Square Root)

Direction = Atn((ViSegX + FSegX)/(ViSegY + FSegY))

Let's hear it for ArcTangent! :) (Atn = Arctangent) Don't forget, however, that arctangent will only return values in the range from -90 to 90 degrees (-π/2 to π/2 radians). You must check the value of (ViSegY + FSegY), if it is less than zero, you must add 180 degrees (or π radians) to the arctangent result to correct.

Keep in mind: all values must be converted to Radians for use in Visual Basic. To convert degrees to radians, simply multiply by (π/180).

Click here to download the source for a little spaceship simulation. It is pure VB, no DirectX, no nothing!