# Math code for a rotation speed?

### » Sat Nov 28, 2009 5:12 am

I'm trying to make a ship rotate at a speed with some inertia. To if the direction to rotate to is flipped than it's not going to instantly turn to the angle at full speed, but rather sow down turning the other way then seed up, exactly like the angular velocity in physics (Which I cannot use for this project)

I can work it up with code but it would take 4-5 lines with some funkay computations.

Is there a simple math in Construct that can do this?
B
33
S
14
G
4
Posts: 251
Reputation: 4,049

### » Sat Nov 28, 2009 5:56 am

You could use a private variable to store rotation speed. Whenever you hold down the right or left keys you would either add or subtract a certain amount from this variable. Then you would apply the rotation speed to the object by setting it's angle to: Object.Angle + RotationSpeed * TimeDelta

Here's a quick example: Inertial Rotation

Hope this helps.
Moderator
B
104
S
39
G
19
Posts: 1,584
Reputation: 18,167

### » Sat Nov 28, 2009 4:12 pm

Yeah, thanks I had that worked out sort of. But my controls are you turn an arrow that points the direction and the ship rotates to that arrow, and the problem of a rotation speed is that when it reaches the arrow the ship will over shoot it, then slow down, then over shoot the other side then slow down etc.

Is there some way to get if the ship needs to rotate clockwise and return (1) or counter-clockwise and return (-1)?
B
33
S
14
G
4
Posts: 251
Reputation: 4,049

### » Sat Nov 28, 2009 4:33 pm

+Always
> Rotate Ship toward MouseX,MouseY by anglediff(Ship.Angle,angle(Ship.X,Ship.Y,MouseX,MouseY))*timedelta degrees

You can influence the rotation amount, if the above equation doesn't satisfy you.
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

### » Sat Nov 28, 2009 4:36 pm

That doesn't work, if you quickly go to the other side then it's going to snap in the other direction, not have any inertia. Thanks though.
B
33
S
14
G
4
Posts: 251
Reputation: 4,049

### » Sat Nov 28, 2009 5:59 pm

Oh, right, momentum. Hmm, now that'd be a little trickier. There is no simple way to do this that I can think of at the moment.

You'd have to create a 'momentum' variable that you add to whenever the ship thrusts toward desired angle and remove from when you thrust in the opposite direction. That 'momentum' variable would then affect the rotation speed.
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

### » Sat Nov 28, 2009 7:06 pm

maybe you could use the anglediff to have some sort of brake, the lesser the anglediff the lower the rotationspeed, when anglediff = 0 the rotationspeed should be 0 , so you wouldn't have overshoot
B
2
G
3
Posts: 72
Reputation: 994

### » Sat Nov 28, 2009 8:27 pm

Try something like: always set angle to anglediff*0.9

The bigger the angle difference, the faster it will turn (there are various ways to cap the speed), and it won't overshoot when it gets to the right angle.

Also, what I typed won't work exactly because it doesn't have timedelta. It should look like: always set angle to anglediff*(somenumber*timedelta) but I can't remember what somenumber's supposed to be for that. Try experimenting with what number should be there.
Moderator
B
89
S
32
G
33
Posts: 3,005
Reputation: 27,467