Note: This is pretty off-topic for the SDL list, a general game programming
list would be more appropriate. But, to reward this improper behavior…
So you have it rotating, so you presumably already have the following
info about the ship:
So what you sound like you need is momentum. What I would do is have
some momentum variables, call them “xm” and “ym”, or “dx” and “dy”, or
whatever you prefer. (I’m going with the standard “Space War” or
"Asteroids" ship movement… you apply thrust, and the ship goes. The ship
does not stop going until you thrust the other direction.)
During each execution of your game loop, apply the momentum to your ship.
// Apply the thrust:
x += xm;
y += ym;
// Wrap around the edges:
if (x < 0) // Wrap from left to right
x += screen->w;
else if (x >= screen->w) // Wrap from right to left
x -= screen->w;
if (y < 0) // Wrap from top to bottom
y += screen->h;
else if (y >= screen->h) // Wrap from bottom to top
y -= screen->h;
This is great, except now you need to be able to change your momentum.
For example, if you push the UP arrow, make the ship ‘go forward’,
where ‘forward’ depends on what direction it’s facing.
For this, you just need some trigonometry. Let’s go the slow way and
assume your X,Y coordinates and XM,YM momentum values are all floats,
and just use the math library’s sin() and cos() functions. A faster way
would be to use ints and fixed-point math (as I did in my game "Vectoroids,"
which kept it snappy on FPU-less devices, such as the Sharp Zaurus PDA).
if (keysym == SDLK_UP)
xm += cos(angle) * thrust_speed;
ym -= sin(angle) * thrust_speed;
Oh, and of course that assumes angle is in radians. If it’s in degrees
(0 through 360), you’ll need to convert it to radians, which is what
sin() and cos() in the C math library expect:
xm += cos(angle * (M_PI / 180.0)) * thrust_speed; // Mmm… pie. drool
Depending on your game logic, etc., you may want to throttle your
speed to a certain limit. Esp. if your motion literally goes from one
X,Y position to the next, without checking for collisions in between…
(You’d want line intersect routines for that, for example.)
Good luck,On Wed, Aug 22, 2007 at 07:24:49PM -0400, L-28C wrote:
I’ve asked Mr. Google but all I find is code to do the actual bitmap
rotation, but I’m using SGE for that.
bill at newbreedsoftware.com