Reputation: 29
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.
Upvotes: 0
Views: 1309
Reputation: 2898
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."
Upvotes: 1
Reputation:
From what I can see, the problem is you're combining your strafe code with your direction code. The two should be separate. For strafing, all you have to do is multiply your speed by a unit vector. For direction, you multiply a Vector2 (containing your cos and sin components) by the speed. Here is an example:
// Our unit vectors
namespace vec
{
Vector2f UpVec{0.0f, -1.0f};
Vector2f DownVec{0.0f, 1.0f};
Vector2f LeftVec{-1.0f, 0.0f};
Vector2f RightVec{1.0f, 0.0f};
}
if (Keyboard::isKeyPressed(Keyboard::Left))
{
player.vel = player.speed * vec::LeftVec;
}
if (Keyboard::isKeyPressed(Keyboard::Right))
{
player.vel = player.speed * vec::RightVec;
}
if (Keyboard::isKeyPressed(Keyboard::Up))
{
float angle = player.angle * M_PI / 180;
Vector2f dir{(float)cos(angle), (float)sin(angle)};
player.vel = player.speed * dir;
}
if (Keyboard::isKeyPressed(Keyboard::Down))
{
float angle = player.angle * M_PI / 180;
Vector2f dir{-(float)cos(angle), -(float)sin(angle)};
player.vel = player.speed * dir;
}
This uses cos and sin from <cmath>
so it expects the argument to be in radians. Then you just do player.position += player.velocity * deltaTime
.
Upvotes: 0