Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Moving forwards/backwards based on angle  (Read 10265 times)

0 Members and 4 Guests are viewing this topic.

mapPhil

  • Newbie
  • *
  • Posts: 11
    • View Profile
Moving forwards/backwards based on angle
« 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);

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Moving forwards/backwards based on angle
« Reply #1 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mapPhil

  • Newbie
  • *
  • Posts: 11
    • View Profile
Moving forwards/backwards based on angle
« Reply #2 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.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Moving forwards/backwards based on angle
« Reply #3 on: June 01, 2011, 12:56:06 am »
Is it rotating in the opposite direction of what you expect?
I use the latest build of SFML2

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Moving forwards/backwards based on angle
« Reply #4 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.

mapPhil

  • Newbie
  • *
  • Posts: 11
    • View Profile
Moving forwards/backwards based on angle
« Reply #5 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.

WitchD0ctor

  • Full Member
  • ***
  • Posts: 100
    • View Profile
    • http://www.teleforce-blogspot.com
Moving forwards/backwards based on angle
« Reply #6 on: June 01, 2011, 01:32:49 pm »
try negative sine
John Carmack can Divide by zer0.

mapPhil

  • Newbie
  • *
  • Posts: 11
    • View Profile
Moving forwards/backwards based on angle
« Reply #7 on: June 01, 2011, 01:36:41 pm »
That did it, I feel like an idiot..Thanks

mapPhil

  • Newbie
  • *
  • Posts: 11
    • View Profile
Moving forwards/backwards based on angle
« Reply #8 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?

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Moving forwards/backwards based on angle
« Reply #9 on: June 02, 2011, 11:38:11 pm »
I think it's the same problem as in this thread, the origin in math is in the bottom left, in SFML it's the upper left, so everything has to be mirrored.

mapPhil

  • Newbie
  • *
  • Posts: 11
    • View Profile
Moving forwards/backwards based on angle
« Reply #10 on: June 02, 2011, 11:45:50 pm »
Ok thats great, thank you

 

anything