>Ok. Heres the scenario. You have a ship. We'll call it A 'cause I
am
>creative. So Ship A zooming along at SPEED in a certain direction
ANGLE.
>Now, ship A decides to turn ANGLE2 and fire its boosters with a
SPEED2.
>Now, exactly what angle would this so-called ship A be at, and what
speed
>(ever played EV, Steel Fighters, or just about any other nice
ship-combat
>game)?
Peter
First of all please view this with a fixed width font such as Courier New
due to my attempt at ASCII art diagrams that I've included.
To give an effect of inertia is actually very easy to do and it's a lot more
complicated to describe than it is to program it! You are currently working
with what are essentially polar co-ordinates using ANGLE as your direction
and SPEED as your distance to move. This is a very convenient way to store
game data because it is easy to change when the user wants to rotate the
spaceship etc. However, when programatically moving your spaceship it's
easier to use a vector description of your ANGLE & SPEED.
.
. .
. .
. .
. .
r . .
. .
. . y
. .
. .
. .
. .
. a .
. . . . . . . . . . . . . .
x
The above diagram is (most importantly) a right angled triangle where x, y &
r are the lengths of those sides and a is the angle in that corner. In your
example a corresponds to ANGLE and r corresponds to SPEED. Some useful
relationships can be extracted from this diagram:
1. x = r * COS(a)
2. y = r * SIN(a)
This diagram is also describing a vector that is moving x units right & y
units up with a magnitude of r. So now you can convert to a vector using
equations 1 & 2 above. This sounds clever but you'll just have 2 variables
x & y that are used to represent your vector.
Anyway, on to the real point. You need 6 components to move an object in 2
dimensions:
i) x position
ii) y position
iii) x velocity
iv) y velocity
v) x acceleration
vi) y acceleration
The whole point of doing this is to simulate inertia, this is how you do it:
Step 1 - Calculate the acceleration
Step 2 - Add the acceleration to the velocity
Step 3 - Add the velocity to the position
Suppose you have just run your game and your space ship is sitting in the
middle of the screen, not moving and facing east. This is how I imagine it
would work in your game:
Initial Conditions
Let the thrust be a value of say 3 (when the thrust key is pressed).
Let ANGLE be 90 (facing east)
Step A - You press the thrust key, this is your acceleration towards the
east because that is the way your spaceship is facing.
Step B - Convert this acceleration to a vector using equations 1 & 2 above
(x = 3 * COS(ANGLE) & y = 3 * SIN(ANGLE)). This gives x = 3 & y = 0.
Step C - Add the acceleration to the velocity. This gives VelocityX = 3 &
VelocityY = 0
Step D - Add the velocity to the position. This gives PositionX = 3 &
PositionY = 0
Now suppose we don't press any keys, we still run you main loop that is
doing all of the above so your spaceship continues to move to the east at 3
distance units per iteration.
Or suppose we rotate the spaceship to face north and then press the thrust
key again (here we go again):
Initial Conditions
Let the thrust be a value of say 3 (when the thrust key is pressed).
Let ANGLE be 0 (facing north)
Step A - You press the thrust key, this is your acceleration towards the
north because that is the way your spaceship is facing.
Step B - Convert this acceleration to a vector using equations 1 & 2 above
(x = 3 * COS(ANGLE) & y = 3 * SIN(ANGLE)). This gives x = 0 & y = 3.
Step C - Add the acceleration to the velocity. This gives VelocityX = 3 &
VelocityY = 3
Step D - Add the velocity to the position. This gives PositionX = 6 &
PositionY = 3
You'll note that Step D has produced the desired result, the initial
Easterly velocity is retained even though we are now facing north and adding
a northerly component to your velocity, whereas in your existing version I
assume the position would have been 3,3 not 6,3.
You'll have to play with the value of your thrust to get it to look good,
probably a value >0 & <1 would be good. Now you have a good model of
movement without any fudge factors involved, it's easy to tinker with.
Suppose you wanted to include 'power ups' that made you go faster, you could
just double the value of your thrust variable and leave the rest alone - no
SELECTs, IFs or ANDs (or pots or pans?).
Please feel free to e-mail me off list, or on list (or over list or under
list?) if you want to. I have been reading the FB list for about 6 months
but not contributed much. My reason for replying to this is that about 2
years ago I (for some reason probably the hell of it) desperately wanted to
simulate a moon orbiting a planet no matter where the moon was or how fast
it was going in whatever direction. This led me down the exact path you're
on, through all the problems of reading physics books and wondering, how the
hell do you code that? Anyway, I now have a Visual Basic app that will
model ~500 objects in 3D space using Newton's laws that all interact / orbit
with one another. It will also download data from NASA on real solar system
objects and model that too, you can zoom in & out on objects and attach
multiple cameras to them for different views etc. I have a cut down FB^3
version that I programmed when I first learnt FB in March (I have to say
that because I work with Phil Yates and he'll think I've been slacking
off!). I plan to one day make it into shareware or at least a screensaver,
would anybody be interested in "My Universe"?
Hope this both helps & makes some sense.
Cheers
Stu