moving a 2d sprite with OpenGL

okay, this is probably a simple problem and I’m obviously overlooking something trivial…

ALL I’m trying to do is to move my spaceship.
I can rotate left and right properly. However, when I try to use the up arrow for “thrusting”, my ship flies off in another direction…

my code??
float vx, vy;
double dAng;
dAng = (ship.rotation / 180) * PI;
vx = (FLOAT)sin(dAng);
vy = (FLOAT)cos(dAng);

ship.position.x += vx;
ship.position.y += vy;

argh!! Every sample I could find does it this
way…what am I missing??

thanks in advance…

explain what you want exactly to do with your space ship, send it to me (y.moumen@voila.fr). And I 'll try my best to help you

Is it going oposite? That is if you press up it does down?

Originally posted by eyuzwa:
[b]okay, this is probably a simple problem and I’m obviously overlooking something trivial…

ALL I’m trying to do is to move my spaceship.
I can rotate left and right properly. However, when I try to use the up arrow for “thrusting”, my ship flies off in another direction…

my code??
float vx, vy;
double dAng;
dAng = (ship.rotation / 180) * PI;
vx = (FLOAT)sin(dAng);
vy = (FLOAT)cos(dAng);

ship.position.x += vx;
ship.position.y += vy;

argh!! Every sample I could find does it this
way…what am I missing??

thanks in advance…[/b]

[This message has been edited by nexusone (edited 02-02-2002).]

[This message has been edited by nexusone (edited 02-02-2002).]

thanks for the responses!

No it goes UP only…
ie. I’m facing to the left, I hit thrust, and I go upwards rather than to the left…

or if I rotate over to the right, and hit thrust, I still only go upwards…

thanks,

Are you using degrees for you ship’s rotation?

ship.rotation = 45.0;

The first part of the code coverts degrees to rad’s.

Originally posted by eyuzwa:
[b]thanks for the responses!

No it goes UP only…
ie. I’m facing to the left, I hit thrust, and I go upwards rather than to the left…

or if I rotate over to the right, and hit thrust, I still only go upwards…

thanks,[/b]

Yeah I believe I’m using degrees (for the rotation)…

ie.
if(player.controls[DIK_LEFT] & 0x80)
{
player.fRotation -= 10.0f;
if(player.fRotation < 0.0f)
player.fRotation = 350.0f;

}

etc…

since opengl handles the rotation matrices with radians, I first convert the rotation variable (in degrees) to radians before
doing the sin/cos stuff…

I KNOW that I’m overlooking something really trivial here, that everyone will just laugh at once it’s figured out…grin

thanks for the help so far, I really appreciate it!

If you are using the glRotate it is in degrees not radians.

glRotate( 45.0, 0, 0, 1) if you are looking down on the xy plane then you rotate the Z axis.

Now the sin/cos function in C uses the radians format.

example:
glRotate(player.fRotation, 0, 0, 1) no conversion to radians needed.

Originally posted by eyuzwa:
[b]Yeah I believe I’m using degrees (for the rotation)…

ie.
if(player.controls[DIK_LEFT] & 0x80)
{
player.fRotation -= 10.0f;
if(player.fRotation < 0.0f)
player.fRotation = 350.0f;

}

etc…

since opengl handles the rotation matrices with radians, I first convert the rotation variable (in degrees) to radians before
doing the sin/cos stuff…

I KNOW that I’m overlooking something really trivial here, that everyone will just laugh at once it’s figured out…grin

thanks for the help so far, I really appreciate it!

[/b]

nexusone: Thanks for the help, but I think you’re misinterpreting my plea for aid. I can rotate my sprite JUST fine. It works perfectly.

I just can’t (properly) MOVE my sprite in the direction it’s pointing…

THAT’s the crux of my dilemma…

Try changing this line:

dAng = (ship.rotation / 360) * 2 * PI;

Originally posted by eyuzwa:
[b]okay, this is probably a simple problem and I’m obviously overlooking something trivial…

ALL I’m trying to do is to move my spaceship.
I can rotate left and right properly. However, when I try to use the up arrow for “thrusting”, my ship flies off in another direction…

my code??
float vx, vy;
double dAng;
dAng = (ship.rotation / 180) * PI;
vx = (FLOAT)sin(dAng);
vy = (FLOAT)cos(dAng);

ship.position.x += vx;
ship.position.y += vy;

argh!! Every sample I could find does it this
way…what am I missing??

thanks in advance…[/b]

nope same result…

I don’t know much about opengl however there does seem to be a logical explaination for the problem. Are you sure that the math library you’re using uses rads for the cosine and sine. What I would try is just passing in the angle in degrees. The reason this seems logical is that 0 degrees is pointing up right? Therefore even if you rotate the ship 180 degrees that’s only going to convert to pie radians, right. However ship moving at 3 degrees would look like it is moving straight up. Hope this helps.

Thanks for the suggestions…yeah I’ll try
just using the coords without doing the conversion to rads…???

I’ll post an update later…

crap still no good. Is there anything
extra you have to do for the rendering code?? Or is glRotate on the z-axis enough?

I don’t know if I was clear enough. I think (if of course the sine and cosine functions are in degrees) that your code should look like this:
vx = (FLOAT)sin(ship.rotation)
xy = (FLOAT)cos(ship.rotation)
If that doesn’t work it means that ship.rotation is either not in degrees or your math library doesn’t use degrees.
I would then go on to test the program further by outputing the value of ship.rotation somewhere on the screen so you can see what is happining (what the values are) while you rotate the ship.
Best of luck.

Originally posted by dmath1:
I would then go on to test the program further by outputing the value of ship.rotation somewhere on the screen so you can see what is happining (what the values are) while you rotate the ship.
Best of luck.

If you have problems outputting to screen try the following code after calculating vy.

FILE *Log = fopen(“Debug.txt”, “a”);
fprintf(Log, "dAng = %f, vx = %f, vy = %f.
", dAng, vx, vy);
fclose(Log);

This sends the results to a text file (Debug.txt) in your working directory and you can compare the values with what you should be getting.

thanks for the suggestions…

According to the msdn, math.h defines sin and cos as using an angle already converted to radians, which is why I was originally doing the conversion…

is there another math library I should be using, or is math.h good enough??

You are working out the vector position of the space craft, but which coordinate system is it relating to? If you ship always flies straight up, then it is referring to the origin, as opposed to the new coordinate system which needs to be created when you rotate your ship, do you see what I mean. Think about push/pop/modelview matrix/rotate/translate all work.

You are working out the vector position of the space craft, but which coordinate system is it relating to? If you ship always flies straight up, then it is referring to the origin, as opposed to the new coordinate system which needs to be created when you rotate your ship, do you see what I mean. Think about push/pop/modelview matrix/rotate/translate all work.

Gavin: Thanks for the suggestions…I was starting to come to that conclusion last night. sigh. After going through mounds of debug spew, the positional calculations looked ok, so perhaps this means an error within my OpenGL rendering code.

ie.
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(pang, 0.0f, 0.0f, 1.0f);
glTranslatef(ship.x, ship.y, ship.z);

glCallList(ship_list);

glPopMatrix();

(where pang is the angle in degrees)

Man this SHOULD be so easy to do…argh.

thanks to all so far who’ve tried to help me, I really appreciate it…