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

Author Topic: Rotating object using analog stick on a game-pad.  (Read 2052 times)

0 Members and 1 Guest are viewing this topic.

bumblecorn

  • Newbie
  • *
  • Posts: 18
    • View Profile
Rotating object using analog stick on a game-pad.
« on: January 24, 2013, 05:04:59 pm »
I'm trying to make an Asteroids type game where you use the left stick to move around and the right stick to rotate and shoot in different directions.
I've got the left stick sorted out, but I can't figure out how to code the right stick for rotation. I want the object to be faced the same direction as the analog stick is pushed i.e. if I'm pushing the stick straight down, the rotation of the object would be set to 180 etc. The coordinates returned by the stick consist of two values, the X and Y axes, but rotation works in degrees which is one parameter, so I can't really use "setRotation" or "rotate"

I've created a very simple alternative that basically just uses the horizontal axis on the stick to add to the rotation:

if (rightStickX > 20)
{
        player.rotate(rightStickX / 99);

}

if (rightStickX < - 20)
{
        player.rotate(rightStickX / 99);

}

Can anyone set me on the right track here, or is this too complicated for a beginner? Or maybe I'm over-thinking this and it's really simple to do?

P.S. I'm using SFML 2.0 -RC
« Last Edit: January 24, 2013, 05:06:56 pm by bumblecorn »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Rotating object using analog stick on a game-pad.
« Reply #1 on: January 24, 2013, 05:41:06 pm »
Use the std::atan2 function to get an angle from X and Y coordinates.
Laurent Gomila - SFML developer

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Rotating object using analog stick on a game-pad.
« Reply #2 on: January 24, 2013, 05:54:01 pm »
On a more elaborate note you could use polar vectors to power rotations of all types. Polar vectors use std::atan2 and other math to give you more control over what you want to do.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

bumblecorn

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Rotating object using analog stick on a game-pad.
« Reply #3 on: January 24, 2013, 06:45:15 pm »
Thanks Laurent, got it working in 2 minutes. Masskiller, I'll keep that in mind, cheers.

 

anything