How do I compare the angle between two objects?

0 favourites
  • 4 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Hi,

    I want to apply seperate actions for different intervalls of the angle between player and enemy; 4 quadrants for the total circle of 360 degrees.

    In the code I have used the condition "is between two values" for each quadrant and written "angle(Sprite1.X,Sprite1.Y,Sprite2.X,Sprite2.Y)".

    The angle values I have set to the following:

    Quadrant 1: first angle 315, second angle 45

    Quadrant 2: first angle 45, second angle 135

    Quadrant 3: first angle 135, second angle 225

    Quadrant 4: first angle 225, second angle 315

    However, this does not yield satisfying results, the desired actions will not take place when the angle between player and enemy is within these angle intervals. Maybe the first and second angles I have assigned are the wrong values?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You should subtract one from the second angle of each condition. This is because the range of one ends at the same angle that another one starts at, so both actions will trigger at the same time.

    Quadrant 1: first angle 315, second angle 44 <-

    Quadrant 2: first angle 45, second angle 134 <-

    Quadrant 3: first angle 135, second angle 224 <-

    Quadrant 4: first angle 225, second angle 314 <-

    Also make sure you put the objects in the right order in the angle() expression. In your example its using Sprite1 as the center point rotating towards Sprite2. If you intend for Sprite2 to be the center point then the angles you are getting from "angle(Sprite1.X,Sprite1.Y,Sprite2.X,Sprite2.Y)" are going to be inverted, and it should instead be "angle(Sprite2.2,Sprite2.Y,Sprite1.X,Sprite1.Y)". :)

  • A few thoughts:

    * there are conditions to compare angles. I’d recommend using them instead of the “value is between values” condition because they handle how angles can wrap around from 360 to 0.

    Another way is to take the angle and calculate a variable for the quadrant number. 1 to 4.

    Quadrant = (round(angle(sprite1.x, sprite1.y, sprite2.x, sprite2.y)/90)+4)%4+1

  • Thanks for the inputs BaconSwagg and R0J0hound, I feel I know how to handle this problem now.

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