Game Maker Community: 360 Degree Rotation Problem *solved* - Game Maker Community

Jump to content

Advanced Users Forum Rules

This is a Q&A forum for advanced GML users ONLY. Moderators will remove inappropriate topics. Make sure that you READ these rules prior to posting: Advanced Users Forum Rules
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

360 Degree Rotation Problem *solved* how do i make 'abs(350-10)<90'?

#1 User is offline   hasser 

  • GMC Member
  • Group: GMC Member
  • Posts: 537
  • Joined: 03-December 06

Posted 21 January 2007 - 08:38 PM

lets say i have a clock with 2 handles and i want that whenever the handle comes within 90 degrees of another the alarm goes off.

if abs(handle0dir-handle1dir)<90 {
alarmgoesoff()
}

this works...to a certian extent... but what if one handle is at 350 degrees and another at 10, they should go off but they dont...

any way to slove this (preferably speed optimized)?

This post has been edited by hasser: 21 January 2007 - 09:20 PM

0

#2 User is offline   xot 

  • media multimixer
  • Group: Global Moderators
  • Posts: 3472
  • Joined: 07-May 04

Posted 21 January 2007 - 09:12 PM

/*
**  usage:
**      diff = angle_difference(angle1,angle2);
**
**  given:
**      angle1    first direction in degrees, real
**      angle2    second direction in degrees, real
**
**  returns:
**      difference of the given angles in degrees, -180 to 180
**
**  www.planetxot.com
*/
return ((((argument0 - argument1) mod 360) + 540) mod 360) - 180;


if (abs(angle_difference(handle0dir,handle1dir)
) < 90) {
    alarmgoesoff()
}

This post has been edited by xot: 21 January 2007 - 09:12 PM


#3 User is offline   hasser 

  • GMC Member
  • Group: GMC Member
  • Posts: 537
  • Joined: 03-December 06

Posted 21 January 2007 - 09:19 PM

great thanks although i dont understand it ^^ it seems to work <_<
0

#4 User is offline   Qjet 

  • GMC Member
  • Group: GMC Member
  • Posts: 260
  • Joined: 12-December 05

Posted 21 January 2007 - 09:22 PM

Hasser the problem you are having is due to the fact that degrees dont follow conventional math, 360 = 0! WTH!

also 350 and 10 degrees are 20 degrees apart, your clock should NOT go off.
300 - 30 is 90 degrees apart.

im reviewing xots solution however i doubt it works for all situations (almost positive) because there arnt two ways to return information, there are TWO diffrences between every degree.

the diffrence between degree 0 and degree 90 is 90 and 270. theres the shortest angle and the longest angle (a reflex angle).

this is where your problem is coming from.
I have some psudocode somewhere on my computer, but i cant seem to find it friend. But ill find and post it later.

edit: OH WAIT, i see what xot's doing. nevermind that will work.

This post has been edited by Qjet: 21 January 2007 - 09:24 PM

0

#5 User is offline   Sinaz 

  • MCP Killer
  • Group: Local Moderators
  • Posts: 2241
  • Joined: 22-September 05

Posted 22 January 2007 - 05:21 PM

xot, on Jan 21 2007, 01:15 PM, said:

/*
**  usage:
**      diff = angle_difference(angle1,angle2);
**
**  given:
**      angle1    first direction in degrees, real
**      angle2    second direction in degrees, real
**
**  returns:
**      difference of the given angles in degrees, -180 to 180
**
**  www.planetxot.com
*/
return ((((argument0 - argument1) mod 360) + 540) mod 360) - 180;


if (abs(angle_difference(handle0dir,handle1dir)
) < 90) {
    alarmgoesoff()
}

<{POST_SNAPBACK}>


I have this code snippet added to every game I make... I inevitably need to use it at some point or another... this script should be made into a function!

#6 User is offline   EyeGuy 

  • GMC Member
  • Group: GMC Member
  • Posts: 832
  • Joined: 01-May 04

Posted 23 January 2007 - 12:55 AM

I was just thinking the same thing. It's used in everything from smooth camera rotation to any form of limited AI turning.

Anyway, all the function does is takes the value and shifts it to the (-180,180) degree range.

The way it works is it first heards the number into the a finite range (-360,360) by using the modulus function. Then by adding 360, it make sure all numbers are positive. By adding another 180, it shifts the value in preperation for the last step. What this step does is it first groups the numbers back to the (0,360) degree range and then shifts the numbers back into their normal positions, but now the range is (-180,180) degrees.

Incidentally, GM's weird handelling of the modulus operation (allowing for negative values) makes this function a good deal less intuitive and slightly more long-winded. In C++, this function would look like:
return ((angle2 - angle1 + 180) % 360) - 180;

But it's hard to complain since GM's mod allows for decimal positions.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users