Shortest rotation angle?

Get help using Construct 2

Post » Tue Apr 16, 2013 9:26 am

If I have an object facing in a direction that needs to turn to match the angle of another object, is there a formula to determine the shortest type of rotation (clockwise vs. counterclockwise)?

This is NOT the same thing as determining the shortest way to turn to FACE another object, which is the formula I ended up creating. I need my object to MATCH the angle of another object.

If you're curious about the facing formula, here it is:

[QUOTE]If
(Player.X - cos(Target.Angle)) * sin(Player.Angle) > (Player.Y - sin(Target.Angle)) * cos(Player.Angle)

...then Clockwise is the shortest.
Otherwise, counterclockwise.[/QUOTE]

For those who don't understand what the above formula does, it basically draws a line behind the player and then determines which side of that line the target is on. However, the above formula made sense to me because I am able to think of it in cartesian coordinates and not polar ones.

I'm trying to think of a way to determine the shortest way to match the angle of another object, and I can't seem to find a cartesian way to think of it.

EDIT: Current line of thinking is:

[QUOTE]abs() = absolute value
If
abs(Player.Angle - Target.Angle) > abs(Player.Angle + 180 - Target.Angle)

... then Counterclockwise
[/QUOTE]

However, this fails if Target.Angle = 0, so this can't be it, even though it seems to work for a lot of cases.Excal2013-04-16 11:24:42
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Tue Apr 16, 2013 10:43 am

if abs(Target.Angle-Player.Angle)>180   .... CCW
else                                    .... CW

i think this could work
B
20
S
8
G
6
Posts: 176
Reputation: 8,047

Post » Tue Apr 16, 2013 10:46 am

If Player.Angle = 10 and Target.Angle = 0, then the absolute value is 10, and by your formula the shortest direction would be clockwise.

However, that is incorrect because clockwise means turning 350 degrees.

Excal2013-04-16 10:51:38
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Tue Apr 16, 2013 11:32 am

[code]Every Tick
   -> Sprite: Rotate ROTATION_SPEED * dt degrees toward target.Angle[/code]

isn't enough for what you need? (:
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Tue Apr 16, 2013 11:37 am

Did you even read the first post? I know how to rotate an object.

However, there's a difference between rotating 10 degrees counterclockwise and 350 degrees clockwise. That's what I'm trying to solve.
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Tue Apr 16, 2013 12:17 pm

In mathematics degrees increased in CCW. But in Construct 2 degrees increased in CW. I wrote the formula thinking in general mathematical way. Simply you can change it like this.

if abs(Target.Angle-Player.Angle)>180   .... CW
else   ... CCW
B
20
S
8
G
6
Posts: 176
Reputation: 8,047

Post » Tue Apr 16, 2013 12:23 pm

If Target.Angle = 350 and Player.Angle = 10, your formula suggests going clockwise, or 340 degrees. The correct answer is to go counterclockwise 20 degrees.
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,194

Post » Tue Apr 16, 2013 1:03 pm

Sorry if I offended you with my answer Excal, but I tend to try to understand why the question was asked and come up with something simple just in case.

Here is my answer to the question "is there a formula to determine the shortest type of rotation (clockwise vs. counterclockwise)?"
rotationType.capx

There might be a complex formula for that, but I would do it this way. It's simple to understand and debug. And the function themselves could be used for other stuff in your project.
(also sine and cosine is known to be a slow operation)
Last edited by Yann on Sun Sep 14, 2014 7:24 pm, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Tue Apr 16, 2013 6:35 pm

[CODE]if (360 + Target.Angle - Player.Angle) % 360 > 180   .... CCW
else                                                 .... CW[/CODE]

Should work for any valid angles.
B
46
S
17
G
18
Posts: 847
Reputation: 13,802

Post » Tue Apr 16, 2013 9:06 pm

Angle(x1,y1,x2,y2) will return either positive or negative 0-180 depending on the order of objects x, and y's used.
So basically it doesn't care about the shortest route, just the order.
I guess in theory you could use that as well to get the shortest angle by testing + or -.
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Next

Return to How do I....?

Who is online

Users browsing this forum: djcrazypants, marekmk, Yahoo [Bot] and 6 guests