I use physics and pivot with object:rotate to code an orbital revolution.
I do this instead of updating x and y properties on enterframe because it’s way faster.
I thought everyting was ok until I realized
I had a strange behaviour that despite all my efforts, I could not correct.
I made a sample code and an image to explain my issue.
The code :
-- setup :
local physics = require "physics"
physics.start()
physics.setGravity( 0, 0 )
physics.setDrawMode( "hybrid" )
local function getPositions( x, y, angle, distance )
local mPi = math.pi;local mCos = math.cos;local mSin=math.sin; local mCeil=math.ceil
return { mCeil(x + (distance \* mCos((angle-90) \* mPi / 180))), mCeil(y + (distance \* mSin((angle-90) \* mPi / 180))) }
end
local indices={}
-- 1 the center :
local center = display.newCircle( 160, 240, 5 )
center:setFillColor(200,0,0,255)
-- physics for rotation
physics.addBody( center, "static", { radius=5 })
-- 2 the rectangles :
local function doObject( angle, distance)
-- the object :
local object=display.newRect( 0, 0, 5, 30 )
object:setFillColor(200,0,200,255)
-- place the object
indices = getPositions( 160, 240, angle, distance )
object:translate( indices[1], indices[2] )
object:rotate(angle)
-- physics for rotation
physics.addBody( object, "dynamic", { isSensor = true, radius=5 } )
physics.newJoint( "pivot", object, center, 160, 240 )
return object
end
local objects={}
for i=1, 5 do
objects[i]=doObject(math.random(360), 150)
end
-- 3 the rotation :
local function mainloop( event )
for i=1,#objects do
objects[i]:rotate(9)
end
end
The image that explains my issue :
https://dl.dropbox.com/u/67104046/pivotmadness.png
Thx for any hints
[import]uid: 9328 topic_id: 28146 reply_id: 328146[/import]