SFML community forums
Help => Graphics => Topic started by: mapPhil on May 31, 2011, 08:12:50 pm
-
Hi
I am creating an asteroids game and I need to move the ships forwards and backwards based on its current angle. If someone could help me it would be greatly appreciated.
I have read that you can calculate the below, but can't seem to get it to work in c++
x = speed * cos(current_angle);
y = speed * sin(current_angle);
-
I have read that you can calculate the below, but can't seem to get it to work in c++
Please supply us with a meaningful problem description, so that we can help you ;)
Note that the functions std::sin() and std::cos() take radians, not degrees.
-
This is the code I used
//Convert angle to radians
angleRADS = (3.1415926536/180)*(sprPlayer.GetRotation());
//Set x and y
forx = 0.01f*cos(angleRADS);
fory = 0.01f*sin(angleRADS);
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right))
{
sprPlayer.Rotate(-0.03f);
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
{
sprPlayer.Rotate(0.03f);
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
{
sprPlayer.Move(forx, fory);
}
The problem is the code above doesn't work, when i rotate the sprite, it doesn't in the direction its meant to.
I'm new to this so any guidance, on what is wrong with the above code will be helpful.
-
Is it rotating in the opposite direction of what you expect?
-
+1 oniLink
"angle 0" points directly to the right and rotates counter-clockwise (when positive Y is upwards).
Although in SFML I think positive Y is downwards, so it should rotate clockwise.
Anyway... first thing I would do is make sure you're image is facing to the right and not upwards like you might think.
-
So the rotation works perfectly using the code above
but when I rotate the sprite and press the "UP" key it doesn't move forwards in the way it is meant to. I'm trying to create an asteroids game, and this is for the ship.
-
try negative sine
-
That did it, I feel like an idiot..Thanks
-
Everything works fine when I changed it to -sin
but how come it needs to be -sin I always though that the velocity is calculated for y using: velocity_y = sin(angle) * speed?
is there something I have done wrong for it to have to be -sin?
-
I think it's the same problem as in this (http://www.sfml-dev.org/forum/viewtopic.php?t=4021) thread, the origin in math is in the bottom left, in SFML it's the upper left, so everything has to be mirrored.
-
Ok thats great, thank you