Results 1 to 3 of 3

Thread: Moving a ship and calculating direction to translate

  1. #1
    Guest

    Moving a ship and calculating direction to translate

    I am trying to move a ship across the screen using the arrow keys. But everytime I move the ship it moves in the wrong direction. I need it to move in the direction it is pointing. I beleive the problem is in the calculation of xd and yd, the coordinates that I am translating too...it seems to move up and down correcting but anyother direction it moves, it seems, the opposite direction...

    /************************************************** *********/
    void rotate(){
    if(rot > 360.0) rot -= 360.0;
    if(rot < 0.0) rot += 360.0;
    if(Left_isPressed)
    rot += 5.0;
    if(Right_isPressed)
    rot -= 5.0;
    if(Up_isPressed){
    yd += (cos((rot/180*3.14)) * speed);
    xd += (sin((rot/180*3.14)) * speed);
    }

    /*******function to draw ship********/
    glPushMatrix();
    glTranslatef(xd,yd,0.0);
    glRotatef(rot,0.0,0.0,1.0);
    glBegin(GL_LINES);
    for(i=0;i<=size;i++)
    glVertex2f(x[i],y[i]);
    glEnd();
    glPopMatrix();
    glutSwapBuffers();

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Chicago, IL US
    Posts
    101

    Re: Moving a ship and calculating direction to translate

    Let me first say I am not an expert.

    That being said, I think you need to have both a concept of "facing direction" and "facing movement" to implement the ship's movement properly. The ship movement is a vector (i.e. it has both magnitude and direction). These could be variables in your application's code (i.e. direction_face, direction_move).

    Also, you might want to look at:
    http://www.evl.uic.edu/aej/488/oldhw/fall2k.hw2.html

    Hope this helps.

    Good luck.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Chicago, IL US
    Posts
    101

    Re: Moving a ship and calculating direction to translate

    The "facing movement" above should read "movement direction". Sorry.

Similar Threads

  1. moving space ship
    By pbivens in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-12-2018, 06:21 AM
  2. Moving A Ship Rotation Problem
    By MoLAoS in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-07-2012, 04:30 PM
  3. Moving A Ship Rotation Problem
    By MoLAoS in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 03-07-2012, 04:30 PM
  4. gluLookAt translate in y direction
    By ulric in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-15-2004, 12:26 PM
  5. Problem with translate in z direction
    By aus79er in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-16-2002, 03:33 PM

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