Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: moving a 2d sprite with OpenGL

  1. #1
    Guest

    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..

  2. #2
    Guest

    Re: moving a 2d sprite with OpenGL

    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

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: moving a 2d sprite with OpenGL

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


    Originally posted by eyuzwa:
    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..
    [This message has been edited by nexusone (edited 02-02-2002).]

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

  4. #4
    Guest

    Re: moving a 2d sprite with OpenGL

    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,

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: moving a 2d sprite with OpenGL

    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:
    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,

  6. #6
    Guest

    Re: moving a 2d sprite with OpenGL

    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!

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: moving a 2d sprite with OpenGL

    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:
    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!


  8. #8
    Guest

    Re: moving a 2d sprite with OpenGL

    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...

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: moving a 2d sprite with OpenGL

    Try changing this line:

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


    Originally posted by eyuzwa:
    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..

  10. #10
    Guest

    Re: moving a 2d sprite with OpenGL

    nope same result...

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 06-16-2016, 04:47 AM
  2. [opengl 2.1]How should I move my 2d sprite?
    By ochi12 in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-24-2015, 12:39 PM
  3. Not purely opengl but making a sprite class
    By blubee in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 09-02-2012, 11:24 PM
  4. OpenGL ES 1.1: transparent line + sprite/texture
    By Lococo in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-12-2011, 02:25 PM
  5. OpenGL, SDL and sprite Collision detection
    By Andreas K in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 06-21-2010, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean