How to check if attribute has been increased or decreased.

alexlaptevalexlaptev Member Posts: 12
edited July 2012 in Working with GS (Mac)
For example, rotation of an actor follows my mouse position, and after releasing the mouse I want to know, which direction the actor was rotating - clockwise or counterclockwise? Has rotation attribute been increased or decreased? Any ideas?

Answers

  • jn2002dkjn2002dk Member Posts: 102
    Before rotating you could store the angle in a local attribute and then compare it to the new angle
  • alexlaptevalexlaptev Member Posts: 12
    Thanks! But if I want to check the final direction, the rotation direction just before releasing the mouse? How I can compare the rotation attribute on the time of a mouse release and, for example, 0,05 sec before it?
  • IsabelleKIsabelleK Member, Sous Chef Posts: 2,807
    Rule if attribute is smaller than X
    Timer, after X second
    Rule if attribute is bigger than X
    Put your rules here.
  • alexlaptevalexlaptev Member Posts: 12
    Thanks, it works. I change the global variable every .05 sec (Timer) and compare it to an attribute on mouse release.
  • ORBZORBZ Member Posts: 1,304
    edited July 2012
    I don't have a solution but simply comparing the prev rotation to the new rotation won't work in one particular case:

    Consider:

    PrevAngle = 359

    Rotates 2 degrees

    NewAngle = 1

    PrevAngle < NewAngle? False


    I'm actually trying to solve something similar to this right now. Ideas welcome.
  • alexlaptevalexlaptev Member Posts: 12
    Yeah, you right. The angles in my project are not 0-360 but 0 to 180 and -180 to 0. So, PrevAngle is not 359 but -1. and PrevAngle < NewAngle? True. But the problem still there with 180 and -179.
  • alexlaptevalexlaptev Member Posts: 12
    Maybe to make a simple rule to check if the PrevAngle is between 90 and 180 & NewAngle is between -180 and -90; then inverse the behaviour. In your case the same with 270-360 && 0-90? Will try.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    You can fix the '359 - 0 boundary challenge' by using the formula:

    difference = ((difference +180)%360)-180

    This also has the nice effect of telling you if the difference is clockwise or not. (The result will be positive or negative.)
  • ORBZORBZ Member Posts: 1,304
    edited July 2012
    @Rthurman

    Are you sure that formula is correct?

    Care to give an example of its usage?
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    edited July 2012
    @ORBZ -- Here is an example:

    Make two attributes:
    self.oldRotation (type = real)
    self.difference (type = real)

    When mouse is down
    -- Change Attribute: self.oldRotation to self.Rotation
    -- Change Attribute: self.difference To: vectorToAngle( game.Mouse.Position.X - self.Position.X , game.Mouse.Position.Y - self.Position.Y )- self.Rotation
    -- Change Attribute: self.difference To: (( self.difference +180)%360)-180
    -- Interpolate Attribute: self.Rotation To: selfRotation + self.difference


    I just had a wave of nostalgia -- discovering this handy little trick was one of my first posts to the gamesalad forum:
    http://forums.gamesalad.com/discussion/28229/be-an-angel-fix-my-angle-stop-rotating-the-long-way-round-to-a-given-angle/p1
  • alexlaptevalexlaptev Member Posts: 12
    Nice, thanks!
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    You are welcome!

    This should simplify several challenges you might be having with controlling the speed and direction of an orbiting rocket.
Sign In or Register to comment.