• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [Solved] Need help with top-down rotation of character.

S

sTRACH

Guest
Hello,

I have a top-down character that consists of two objects - obj_head and obj_body.
obj_head is set to always face the mouse.
With obj_body, I want it to follow obj_head if obj_head's angle is greather or less than 45 degrees.

This code though there is a problem with it, worked the best so far:
Code:
if((360 - (image_angle - obj_head.image_angle)) % 360 > 45 and (360 - (image_angle - obj_head.image_angle)) % 360 < 315){
   if(image_angle - obj_head.image_angle > 0){
           image_angle = (ceil(obj_head.image_angle) + 45) % 360;
   }
   if(image_angle - obj_head.image_angle < 0){
           image_angle = (ceil(obj_head.image_angle) - 45) % 360;
   }
}
image_angle == obj_body.image_angle

The outside if is supposed to check if the angle is > or < 45 degrees and it works fine.

When image_angle is 45(ish) and obj_head is at 359(ish) degrees, this code changes image_angle the opposite way. The same happens when image_angle is 315(ish) and obj_head.image_angle is 1(ish).


Can you please take a look at the code and help me out?
Thank you for your answers in advance.
 

Attachments

NightFrost

Member
Check angle_difference which will give you the smallest difference between two given angles, returning a value between -180 and 180.
 
S

sTRACH

Guest
Ah, exactly what I needed. Thank you very much :)

If anyone needs to do a similar thing in the future, here's my working code:

Code:
if(abs(angle_difference(image_angle, obj_head.image_angle)) > 45){
    if(angle_difference(image_angle, obj_head.image_angle) < 45){
        image_angle -= angle_difference(image_angle, obj_head.image_angle) + 45;
    }
    else if(angle_difference(image_angle, obj_head.image_angle) > 45){
        image_angle -= angle_difference(image_angle, obj_head.image_angle) - 45;
    }
    image_angle = image_angle % 360;
}
 
Top