# Test for object within angle

Get help using Construct 2

### » Sat Jan 21, 2012 2:50 am

I've been struggling with this issue for awhile and I know there must be an easy answer but I can't come up with it.

I'm making a top-down space shooter enemy AI. The enemy ship can rotate in any direction. I want to be able to test whether a certain object is within a range of angles relative to the rotation of the ship.

For example if an obstacle is within 0 to 30 degrees (of the front of the ship) I can make the ship turn left (to avoid it) and if there's an obstacle within 0 to -30 degrees I can make the ship turn right.

Using the Angle expression doesn't work because it doesn't account for the rotation of the ship.

And the AngleDiff expression doesn't work because it provides an absolute value and I can't tell whether the object is to the left or right of the ship.

Here's a simple capx that I hope will demonstrate what I'm trying to accomplish.

http://dl.dropbox.com/u/28484936/rotate_issue.capx

Thanks for the help!robit_studios2012-01-21 02:51:47
B
21
S
5
G
3
Posts: 65
Reputation: 3,789

### » Sat Jan 21, 2012 4:48 am

Did that a while ago for someone else
avoidBalls.capx
Also note that the ship sprite need a little preparation.
Last edited by Yann on Sun Sep 14, 2014 10:49 am, edited 1 time in total.
B
69
S
22
G
14
Posts: 1,488
Reputation: 16,594

### » Sat Jan 21, 2012 4:49 am

this is a sh*tty fix but if you add a +90 to sprite angle so..
anglediff(Sprite.Angle, angle(Sprite.x, Sprite.y, Mouse.x, Mouse.y))

becomes:
anglediff(Sprite.Angle+90, angle(Sprite.x, Sprite.y, Mouse.x, Mouse.y))

Then you can have it so if this value is above 90, then it's on the left.. if it's below 90 it's on the right.
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

### » Sat Jan 21, 2012 5:09 pm

Yann,

Thanks. Your example is great (although, I'm not sure I am able to grasp how it works completely). But, I implemented it into my engine but I don't think it's going to work.

What I really need to do is simply be able to test whether an object's position falls within a certain angle of the front of the ship and whether it's on the right or left side of that ship.

I updated this Capx to show what I'm trying to accomplish. As you'll see it works perfectly as long as the "ship" is at it's default rotation. As soon as you rotate the ship I cannot figure out how to test position of the other object.

http://dl.dropbox.com/u/28484936/rotate_issue.capx

KeePee,

I see how your idea works but I want to try to avoid doing something like that if I can. Thanks, though!robit_studios2012-01-21 18:12:27
B
21
S
5
G
3
Posts: 65
Reputation: 3,789

### » Sat Jan 21, 2012 5:27 pm

Don't the Sprite conditions 'Is between angles', 'Is within angle' and 'Is clockwise from' help?
Scirra Founder
B
408
S
242
G
92
Posts: 24,910
Reputation: 198,648

### » Sat Jan 21, 2012 6:11 pm

I didn't think those conditions could test a sprite's position within another sprite's angle.

Don't they just compare the angles between two sprites?

If there's a way to use them to know another object's position, I'm missing how to do it.

Edit: Ah, I see my wording was a little misleading in my last post. I edited it to be a little clearer.robit_studios2012-01-21 18:13:16
B
21
S
5
G
3
Posts: 65
Reputation: 3,789

### » Sat Jan 21, 2012 6:15 pm

You can, just use something like:

Is 30 degrees within angle(Player.X, Player.Y, Target.X, Target.Y)

and

Is clockwise of angle(Player.X, Player.Y, Target.X, Target.Y)

tells you which side it's on.
Scirra Founder
B
408
S
242
G
92
Posts: 24,910
Reputation: 198,648

### » Sat Jan 21, 2012 6:53 pm

Ashley,

Brilliant! You solved my issue! I had to adjust it a little bit to account for the rotation of the "Player".

So, my code looks like this:

Is 30 degrees within Player.Angle-angle(Player.X, Player.Y, Target.X, Target.Y)

and

Is clockwise of Player.Angle-angle(Player.X, Player.Y, Target.X, Target.Y)

Once I did that, it works like a charm. I updated my capx that I was using as an example. I'll leave it up as it will hopefully help someone else who has this same question.

http://dl.dropbox.com/u/28484936/within_angle_test.capx

Thanks again!
robit_studios2012-01-21 18:54:41
B
21
S
5
G
3
Posts: 65
Reputation: 3,789