How to measure speed of rotation

Hey guys,

quick question.  Is it possible to measure the speed of a rotating physics object.  I have a car game where the car remains at the center of the screen, and the world behind it moves according to the accelerometer and gravity.  The tires are rotating against the ground and I would like to measure their speed of rotation (how fast they are spinning).

Ultimately I want to have multiple car engine sounds, dependent upon how fast the car is moving.

Thanks in advance,

Ed

The easiest way I can think of is to store the rotation in one frame, and then in the next frame compare the current rotation to the previously stored rotation.

E.g.

local prevRotation = nil local speed = 0 local function update() if prevRotation then speed = myWheel.rotation - prevRotation print(speed) end prevRotation = myWheel.rotation end Runtime:addEventListener("enterFrame", update)

That’s very basic, but the principle should work just fine.

Hmmm…thanks for that but unfortunately that doesn’t give me the speed of the rotation, just the rotation itself. But when you wrote your code it made me think of angularVelocity, which is exactly what I needed.

print (tire1.angularVelocity)

that told me the speed that I was look for.

I don’t know what you do exactly with it, but I guess you try to limit it.

Use the clamp method:

local function clamped( value, lowest, highest ) return math.max( lowest, math.min( highest, value ) ) end local angularVelocity = clamped(tire1.angularVelocity, -100, 100); tire1.angularVelocity = angularVelocity

The easiest way I can think of is to store the rotation in one frame, and then in the next frame compare the current rotation to the previously stored rotation.

E.g.

local prevRotation = nil local speed = 0 local function update() if prevRotation then speed = myWheel.rotation - prevRotation print(speed) end prevRotation = myWheel.rotation end Runtime:addEventListener("enterFrame", update)

That’s very basic, but the principle should work just fine.

Hmmm…thanks for that but unfortunately that doesn’t give me the speed of the rotation, just the rotation itself. But when you wrote your code it made me think of angularVelocity, which is exactly what I needed.

print (tire1.angularVelocity)

that told me the speed that I was look for.

I don’t know what you do exactly with it, but I guess you try to limit it.

Use the clamp method:

local function clamped( value, lowest, highest ) return math.max( lowest, math.min( highest, value ) ) end local angularVelocity = clamped(tire1.angularVelocity, -100, 100); tire1.angularVelocity = angularVelocity