• 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!

Legacy GM Need help with turning object.

S

Stillwel

Guest
Ei... im trying to make a game where if the object wants to move it have to face towards the target first before they are allowed to accelerate. im using this line of code



its works fine until scenarios where the unit is facing 0 degree and the target is 270 degree. it should just need to turn counter clockwise it`s direction to reach the targeted direction. but instead what happens is that the unit circles and turn clockwise so it basically circles around. is there away to fix this?
 

YoSniper

Member
You will have to check the direction of the target both in relation to direction and (direction + 180). This is the inherent problem with the domain of direction being restricted to [0, 360).

Code:
var target_dir = point_direction(x, y, target_x, target_y);
if abs(target_dir - dir) < TOLERANCE or abs(target_dir - dir) > 360 - TOLERANCE {
    direciton = target_dir;
    //Pursue target
} else if direction < 90 {
    if target_dir > direction + 180 or target_dir < direction {
        //turn right
    } else {
        //turn left
    }
} else if direction > 270 {
    if target_dir > direction or target_dir < direction - 180 {
        //turn left
    } else {
        //turn right
    {
} else {
    if target_dir > direction {
        //turn left
    } else {
        //turn right
    }
}
This was as simple as I could get. For the record, I recommend setting state variables in code like this, and then handling the direction changing outside of those blocks. That way, you don't have a whole bunch of code repeated in many different blocks (which can be hard to debug.)
 

FrostyCat

Redemption Seeker
Don't use direct comparisons for directions like this, they come apart too easily when the 0-360 divide is involved. Use angle_difference() and take advantage of the returned value's sign to determine which way to turn. See this and this as examples.

Also, don't post code in screenshot form. Use [code] and [/code] tags.
 
Top