[SOLVED] Calculating angle between 2 directions from actor

froz

  • Posts: 250
Hello,

First of all, I'm new to Stencyl, but I have to say that I love it. It's so easy to understand, so easy to experiment with it, really great program.

I have a problem with some trygonometry though. Let me say what I have and what I want to get.

I have 2 angles and I want to determine the shorter angle that is between them. Or let me say that in other words - I have 2 directions from the actor and I want to know wheter first direction is closer clockwise or counterclockwise to the second direction.

For example direction A is 170 degrees, directon B is -170 degrees. The shorter angle between them is 20 degrees, counterclockwise from A. How can I calculate it?

Thanks for any help.

« Last Edit: June 01, 2012, 03:12:41 am by froz »

Ryusui

  • Posts: 827
Use absolute value (so "negative" angles compare as positive) and either the "larger/smaller of" blocks or the "greater than/less than" booleans.
In the event of a firestorm, the salad bar will remain open.

Sunflower

  • Posts: 591
Make a number attribute "dAngle" and boolean "Clockwise?", and then insert something like that:
Code: [Select]
set Clockwise? to <false>
set dAngle to [Angle A - Angle B]
if <dAngle < 0>
  set dAngle to [negate [dAngle]]
  set Clockwise? to <not <Clockwise?>>
if <dAngle > 180>    //another if, not "otherwise if"!
  set dAngle to [360 - dAngle]
  set Clockwise? to <not <Clockwise?>>
Thanks to these blocks dAngle should tell you what angle is between A and B, and which way should you go from one angle to another.

My Little Analysis tells me that it should work. If not, please let me know. If it does, also let me know. ;)

froz

  • Posts: 250
Thank you so much. It is exactly what I needed and it seems to be working well. I'm not 100% sure because I have to fix something else first to check it live in my game.
I was thinking that some ifs could be the way to do it, but I couldn't figure out such an elegant solution and I didn't want to create 10 ifs just for that. Thanks again.

t4u

  • Posts: 418
Make a number attribute "dAngle" and boolean "Clockwise?", and then insert something like that:
Code: [Select]
set Clockwise? to <false>
set dAngle to [Angle A - Angle B]
if <dAngle < 0>
  set dAngle to [negate [dAngle]]
  set Clockwise? to <not <Clockwise?>>
if <dAngle > 180>    //another if, not "otherwise if"!
  set dAngle to [360 - dAngle]
  set Clockwise? to <not <Clockwise?>>
Thanks to these blocks dAngle should tell you what angle is between A and B, and which way should you go from one angle to another.

My Little Analysis tells me that it should work. If not, please let me know. If it does, also let me know. ;)

Whole day of work only to see it solved in 8 lines my mind was crushed.
USE PICTURES WHEN YOU ASK SOMETHING!
If I helped you be sure to mention it in your game.

Tutorials + downloads:
http://t4upl.blogspot.com/

t4u

  • Posts: 418
Make a number attribute "dAngle" and boolean "Clockwise?", and then insert something like that:
Code: [Select]
set Clockwise? to <false>
set dAngle to [Angle A - Angle B]
if <dAngle < 0>
  set dAngle to [negate [dAngle]]
  set Clockwise? to <not <Clockwise?>>
if <dAngle > 180>    //another if, not "otherwise if"!
  set dAngle to [360 - dAngle]
  set Clockwise? to <not <Clockwise?>>
Thanks to these blocks dAngle should tell you what angle is between A and B, and which way should you go from one angle to another.

My Little Analysis tells me that it should work. If not, please let me know. If it does, also let me know. ;)

Doesn't work afterall check for A= -140 B =120.

set Clockwise? to <false> (Clockwise?=false)
set dAngle to [-140 - 120]    ( = -260)


if <dAngle < 0> (It is -260 < 0)
  set dAngle to [negate [dAngle]]   (260)
  set Clockwise? to <not <Clockwise?>>   ((Clockwise?=true))

if <dAngle > 180>    //another if, not "otherwise if"! (It is 260>180 )
  set dAngle to [360 - dAngle]    (360 - 260 = 100)
  set Clockwise? to <not <Clockwise?>>[/code] ((Clockwise?=false))

and it should be clockwise?=true.
dAngle is as it should be.
USE PICTURES WHEN YOU ASK SOMETHING!
If I helped you be sure to mention it in your game.

Tutorials + downloads:
http://t4upl.blogspot.com/