How would one calculate how many times an object rotated..

Greetings,

Seems like this would be relatively simple concept but having difficulties on how to approach.  Some guidance would be appreciated.

Basically I want to be able to calculate how much an object rotates a full 360.

I know how I would start and stop the calculation - but just wondering how to be able to determine when an object has done a full rotation so to speak… ?

More importantly - regardless if it rotates left or right… one complete rotation keeps getting counted, doesn’t matter direction of spin- if it spins right two times fully… then spins left 3 times… it should count as 5 times.

Thanks for any input!

what “causes” the rotation?  that is, find that code, start there.  then probably keep a running sum of those individual “delta” rotations, because  something simpler like “currentAngle - startAngle” might not reveal the total rotation (due to the modular math)

So I basically have everything.  

My only issue is resetting the ‘object.rotation’ so it never goes past 360 or -360 (depending which way it spins).  But still maintaining a spin… kinda hard to describe.

I have an object that spins, and say it does two complete rotations then stops - ‘object.rotation’ returns 720.  I have a pretty simple function that has a counter for each 360… then I can always get the rotation between 0-360 by subtracting the ‘object.rotation’ with rotationCount * 360.

object.rotation - (360\*rotationCount)

But when I want to reset the ‘object.rotation’ to 0 , it still reads the ‘object.rotation’ as 720.  So the object spins twice to get it back aligned to 0. I want to have this object rotate multiple times - then at a certain point, realign with 0 degrees (or upright?) without rotating 3 or 4 times back like rewinding a clock…

:ph34r:

EDIT: whoops should be using Delta Angle rotation…

object:rotate(deltaAngle)

Are you getting .rotation values > 360? If so:

numRotations = math.floor( object.rotation / 360 )

actualAngle = object.rotation - ( numRotations * 360 )

Rob

Yeah I know how to get the actual rotations and angle between 0 and 360.

Is there any way to keep the ‘object.rotation’ between 0 and 360 even if it rotates around continuously?  Effectively making the ‘object.rotation’ be higher then 360.  But I want it to be able to continually rotate the object while the .rotation value doesn’t go higher then 360 - instead of going above it just goes back to 0 value.

@Rob Miracle - thanks this helps… one issue I’m finding is that when the object changes rotation direction (spin left after it has spun right, or vice versa), the number of rotations is subtracted (or added depending on which way it’s rotating).

I tried to circumnavigate this by using: numRotations = math.abs(numRotations) but doesn’t seem to matter 

I’m trying to count number of rotations total - regardless if it’s rotating left or right , which the object will do… 

fwiw, that’s the benefit of the “sum up the deltas” approach, consider:

if you wanted the total signed rotation (ie, the net relative rotation, rotation in the positive direction minus rotation in the negative direction), then:  totalRotation = totalRotation + deltaRotation

if you instead wanted the total unsigned rotation (ie, the absolute total of all rotation, regardless of direction), then:  totalRotation = totalRotation + math.abs(deltaRotation)

because if you just “throw away” those deltas (that is, once they’re consumed into the aggregate) it becomes quite difficult to adequately “guess” what total rotation (signed OR unsigned) might have be in the presence of +/- rotations (that may have also wrapped the modulo in either/both direction).

Thanks @davebollinger

I’ll give this a shot and see if I can achieve what I wanted.  I’m just not sure how to get deltaRotation since it’s not a pre-defined lua variable… been more difficult then I thought, I may just add two sensors, sensor1 on top, and sensor2 on bottom.

Soon as sensor1.y > sensor2.y - add to a rotation count (I’m actually counting 180 degrees not 360, but easy to modify for either) …

oh… physics!  that was the missing piece of your original question (and probably why you didn’t seem to know where to look)

so you’ll want to do something like “monitor” the object’s rotation per frame (probably in an enter frame listener) to “discover” it’s delta rotation for that frame, then sum it signed/unsigned as you need.  (this approach might still fail if object capable of rotating very fast, >180d per frame)

something like (untested, just for ideas)

object.previousRotation = object.rotation -- make a place to store it object.sumRotation = 0 -- make a place to sum it local function onEnterFrame(event) local deltaRotation = (obj.rotation - object.previousRotation) % 360 -- calc delta object.sumRotation = object.sumRotation + deltaRotation -- sum deltas object.previousRotation = object.rotation -- store for next time end

(noting that you may have to adjust the modular arithmetic depending on whether wanting signed/unsigned sums)

Ahh great thank you @davebollinger —I’ll definitely give this a look.

Right now I’m approaching it a little differently… My game is based on rotation of an object - that is shaped like a rectangle.

I have two sensors with the ‘weld’ joint physics attached to the top and bottom , named respectively.

Then I have another variable that is basically an ‘upright’ position true/false.

Then using this bit of code (I have some other things that aren’t relevant here):

if upRight == true and flipSensorTop.y \> flipSensorBottom.y + 100 then rotationCounts = rotationCounts + 1 upRight = false elseif upRight == false and flipSensorTop.y \< flipSensorBottom.y - 100 then rotationCounts = rotationCounts + 1 upRight = true end

I have a couple other variables to prevent some things but that’s the basic part.  Haven’t tested enough to know it works as well as I want… but so far so good.

Appreciate the input

what “causes” the rotation?  that is, find that code, start there.  then probably keep a running sum of those individual “delta” rotations, because  something simpler like “currentAngle - startAngle” might not reveal the total rotation (due to the modular math)

So I basically have everything.  

My only issue is resetting the ‘object.rotation’ so it never goes past 360 or -360 (depending which way it spins).  But still maintaining a spin… kinda hard to describe.

I have an object that spins, and say it does two complete rotations then stops - ‘object.rotation’ returns 720.  I have a pretty simple function that has a counter for each 360… then I can always get the rotation between 0-360 by subtracting the ‘object.rotation’ with rotationCount * 360.

object.rotation - (360\*rotationCount)

But when I want to reset the ‘object.rotation’ to 0 , it still reads the ‘object.rotation’ as 720.  So the object spins twice to get it back aligned to 0. I want to have this object rotate multiple times - then at a certain point, realign with 0 degrees (or upright?) without rotating 3 or 4 times back like rewinding a clock…

:ph34r:

EDIT: whoops should be using Delta Angle rotation…

object:rotate(deltaAngle)

Are you getting .rotation values > 360? If so:

numRotations = math.floor( object.rotation / 360 )

actualAngle = object.rotation - ( numRotations * 360 )

Rob

Yeah I know how to get the actual rotations and angle between 0 and 360.

Is there any way to keep the ‘object.rotation’ between 0 and 360 even if it rotates around continuously?  Effectively making the ‘object.rotation’ be higher then 360.  But I want it to be able to continually rotate the object while the .rotation value doesn’t go higher then 360 - instead of going above it just goes back to 0 value.

@Rob Miracle - thanks this helps… one issue I’m finding is that when the object changes rotation direction (spin left after it has spun right, or vice versa), the number of rotations is subtracted (or added depending on which way it’s rotating).

I tried to circumnavigate this by using: numRotations = math.abs(numRotations) but doesn’t seem to matter 

I’m trying to count number of rotations total - regardless if it’s rotating left or right , which the object will do… 

fwiw, that’s the benefit of the “sum up the deltas” approach, consider:

if you wanted the total signed rotation (ie, the net relative rotation, rotation in the positive direction minus rotation in the negative direction), then:  totalRotation = totalRotation + deltaRotation

if you instead wanted the total unsigned rotation (ie, the absolute total of all rotation, regardless of direction), then:  totalRotation = totalRotation + math.abs(deltaRotation)

because if you just “throw away” those deltas (that is, once they’re consumed into the aggregate) it becomes quite difficult to adequately “guess” what total rotation (signed OR unsigned) might have be in the presence of +/- rotations (that may have also wrapped the modulo in either/both direction).

Thanks @davebollinger

I’ll give this a shot and see if I can achieve what I wanted.  I’m just not sure how to get deltaRotation since it’s not a pre-defined lua variable… been more difficult then I thought, I may just add two sensors, sensor1 on top, and sensor2 on bottom.

Soon as sensor1.y > sensor2.y - add to a rotation count (I’m actually counting 180 degrees not 360, but easy to modify for either) …

oh… physics!  that was the missing piece of your original question (and probably why you didn’t seem to know where to look)

so you’ll want to do something like “monitor” the object’s rotation per frame (probably in an enter frame listener) to “discover” it’s delta rotation for that frame, then sum it signed/unsigned as you need.  (this approach might still fail if object capable of rotating very fast, >180d per frame)

something like (untested, just for ideas)

object.previousRotation = object.rotation -- make a place to store it object.sumRotation = 0 -- make a place to sum it local function onEnterFrame(event) local deltaRotation = (obj.rotation - object.previousRotation) % 360 -- calc delta object.sumRotation = object.sumRotation + deltaRotation -- sum deltas object.previousRotation = object.rotation -- store for next time end

(noting that you may have to adjust the modular arithmetic depending on whether wanting signed/unsigned sums)

Ahh great thank you @davebollinger —I’ll definitely give this a look.

Right now I’m approaching it a little differently… My game is based on rotation of an object - that is shaped like a rectangle.

I have two sensors with the ‘weld’ joint physics attached to the top and bottom , named respectively.

Then I have another variable that is basically an ‘upright’ position true/false.

Then using this bit of code (I have some other things that aren’t relevant here):

if upRight == true and flipSensorTop.y \> flipSensorBottom.y + 100 then rotationCounts = rotationCounts + 1 upRight = false elseif upRight == false and flipSensorTop.y \< flipSensorBottom.y - 100 then rotationCounts = rotationCounts + 1 upRight = true end

I have a couple other variables to prevent some things but that’s the basic part.  Haven’t tested enough to know it works as well as I want… but so far so good.

Appreciate the input