How do I get inertia rotation?

Not favoritedFavorited Favorited 0 favourites
  • 5 posts
From the Asset Store
A template for creating sprite editing functionality, including selection, resizing, rotation, duplication and more
  • Hi, guys!

    I have been trying to achieve a rotation with momentum (inertia).

    It's about a submarine (top down perspective). Rotating must be smooth (I achieve this) but it's not slowing down until stops when key/stick is not being pressed anymore.

    Using anglediff or anglelerp on this code didn't do the trick:

    anglelerp(self.Angle,angle(0,0,gamepad.axis(0,2),gamepad.axis(0,3)),1*dt)

    OR

    anglediff(self.Angle,angle(0,0,gamepad.axis(0,2),gamepad.axis(0,3)))*(dt)

    Could you help with this? (It could be keyboard instead of gamepad, of course).

  • Giving something momentum basically means you give it velocity and instead of changing the position or angle, you’d change the velocity.

    It’s pretty popular to use lerp() to do an ease out but I don’t think that’s suitable to use with momentum.

    So you’d add a angvel variable to the sprite and then one way you can change the angular velocity is with a damped spring. Diff is the signed angle difference which is like the anglediff() expression but it’s negative if ccw. In the expression that sets the angvel you’ll see 0.1. You can change those in the range of 0-1 to adjust the spring stiffness and the amount of damping.

    Var diff=0

    Compare: dt>0

    — set diff to sprite.angle-angle(0,0,gamepad.axis(0,2),gamepad.axis(0,3))

    — set diff to angle(0,0,cos(diff),sin(diff))

    — sprite: add -0.1*diff/dt-0.1*self.angvel to angvel

    — sprite: rotate self.angvel*dt degrees clockwise.

  • Thanks, R0J0! (as usually :)

    I need to understand:

    Compare dt>0 (it would be diff>0?)

    in the first action set diff to sprite.angle-angle(0,0,gamepad.axis(0,2),gamepad.axis(0,3))

    and then, the same variable to angle(0,0,cos(diff),sin(diff))???

    The third event "add -0.1*da (da would be dt?)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I have been trying to achieve a rotation with momentum (inertia).

    It's about a submarine (top down perspective). Rotating must be smooth (I achieve this) but it's not slowing down until stops when key/stick is not being pressed anymore.

    I hope you know that you could easily achieve what you want by simply adding the Physics behavior to the submarine and setting the Linear Damping and Angular Damping parameters to 1, as you can see in this example: fileport.io/Zq7xVzCJ7TCF

  • Hi.

    The “compare: dt>0” is correct. Dt can be 0 in construct but we don’t want that since we divide by dt, and dividing by 0 would give infinity.

    And yes, we are setting diff twice but the second one is using the result of the first. Basically what that’s calculating is the signed angle difference or: singnedAngleDiff(a,b)=angle(0,0,cos(a-b),sin(a-b))

    Or another way to think about it is first we set diff=a-b then we normalize the angle to the range -180,180.

    The third action had a typo. “da” should have been “diff”. My apologies. The formula defines a damped spring.

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