Test for object within angle

Forum Home Forum Home > Construct 2 General > How do I....?
 Post Reply Post Reply
Author
2,450 Rep
Post Options Post Options   Quote robit_studios Quote  Post ReplyReply Direct Link To This Post Topic: Test for object within angle
    Posted: 21 Jan 2012 at 2:50am
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!

Edited by robit_studios - 21 Jan 2012 at 2:51am
Back to Top
11,785 Rep
Post Options Post Options   Quote Yann Quote  Post ReplyReply Direct Link To This Post Posted: 21 Jan 2012 at 4:48am
Did that a while ago for someone else
http://dl.dropbox.com/u/23551572/C2/avoidBalls.capx
Might answer your question
Also note that the ship sprite need a little preparation.
Back to Top
2,394 Rep
Post Options Post Options   Quote keepee Quote  Post ReplyReply Direct Link To This Post Posted: 21 Jan 2012 at 4:49am
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.
Back to Top
2,450 Rep
Post Options Post Options   Quote robit_studios Quote  Post ReplyReply Direct Link To This Post Posted: 21 Jan 2012 at 5:09pm
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!

Edited by robit_studios - 21 Jan 2012 at 6:12pm
Back to Top

Scirra Developer
78,427 Rep
Post Options Post Options   Quote Ashley Quote  Post ReplyReply Direct Link To This Post Posted: 21 Jan 2012 at 5:27pm
Don't the Sprite conditions 'Is between angles', 'Is within angle' and 'Is clockwise from' help?
Back to Top
2,450 Rep
Post Options Post Options   Quote robit_studios Quote  Post ReplyReply Direct Link To This Post Posted: 21 Jan 2012 at 6:11pm
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.

Edited by robit_studios - 21 Jan 2012 at 6:13pm
Back to Top

Scirra Developer
78,427 Rep
Post Options Post Options   Quote Ashley Quote  Post ReplyReply Direct Link To This Post Posted: 21 Jan 2012 at 6:15pm
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.
Back to Top
2,450 Rep
Post Options Post Options   Quote robit_studios Quote  Post ReplyReply Direct Link To This Post Posted: 21 Jan 2012 at 6:53pm
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!


Edited by robit_studios - 21 Jan 2012 at 6:54pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down