# Compare Angle Dramas

### » Sat Dec 13, 2008 12:58 am

For an idea I'm trying out, I need to compare whether the angle between the player and the obstacle is between two other angles. For example:
Is angle between player and obstacle within obstacle's angle + 45 and obstacle's angle - 45

This sort of thing works fine and dandy if I use a perfect square at an angle of 0, but I want to use squares of varying widths and heights that also have an arbitrary angle.

Finding the angles between each corner of the square i can do, it's the comparing of the angles that messes everything up, specifically, the 180 and -180 crossover point.
Using the 'is greater than' and 'is less than' comparisons gets stuffed when the range you're comparing crosses over the 180 mark.

Any help on resolving my issue?
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Sat Dec 13, 2008 1:56 am

Using some trigonometry you can do real angle calculations, but a simplified version might just be to use the 'rotate towards...' actions in Sprite. You might be able to work out something with that...

By chance, the next build has better angle calculation features. There's a 'rotateangle' expression which rotates an angle towards an angle. In this case, if you temporarily rotate the player's angle to the obstacle's angle with a step of 45 degrees, if it then equals the obstacle angle you're within 45 degrees. Obviously you'd need to use a separate object or store the old angle, so you don't leave it changed.
Scirra Founder
B
362
S
216
G
75
Posts: 23,100
Reputation: 180,442

### » Sat Dec 13, 2008 1:59 am

That might help out, thanks.
Basically I'm trying to tell construct which side of the square my player collides with so it knows the angle at which to bounce off. The squares will be different shapes, sizes and angles so i'm trying to develop a way of finding out regardless of these factors.
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Sat Dec 13, 2008 11:02 am

Anyone got any ideas how to test to see if one angle is between two other arbitrary angles?
You'd think it would easier than this :S
stupid 180, -180 crossover....
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Sun Dec 14, 2008 4:10 am

Here's a cap of the problem is you don't quite get what i mean:
[url:3sm27nkb]http://www.mediafire.com/?zz5zzzldd1e[/url:3sm27nkb]

WSAD to resize box, Q and E to rotate, X to reset

Basically it works fine until you turn the dam thing cause the 180 to -180 crossover point thingy

Any suggestions?
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Sun Dec 14, 2008 4:24 am

Unless you want the trigonometric calculations behind this, I think you may as well wait until the next build. There's a new 'angle is clockwise of other angle' expression, so you could test if the angle is clockwise of one angle, and anticlockwise of the other. I think that'd do the trick.
Scirra Founder
B
362
S
216
G
75
Posts: 23,100
Reputation: 180,442

### » Sun Dec 14, 2008 5:28 am

Those new angle things sound awesome i can't wait
For curiousity's sake i would like to know the trig behind it, i'll most likely just dump it later for the new angle conditions, unless it's too much effort, in which case i don't mind waiting till the next build
sweet
B
3
S
2
G
5
Posts: 351
Reputation: 2,377

### » Sun Dec 14, 2008 12:45 pm

Here's the function, hope it's clear enough:

[code:15kakc7c]// start angle, end angle, step
float RotateTowards(float start, float end, float step)
{
if (step == 0) return start;

float cs = cos(start);
float ce = cos(end);
float ss = sin(start);
float se = sin(end);

float z = cs*se - ss*ce; // for determining clockwise/anticlockwise
float dot = cs*ce + ss*se;
float angle = acos(dot); // angle difference between start and end

if(angle > step)
if(z > 0) // is clockwise
return start + step;
else // is anticlockwise
return start - step;
else
return end; // angle difference is less than step - finish on end angle
}[/code:15kakc7c]
Scirra Founder
B
362
S
216
G
75
Posts: 23,100
Reputation: 180,442