[RESOLVED] Question - Rotate object through touch

I’ve come across another challenge with this spinning code. I’m now using it to spin a steering wheel, then have the car react as a result.

I don’t understand why the “rotation” value of my item starts at 0, and sometimes:

  1. goes up to 180 as I turn it to the right, and flicks to a negative value (say -179) when I get completely upside down, using negative values increasing to 0 as I continue to turn the item to the right and back to vertical (0).

  2. goes up to some other value (say 43) as I turn to the right, then flicks to a large negative (-317), increasing to zero as I continue to turn to the right.

  3. goes all the way to 360 as I turn to the right, and back to 0 when I return to the top.

It seems to change based on where on the wheel I grab to start turning. Bizarrely the car seems to take on the same rotation numbers at the same time (i.e. -55 on the wheel = -55 for the car, even though -55 may be a completely different on-screen angle in example 1 vs example 2).

Is there a way to make this consistent so that I can use the difference between the wheel rotation and the car rotation to define the torque that I want to apply to the car?

Thanks,

Nathan.

Hi Nathan,

You may want to try this code, which should control the “flick to opposite” behavior that you’re describing. This code calculates the angle between your object (at “srcX”/“srcY”) and a touch (at “dstX”/“dstY”), which may not be how your control actually works, but parts of the code can be parsed out and implemented as you see fit.

[lua]

local function angleBetween( srcX, srcY, dstX, dstY )

        local angle = ( math.deg( math.atan2( dstY-srcY, dstX-srcX ) )+90 )

        if ( angle < 0 ) then angle = angle + 360 end ; return angle % 360

end

[/lua]

Hope this helps,

Brent

Brent,

Woohoo - looks like the problem is fixed. I used your line “if ( angle < 0 ) then angle = angle + 360 end ; return angle % 360” to “fix” the angles being returned by rotation, so they reliably went from 0-360.

Thanks,

Nathan.