Expression returns negative angles...?

2 favourites
  • 6 posts
  • I can use angle(x1,y1,x2,y2) to get an object to point towards another. This works fine; the object rotates between 0 and 359 as expected.

    However, if I'm using this expression to set a variable instead of an object's angle, it returns negative angles. For example, 180 becomes -180 / 225 becomes -135, and so on.

    What's the deal with that?

    To clarify on what I'm doing...I'm using a sprite object with 8 animations that represent 8 different angles it can face. They are named a0, a45, a90, a135, and so on. I'm using the angle() expression to get the angle between this object and my player, but storing it in an instance variable. I then use this instance variable to tell the object which animation to play (which angle it should be facing). Why? Because the perspective of this object doesn't allow me to simply rotate it - I have to use a different sprite for each 45 degree increment.

    I could solve this by putting these negative angles in the animation names, but C2 won't allow the "-" character, so that's not an option.

  • I've run into this issue before. I ended up using an intermediate function:

    On angle360(x1,y1,x2,y2)

    If angle(params) < 0 | return 360+angle(params)

    Else | return angle(params)

    This will give you the standard 360 degree angles starting from 0 at 3oclock going counter clockwise.

  • im usually using abs(angle(x1,y1,x2,y2)) that solves it for me most of times.

    abs-olute basically takes "-n" and shows u "n" takes "n.nnnnnnnnnnnnn" and shows "n" absolute values to the rescue lol

    Edited: however my suggestion it might create problems giving only positive values... R0j0's formula should work better.. makes more sense in mathematical way.

  • It's a mathimatical reason. The angle() function uses atan() to do the calculation, which has a range of [-180,180). Also that range is often referred to as a normalized angle and is usuful at times. Having the angle in the range of [0,360) is mainly useful in the case you stated above, and I usually convert it over in a expression with: (angle(x1,y1,x2,y2)+360)%360

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • (angle(x1,y1,x2,y2)+360)%360

    I forgot modulo! That is a much more elegant solution.

  • Thanks guys! R0j0hound's looked the simplest so I ended up going with that. To be exact I used int(((angle(self.X,self.Y,player.X,player.Y)+360)%360)/45)*45 to get the 0-360 range as an integer in 45 degree increments. Yeesh.

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