Search code examples
c++2dsdl2d-games

2D Movement in a specific direction


I am learning how to move around a 2D map and need help with using trigonometry functions.

Here is my current code.

const Uint8 * key = SDL_GetKeyboardState(NULL);
    if(key[SDL_SCANCODE_D]){
        if(render_arrow){
            arrow.Angle(1);
        }
    }
    if(key[SDL_SCANCODE_A]){
        if(render_arrow){
            arrow.Angle(-1);
        }
    }
    if(key[SDL_SCANCODE_LEFT]){
        if(render_arrow){
            arrow.set_location(arrow.X_Co() - 1, arrow.Y_Co());
        }
    }
    if(key[SDL_SCANCODE_RIGHT]){
        if(render_arrow){
            arrow.set_location(arrow.X_Co() + 1, arrow.Y_Co());
        }
    }
    if(key[SDL_SCANCODE_UP]){
        if(render_arrow){
            arrow.set_location(arrow.X_Co(), arrow.Y_Co() - 1);
        }
    }
    if(key[SDL_SCANCODE_DOWN]){
        if(render_arrow){
            arrow.set_location(arrow.X_Co(), arrow.Y_Co() + 1);
        }
    }

But this has obvious limitations and no link between direction and angle.

What i do know is that there is a function in that somehow use the the tangent ratio to calculate the quadrant you are facing and give you some numbers to adjust the location. However i don't know the function nor how to use it properly.

What i would like to be able to do is have the arrow move in the direction it is pointing when i press UP, and backwards when i press DOWN. And to strafe side to side with LEFT and RIGHT. The A and D keys are used to turn the angle.

Any helpful algorithms you might know would be greatly appreciated.

Edit: Angle(0) returns the current angle.


Solution

  • if(key[SDL_SCANCODE_UP]){
        if(render_arrow){
            deltaX = AMOUNT_TO_MOVE_IN_FROM_ONE_KEYSTROKE * cos (arrow.angle());
            deltaY = AMOUNT_TO_MOVE_IN_FROM_ONE_KEYSTROKE * sin (arrow.angle());
    
            arrow.set_location(arrow.X_Co()+deltaX, arrow.Y_Co() +deltaY);
        }
    }
    if(key[SDL_SCANCODE_DOWN]){
        if(render_arrow){
                                                                     //Note the -'s
            deltaX = - AMOUNT_TO_MOVE_IN_FROM_ONE_KEYSTROKE * cos (arrow.angle()); 
            deltaY = - AMOUNT_TO_MOVE_IN_FROM_ONE_KEYSTROKE * sin (arrow.angle());
            arrow.set_location(arrow.X_Co()+deltaX, arrow.Y_Co() +deltaY);
        }
    }
    

    For more information on converting from angle to x y coordinates, see this page http://www.mathsisfun.com/polar-cartesian-coordinates.html (scroll down to "To Convert from Polar to Cartesian"), or Google "convert polar cartesian."