Rotation in 2D

Hi
I’m using OpenGL and SDL for a 2D game that I’m trying to make.
I have a sprite that I rotate with help of glRotatef and it works but…
I want to make the sprite to move in the direction that it is “pointing to”, I
cannot find a way to make it work.

I have tested with something like this (I read about it somewhere):
xpos += velx * std::cos(angle) * time_scale;
ypos += vely * -std::sin(angle) * time_scale;

But that did not work, anyone who knows how I can do this?

“Did not work” in what way?

The only thing I can imagine is that your sprites end up rotating in
the wrong direction, as a result of the visual rotation having a
different axis orientation from that of the game logic code above.

Anyway, the signs depend on the relation between OpenGL rotations and
axis orientation. Keep in mind that OpenGL things increasing Y means
upwards, while many other APIs has it the other way around!

If you’re not sure about this (and the trig and stuff), this is a case
where you can simply remove all “mysterious” negations, test, and
then insert one in a sensible place if things are mirrored.

Anyther thing to check: Are your sprite images drawn pointing in the
0? direction…? If not, you’ll have to fix them, or (probably
better) adjust the rendering rotation appropriately.

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Wednesday 10 September 2003 14.38, Alexander Bussman wrote:

Hi
I’m using OpenGL and SDL for a 2D game that I’m trying to make.
I have a sprite that I rotate with help of glRotatef and it works
but… I want to make the sprite to move in the direction that it
is “pointing to”, I cannot find a way to make it work.

I have tested with something like this (I read about it somewhere):
xpos += velx * std::cos(angle) * time_scale;
ypos += vely * -std::sin(angle) * time_scale;

But that did not work, anyone who knows how I can do this?

I have tested with something like this (I read about it somewhere):
xpos += velx * std::cos(angle) * time_scale;
ypos += vely * -std::sin(angle) * time_scale;

But that did not work, anyone who knows how I can do this?

Uhm… the code seems to be right but:

  • you have to be sure that angle is expressed in radiants,
    if it isn’t, you have to convert it doing this (C code):
    rad = (double)angle*M_PI/180;

  • maybe you will have to put “sin” in place of “cos” and viceversa,
    it depends on where you have the angle 0.

Hope that will be useful.

Bye,
Enzo.

It should work. What do you mean by “it doesn’t work”? It doesn’t move? It does move but in a wrong direction?

If your xpos and ypos are integers and you’re getting a lot of FPS, it’s possible that velx*cos(a) is less than 1 and therefore the integer never gets modified.

Lic. Gabriel Gambetta
ARTech - GeneXus Development Team
ggambett at artech.com.uy> ----- Original Message -----

From: Alexander Bussman [mailto:alexander.bussman@telia.com]
Sent: Mi?rcoles, 10 de Septiembre de 2003 09:38 a.m.
To: sdl at libsdl.org
Subject: [SDL] Rotation in 2D

Hi
I’m using OpenGL and SDL for a 2D game that I’m trying to make. I have a sprite that I rotate with help of glRotatef and it works but… I want to make the sprite to move in the direction that it is “pointing to”, I
cannot find a way to make it work.

I have tested with something like this (I read about it somewhere): xpos += velx * std::cos(angle) * time_scale; ypos += vely * -std::sin(angle) * time_scale;

But that did not work, anyone who knows how I can do this?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

It seems like your approach is not wrong but if you haven’t mistyped it
it should be
xpos += vel * std::cos(angle) * time_scale; /* not velx*/
ypos += vel * -std::sin(angle) * time_scale; /* not vely */
'cause the multiplication of velocity with the trigonometric funcs
gives you the vertical and horizantal speed.
Also you can try a first translate forward the rotate approach so that
your sprite moves in the direction it looks (remember in openGL you
should write the code in reverse order, last transformation done is
written first). But you should be careful about your origin.On Wed, 2003-09-10 at 12:38, Alexander Bussman wrote:

Hi
I’m using OpenGL and SDL for a 2D game that I’m trying to make.
I have a sprite that I rotate with help of glRotatef and it works but…
I want to make the sprite to move in the direction that it is “pointing to”, I
cannot find a way to make it work.

I have tested with something like this (I read about it somewhere):
xpos += velx * std::cos(angle) * time_scale;
ypos += vely * -std::sin(angle) * time_scale;

But that did not work, anyone who knows how I can do this?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Perit Bezek :slight_smile:

Thanks for the fast reply.
that “did not work” thing meant that it seams to be very wrong :slight_smile:
If I rotate the sprite a few degrees and try to let it move forward it moves
in one way and if I rotate it some more degrees it moves in a totally
diffrent direction.

I increse the angle with 0.15 if I press the right arrow on the keyboard and
-0.15 with the left arrow key (just for testing now). And then when I press
the up arrow it should move in the direction it is pointing to (just to
explain how I test this).

Well here the rendering / rotation code is:
(GL_Rect is a structure with the x,y,w,h in floats are in)
void gfx::draw_rect(GL_Rect rect, GLuint texture, double rotate)
{
glPushMatrix();
glLoadIdentity();
glTranslatef(rect.x, rect.y, 0.0f);
glRotatef(rotate, 0.0f, 0.0f, 1.0f);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex2f(rect.w, 0);
glTexCoord2f(1.0f, 0.0f); glVertex2f(rect.w, rect.h);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0, rect.h);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopAttrib();
glPopMatrix();
}

Anything wrong in there?On Wednesday 10 September 2003 15.07, David Olofson wrote:

On Wednesday 10 September 2003 14.38, Alexander Bussman wrote:

Hi
I’m using OpenGL and SDL for a 2D game that I’m trying to make.
I have a sprite that I rotate with help of glRotatef and it works
but… I want to make the sprite to move in the direction that it
is “pointing to”, I cannot find a way to make it work.

I have tested with something like this (I read about it somewhere):
xpos += velx * std::cos(angle) * time_scale;
ypos += vely * -std::sin(angle) * time_scale;

But that did not work, anyone who knows how I can do this?

“Did not work” in what way?

The only thing I can imagine is that your sprites end up rotating in
the wrong direction, as a result of the visual rotation having a
different axis orientation from that of the game logic code above.

Anyway, the signs depend on the relation between OpenGL rotations and
axis orientation. Keep in mind that OpenGL things increasing Y means
upwards, while many other APIs has it the other way around!

If you’re not sure about this (and the trig and stuff), this is a case
where you can simply remove all “mysterious” negations, test, and
then insert one in a sensible place if things are mirrored.

Anyther thing to check: Are your sprite images drawn pointing in the
0? direction…? If not, you’ll have to fix them, or (probably
better) adjust the rendering rotation appropriately.

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.

| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |

`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

OpenGL uses degrees, standard cos/sin in the math library use
radians. Anyway, if d is in degrees, you can convert to radians
using the following formulae where r is in radians:

r = 3.14 * d / 180.0

… where you can use some more accurate constant for pi instead of my
ad hoc 3.14 (but it is enough for most simple game applications :slight_smile:

So I guess you’ll have to decide whichever you want the variable
’rotate’ to mean (degrees or radians), and then convert appropriately
either in the OpenGL code section or in the sin/cos code section…

Personally, I use conversion functions in which I use the above
formulae:

float deg2rad(float deg);
float rad2deg(float rad);

/Olof

Alexander Bussman: “Re: [SDL] Rotation in 2D” (2003-09-10 15:45)

#Thanks for the fast reply.
#that “did not work” thing meant that it seams to be very wrong :slight_smile:
#If I rotate the sprite a few degrees and try to let it move forward it moves
#in one way and if I rotate it some more degrees it moves in a totally
#diffrent direction.#
#I increse the angle with 0.15 if I press the right arrow on the keyboard and
#-0.15 with the left arrow key (just for testing now). And then when I press
#the up arrow it should move in the direction it is pointing to (just to
#explain how I test this).

#Well here the rendering / rotation code is:
#(GL_Rect is a structure with the x,y,w,h in floats are in)
#void gfx::draw_rect(GL_Rect rect, GLuint texture, double rotate)
#{

glPushMatrix();

glLoadIdentity();

glTranslatef(rect.x, rect.y, 0.0f);

glRotatef(rotate, 0.0f, 0.0f, 1.0f);

glPushAttrib(GL_ALL_ATTRIB_BITS);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_QUADS);

glTexCoord2f(0.0f, 1.0f); glVertex2f(0, 0);

glTexCoord2f(1.0f, 1.0f); glVertex2f(rect.w, 0);

glTexCoord2f(1.0f, 0.0f); glVertex2f(rect.w, rect.h);

glTexCoord2f(0.0f, 0.0f); glVertex2f(0, rect.h);

glEnd();

glDisable(GL_TEXTURE_2D);

glPopAttrib();

glPopMatrix();

#}

#Anything wrong in there?

#On Wednesday 10 September 2003 15.07, David Olofson wrote:
#> On Wednesday 10 September 2003 14.38, Alexander Bussman wrote:
#> > Hi
#> > I’m using OpenGL and SDL for a 2D game that I’m trying to make.
#> > I have a sprite that I rotate with help of glRotatef and it works
#> > but… I want to make the sprite to move in the direction that it
#> > is “pointing to”, I cannot find a way to make it work.
#> >
#> > I have tested with something like this (I read about it somewhere):
#> > xpos += velx * std::cos(angle) * time_scale;
#> > ypos += vely * -std::sin(angle) * time_scale;
#> >
#> > But that did not work, anyone who knows how I can do this?
#>
#> “Did not work” in what way?
#>
#> The only thing I can imagine is that your sprites end up rotating in
#> the wrong direction, as a result of the visual rotation having a
#> different axis orientation from that of the game logic code above.
#>
#> Anyway, the signs depend on the relation between OpenGL rotations and
#> axis orientation. Keep in mind that OpenGL things increasing Y means
#> upwards, while many other APIs has it the other way around!
#>
#> If you’re not sure about this (and the trig and stuff), this is a case
#> where you can simply remove all “mysterious” negations, test, and
#> then insert one in a sensible place if things are mirrored.
#>
#> Anyther thing to check: Are your sprite images drawn pointing in the
#> 0? direction…? If not, you’ll have to fix them, or (probably
#> better) adjust the rendering rotation appropriately.
#>
#>
#> //David Olofson - Programmer, Composer, Open Source Advocate
#>
#> .- The Return of Audiality! --------------------------------.
#>
#> | Free/Open Source Audio Engine for use in Games or Studio. |
#> | RT and off-line synth. Scripting. Sample accurate timing. |
#>
#> `-----------------------------------> http://audiality.org -’
#> — http://olofson.nethttp://www.reologica.se
#>
#>
#> _______________________________________________
#> SDL mailing list
#> SDL at libsdl.org
#> http://www.libsdl.org/mailman/listinfo/sdl

#_______________________________________________
#SDL mailing list
#SDL at libsdl.org
#http://www.libsdl.org/mailman/listinfo/sdl

oh, stupid me thanks for the help.
Now it works very good! :)On Wednesday 10 September 2003 16.12, Olof Bjarnason wrote:

OpenGL uses degrees, standard cos/sin in the math library use
radians. Anyway, if d is in degrees, you can convert to radians
using the following formulae where r is in radians:

r = 3.14 * d / 180.0

… where you can use some more accurate constant for pi instead of my
ad hoc 3.14 (but it is enough for most simple game applications :slight_smile:

So I guess you’ll have to decide whichever you want the variable
’rotate’ to mean (degrees or radians), and then convert appropriately
either in the OpenGL code section or in the sin/cos code section…

Personally, I use conversion functions in which I use the above
formulae:

float deg2rad(float deg);
float rad2deg(float rad);

/Olof

Alexander Bussman: “Re: [SDL] Rotation in 2D” (2003-09-10 15:45)

#Thanks for the fast reply.
#that “did not work” thing meant that it seams to be very wrong :slight_smile:
#If I rotate the sprite a few degrees and try to let it move forward it
moves #in one way and if I rotate it some more degrees it moves in a
totally #diffrent direction.

#I increse the angle with 0.15 if I press the right arrow on the keyboard
and #-0.15 with the left arrow key (just for testing now). And then when I
press #the up arrow it should move in the direction it is pointing to (just
to #explain how I test this).

#Well here the rendering / rotation code is:
#(GL_Rect is a structure with the x,y,w,h in floats are in)
#void gfx::draw_rect(GL_Rect rect, GLuint texture, double rotate)
#{

glPushMatrix();

glLoadIdentity();

glTranslatef(rect.x, rect.y, 0.0f);

glRotatef(rotate, 0.0f, 0.0f, 1.0f);

glPushAttrib(GL_ALL_ATTRIB_BITS);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_QUADS);

glTexCoord2f(0.0f, 1.0f); glVertex2f(0, 0);

glTexCoord2f(1.0f, 1.0f); glVertex2f(rect.w, 0);

glTexCoord2f(1.0f, 0.0f); glVertex2f(rect.w, rect.h);

glTexCoord2f(0.0f, 0.0f); glVertex2f(0, rect.h);

glEnd();

glDisable(GL_TEXTURE_2D);

glPopAttrib();

glPopMatrix();

#}

#Anything wrong in there?

#On Wednesday 10 September 2003 15.07, David Olofson wrote:
#> On Wednesday 10 September 2003 14.38, Alexander Bussman wrote:
#> > Hi
#> > I’m using OpenGL and SDL for a 2D game that I’m trying to make.
#> > I have a sprite that I rotate with help of glRotatef and it works
#> > but… I want to make the sprite to move in the direction that it
#> > is “pointing to”, I cannot find a way to make it work.
#> >
#> > I have tested with something like this (I read about it somewhere):
#> > xpos += velx * std::cos(angle) * time_scale;
#> > ypos += vely * -std::sin(angle) * time_scale;
#> >
#> > But that did not work, anyone who knows how I can do this?
#>
#> “Did not work” in what way?
#>
#> The only thing I can imagine is that your sprites end up rotating in
#> the wrong direction, as a result of the visual rotation having a
#> different axis orientation from that of the game logic code above.
#>
#> Anyway, the signs depend on the relation between OpenGL rotations and
#> axis orientation. Keep in mind that OpenGL things increasing Y means
#> upwards, while many other APIs has it the other way around!
#>
#> If you’re not sure about this (and the trig and stuff), this is a case
#> where you can simply remove all “mysterious” negations, test, and
#> then insert one in a sensible place if things are mirrored.
#>
#> Anyther thing to check: Are your sprite images drawn pointing in the
#> 0? direction…? If not, you’ll have to fix them, or (probably
#> better) adjust the rendering rotation appropriately.
#>
#>
#> //David Olofson - Programmer, Composer, Open Source Advocate
#>
#> .- The Return of Audiality! --------------------------------.
#>
#> | Free/Open Source Audio Engine for use in Games or Studio. |
#> | RT and off-line synth. Scripting. Sample accurate timing. |
#>
#> `-----------------------------------> http://audiality.org -’
#> — http://olofson.nethttp://www.reologica.se
#>
#>
#> _______________________________________________
#> SDL mailing list
#> SDL at libsdl.org
#> http://www.libsdl.org/mailman/listinfo/sdl

#_______________________________________________
#SDL mailing list
#SDL at libsdl.org
#http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl