Be an Angel fix my Angle. Stop rotating the long way round to a given angle?

2»

Comments

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,879
    LudwigHeijden said:
    For the pendulum part. There is an example available which is almost good. Do a search in the gamesalad application. I believe it is named swing example or something. I have it working in my next update for Kung Fu Finger with the accelerometer. The only thing I couldn't get fixed is that they start at upside down when the level starts.

    OK -- following your suggestion I have studied FireMapleGame's swinging/hanging demo and really like it. It is just right for learning about making things swing like pendulums. As I studied it, I realized that he had implemented a 'negative feedback control system'. (Whether he meant to or not -- thats what he did.)

    For my hanging/swinging portrait, I devised the simplest 'control system' pendulum I can think of -- and will show it below. But first -- before this seems like a hijacking of the original thread, I want to bring this back around to StormyStudio's original reason for the thread. He wanted to use interpolate on his angles because it felt a little more natural to him. (Objects really do ease in and out as they move.) The control system approach I'm about to show also naturally 'interpolates' object's attributes. So Stormy, if you are still following this thread, this will help your flashlight turn towards the mouseclick with a natural effect.

    OK here goes!

    First of all, you have already done 90% of the work of making a pendulum swing in realizing that you need to find the difference between the current angle (self.rotation) and the desired or target angle (down because of gravity). Then all that is left is to successively approximate getting to the desired angle in small increments.

    Make an actor and place it on the scene with it's 'bottom' pointing toward zero (the bottom pointing to the right). It might help to give the actor an image that has an obvious direction indicator like an arrow or something. We would want the arrow to swing downward and oscillate like a pendulum before it finally comes to rest.

    Give this actor three attributes:
    targetAngle (real)
    difference (real)
    pendulumOutput (real)

    Then give the actor the following behaviors:
    Inside a Timer (every .025 seconds):
    ….Change Attribute: self.difference To:270-self.Rotation
    ….Change Attribute: self.difference To:(((self.difference+180)%360)-180)*.99
    ….Change Attribute: self. pendulumOutput To: ( self.pendulumOutput + self.difference)*.96
    ….Change Attribute: self. motion.AngularVelocity To: self.pendulumOutput

    And that's it!

    The 270 represents 'down'. Change it to whatever angle and device orientation you need. (Stormy can change it to the angle of the mouse click.)
    The .99 and .96 are '"slowing factors". You can tweak them both until the pendulum's speed and oscillation seems just right. But be careful! Give the wrong slowing factors and violent oscillations will begin.

    This should get a pendulum swinging naturally to the correct angle without all the violent oscillations that your game is currently exhibiting.

    And look! It still uses the shortest angle formula that we discovered. (Which was the intent of this discussion in the first place.)
  • LumpAppsLumpApps Member Posts: 2,881
    Looks great!
    This is very usefull in Kung Fu Finger where actors need to swing.
    I'll probably try to make custom timers because I have lots of actors swinging at the same time.
    The version I have now already had pendulums but with the upside down swing in the beginning as Fire Maples one. I tried to get this better for hours and hours but never got it to work right.
    Finaly I start to like math. I was never good at it because I am a practical person but with game salad it is all put to practice ;)

    RThurman thanks a lot for posting and Stormy sorry for clogging your post :))
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    Ludwig I got the answer I needed earlier on so please clog away :-)
Sign In or Register to comment.