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

Author Topic: Help with proper 360 sprite rotation on keypress... [SOLVED]  (Read 1983 times)

0 Members and 1 Guest are viewing this topic.

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Help with proper 360 sprite rotation on keypress... [SOLVED]
« on: September 17, 2013, 12:10:14 pm »
Remember the first/second Grand Theft Auto's? A top down car game with 360* rotation is what im trying to achieve.

Currently my sprite changes direction with .setRotation, but i'd like to implement something fancier(ie: that rotates to that degree rather than jumping straight to it).

I currently have movement setup like:

Quote
if key = w //forward
    if key = d //right
    playSprite.setRotation(315);  //face north east
    if key = a //left
   playSprite.setRotation(225); //face north west
else
   playSprite.setRotation(270); //face north
end if

if key = s //down
    if key = d //right
    playSprite.setRotation(45);  //face south east
    if key = a //left
   playSprite.setRotation(135); //face south west
else
   playSprite.setRotation(360); //face south
end if

So if I were to do something like:

Quote
if key = w && key = d   //north east
   if(playSprite.getRotation > 315)
   playsprite.rotate(100*dt);
   end if

I run into the probably obvious problem of if your facing north west and you want to now face north east your going to rotate on a clockwise angle to the other side while facing the opposite direction to what you wanted.

So im *guessing* I should call a function that finds the shortest distance between the degrees whether it be clockwise or anticlockwise and then apply that?






   
« Last Edit: September 17, 2013, 12:52:16 pm by HailBee »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Need help with math or tips on proper 360 sprite rotation on keypress...
« Reply #1 on: September 17, 2013, 12:21:04 pm »
I don't think a function like that exists, but it's so simple you can just write it yourself.  It might look something like this:

angle_dist(int d1, int d2) {
    if(abs(d2 - d1) > 180)
        if(d2 > d1)
            return (360-d2) + d1;
        else
            return (360-d1) + d2;
    else
        return abs(d2 - d1)
}
(I'll leave the signs for you to figure out)

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Need help with math or tips on proper 360 sprite rotation on keypress...
« Reply #2 on: September 17, 2013, 12:28:17 pm »
I don't think a function like that exists, but it's so simple you can just write it yourself.  It might look something like this:

angle_dist(int d1, int d2) {
    if(abs(d2 - d1) > 180)
        if(d2 > d1)
            return (360-d2) + d1;
        else
            return (360-d1) + d2;
    else
        return abs(d2 - d1)
}
(I'll leave the signs for you to figure out)

Thanks I was going to write something similar but wanted to make sure thats how it's normally done in game development(as in for top down rpg's or shooters with 360* rotation).

Edit: Wooo got it!!, even with your help that felt like a minor win hehe :D

int Player::findRotation(int rotation)
{
   int currentRot = playSprite.getRotation();
   int desiredRot = rotation;
   if((desiredRot - currentRot) > 180)
   {
      return -rotation;
   }
}
« Last Edit: September 17, 2013, 12:50:48 pm by HailBee »