angle() function question

0 favourites
  • Hello,

    I would like to know why angle() function goes into negatives after 180°. I would like it to go to 360° because my algorithm based on angle detected between an object and touch input fails for:

    If value angle(x1,y1,x2,y2) is between values 135°&225° --->do something. it fails after 180°.

    It also fails if i do: 135°&-135°

    I figured this out with another way but again I have the feeling that this function is broken.

    Please explain me.

    Plus the angle values are invetrted compared to the normal angle behavior in construct. (I've included an image)

  • 180, and -180 are the same, just as 0, and 360 are the same, as far as the circle goes.

    360- 180= 180

    360 + negative 180= ?

    Think of it as two semicircles.

  • It's working correctly, it gives the angle in the range from -180 to 180 because of the math behind it.

    If you want it in the range of 0 to 360 then you can convert it with (a+360)%360.

    I don't have the second issue. Are you putting the coordinates in the correct order? It finds the angle from point 1 to point 2.

  • How would you decide the correct order?

    Thus far I've just gone with the same order as they are put into conditions.

  • newt I know that. But construct somehow didn't.

    I used this logic and it didn't work. Past the 180° C2 was considering it's over the angle limit (maybe with the final else event but i don't see the point)

    So basically once the angle did not match the width of the DragLine was returned to 0. And it happened at 180° instead of 225°. Because the angle started to decrementing. And i proved it with putting from 135to (-135) as the angle condition. The line even failed to appear so it has a problem with negative angles. Something is broken. I would like to know what.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I resolved this issue with this logic but in my case the angle() didn't work properly. I would like to know why.

  • I don't have the second issue. Are you putting the coordinates in the correct order? It finds the angle from point 1 to point 2.

    I tried both. angle(x1,y1,x2,y2) and angle (x2,y2,x1,y1). Same problem.

  • But yeah I could simply covert the 180° ranged angles to a 360° and this will probably work. I was just surprised that C2 does not recognise -135° as 225°

  • System compare isn't the best way to go. Ideally you would do all the comparisons in the actions, rather than the conditions.

    Maybe you could say what you are trying to do?

  • just to draw a line from a start.X&start.Y to a perpendicular direction depending of the relative touch.x&touch.y position.

    But it's okay i resolved the stuff. Would be cool if angle did automatically convert to a 360° based value automatically. I don't see the point of decrementing after 180°.

    Plus the angles are inverted compared to the normal construct angles logic. with angle() 0 is where 180 normally is.

    Next time i will just write my own angle() function that adds 360 in case that that you have a negative.

  • Just do a "is in touch"

    sprite set angle to angle(self.x,self.y,touch.x,touch.y)

    With the hotspot set to the 0 x.

  • Just do a "is in touch"

    sprite set angle to angle(self.x,self.y,touch.x,touch.y)

    With the hotspot set to the 0 x.

    Yeah well in this case the angle of the sprite will go in all 360°, i needed only 90° based directions.

    See

    http:(erase this space)//bricklayer-milks-47242.bitballoon.com/

  • I think, for this instance, you would be better off determining if the "snapped" touch xy is greater than, less than etc of the initial touch position.

    The angle serves no purpose.

  • I think, for this instance, you would be better off determining if the "snapped" touch xy is greater than, less than etc of the initial touch position.

    The angle serves no purpose.

    Yep should work.

    But again my angle logic should have worked with brute js code :p

    It's okay I'm learning the limitations of some event stuff in c2

  • Construct 2 is working correctly, the problem is your own comparisons. -135 degrees is mathematically identical to +225 degrees, so both answers are equally correct. The problem is you are making linear comparisons that are not aware of the cyclical property of angles. You are expecting only one of the possible set of correct answers.

    Use the Sprite object's angle conditions instead, which are cycle-aware - for example "is within 90 degrees of 0 degrees" will match anything in the -90 to +90 degree range, or +270 to +90, or +270 to +450, etc. since they are all mathematically identical ranges and the math used in the condition is cycle-aware. Funnily enough this exact issue came up on the forums recently and I realise not everyone uses the Sprite object for this type of thing so the next beta should have those features as system conditions as well.

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