# Interpolated Rotation/direction?

3 replies to this topic

GMC Member

• New Member
• 30 posts

Posted 26 January 2007 - 04:44 PM

Does anyone know the 'best practice/most efficient' way to interpolate rotation on a GM object?

Assume a top-down view of a tank with a current turret direction of 15 (when I use the word 'direction' I am specifically referring to how GM uses this word in GML).

Assume that tank has recieved 'orders' to move its turret's to a new direction of 190.

How can I have turret rotate smoothly from one direction (15) to the new target direction (190)? I can't just increment or decrement blindly, because that won't always result in the turret rotating the right way (ie, the shortest way) to achieve it's new direction.

Just to clarify, I'm not looking for 'always point at the mouse code'. I hope the above is clear, I hope I'm describing the issue clearly enough - if I am not, please let me know.

I would prefer 'Code Example + Explanation' over 'here is a .gm6 file' (as I am at work and cannot run GameMaker here)... But as I am the one asking, I'll take whatever I can get.

I am looking for the 'best practice' or 'most efficient' way to do this (efficiency is important for this program), but again, I'll take and appreciate any suggestions anyone can offer.
• 0

### #2 Abyssal_Nuclei

Abyssal_Nuclei

GMC Member

• GMC Member
• 1695 posts

Posted 26 January 2007 - 04:49 PM

Maybe this topic would be of help?

http://forums.gamema...howtopic=270374

specificly

```//direction_to(new_direction,turn_rate)
//Change the direction towards the desired direction
//but not more than turn_rate degrees
var t;
t = ((((argument0 - direction) mod 360) + 540) mod 360) - 180

if (abs(t) < argument1) {
direction = argument0
} else {
direction += argument1*sign(t)
}```

The above code is basically for a homing missile or homing enemy to know what direction to turn to start to face the target.
t will contain a value of how far you need to turn and whether it will be a positive (counterclockwise) or negative (clockwise) turn. The code after it will automatically turn the object for you - which you may modify, remove, whatever, if you don't like it that way.

<{POST_SNAPBACK}>

~Abyss
• 0

### #3 ragarnak

ragarnak

GMC Member

• Retired Staff
• 19468 posts
• Version:GM8

Posted 26 January 2007 - 04:57 PM

Maybe this topic would be of help?

<{POST_SNAPBACK}>

Even shorter :

//argument0 - direction to turn to
//argument1 - max step-angle

direction += max(-argument1,min(+argument1, ((((argument0 - direction) mod 360) + 540) mod 360) - 180 ))

• 0

GMC Member

• New Member
• 30 posts

Posted 26 January 2007 - 05:00 PM

Thanks Abyssal_Nuclei! That code wasn't *exactly* what I was looking for, BUT it did lead me to this after following your link and doing some reading:

http://forums.gamema...4

```// angle_dist(ang1,ang2);
return ((((argument0 - argument1) mod 360) + 540) mod 360) - 180;```

Which I belive is exactly what I'm looking for.

I searched alot for this before posting this topic, but I just couldn't find it.

Thanks again!

Edited by Glenshadow, 26 January 2007 - 05:02 PM.

• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users