SFML community forums

Help => Graphics => Topic started by: mapPhil on May 31, 2011, 08:12:50 pm

Title: Moving forwards/backwards based on angle
Post 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);
Title: Re: Moving forwards/backwards based on angle
Post by: Nexus on May 31, 2011, 08:14:42 pm
Quote from: "mapPhil"
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.
Title: Moving forwards/backwards based on angle
Post by: mapPhil on May 31, 2011, 08:45:03 pm
This is the code I used

Code: [Select]
//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.
Title: Moving forwards/backwards based on angle
Post by: OniLinkPlus on June 01, 2011, 12:56:06 am
Is it rotating in the opposite direction of what you expect?
Title: Moving forwards/backwards based on angle
Post by: Disch on June 01, 2011, 04:38:32 am
+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.
Title: Moving forwards/backwards based on angle
Post by: mapPhil on June 01, 2011, 11:26:20 am
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.
Title: Moving forwards/backwards based on angle
Post by: WitchD0ctor on June 01, 2011, 01:32:49 pm
try negative sine
Title: Moving forwards/backwards based on angle
Post by: mapPhil on June 01, 2011, 01:36:41 pm
That did it, I feel like an idiot..Thanks
Title: Moving forwards/backwards based on angle
Post by: mapPhil on June 02, 2011, 10:51:11 pm
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?
Title: Moving forwards/backwards based on angle
Post by: Lupinius on June 02, 2011, 11:38:11 pm
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.
Title: Moving forwards/backwards based on angle
Post by: mapPhil on June 02, 2011, 11:45:50 pm
Ok thats great, thank you