0 Favourites

Test for object within angle

  • 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.

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

    Thanks for the help!

  • Did that a while ago for someone else

    avoidBalls.capx

    Might answer your question

    Also note that the ship sprite need a little preparation.

  • 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.

  • 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.

    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!

  • Don't the Sprite conditions 'Is between angles', 'Is within angle' and 'Is clockwise from' help?

  • 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.

  • 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.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)