Skip to content

Instantly share code, notes, and snippets.

@hovatterz
Created January 24, 2013 21:38
Show Gist options
  • Save hovatterz/4628036 to your computer and use it in GitHub Desktop.
Save hovatterz/4628036 to your computer and use it in GitHub Desktop.
// Move a rotated sprite forward/backward
double radians = (M_PI / 180) * _player.getRotation();
float dx = std::cos(radians) * _player.getSpeed() * deltaTime;
float dy = std::sin(radians) * _player.getSpeed() * deltaTime;
Vector2f position = _player.getPosition();
if (direction == DIRECTION_FORWARD) {
position.x += dx;
position.y += dy;
} else if (direction == DIRECTION_BACKWARD) {
position.x -= dx;
position.y -= dy;
}
_player.setPosition(position);
// Face a sprite towards a point
Vector2f direction = point - _player.getPosition();
_player.setRotation(std::atan2(direction.y, direction.x) * (180 / M_PI));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment