pivot revolution madness ...

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 :slight_smile: [import]uid: 9328 topic_id: 28146 reply_id: 328146[/import]

You don’t say what the strange behavior is, but from your screenshot I guess you want the objects to always point to the center dot? In your sample code your ‘mainloop’ function isn’t being called, but assuming your actual code is using it (perhaps in an “enterFrame” event listener) then line 43 would rotate each object 9 degrees. Try rotate(0) and see if they point towards the center. [import]uid: 9422 topic_id: 28146 reply_id: 113724[/import]

Thx for your feedback.
You’re right : I want my objects to point to the center.

But you missed the trick : the rotate(9) is essential. It’s the thing making my objects revolute. [import]uid: 9328 topic_id: 28146 reply_id: 113729[/import]

I added

Runtime:addEventListener("enterFrame",mainloop)

to the end of your sample to see what you are talking about.

I see that the rotate(9) causes the orbiting effect, but it’s also causing the objects to point away from the center. Setting it to 0 causes them to stop orbiting but they do then point center. Setting it to 30 causes the orbit to be very fast but then the objects are pointing 30 degrees away from center.

In my experience with Box 2D and Corona there’s really no fooling the physics system without some unwanted side effects or outright crashing, so this might just be a limitation of your “trick”.

You could try making the orbiting joint objects invisible and then add new, visible rectangle objects attached with weld joints to the orbiting objects. Then counter rotate the visible objects same # of degrees as you rotate the underlying invisible orbit joints. [import]uid: 9422 topic_id: 28146 reply_id: 113756[/import]

I indeed omit this :

  
-- pause :  
local text = display.newText( "PAUSE", 0, 0, native.systemFont, 30 )  
text.x = 160  
text.y = 460  
text:setTextColor( 255,110,110 )  
local isPause=false  
local function pause( event )  
  
 if event.phase == "began" then  
  
 if isPause==false then  
 Runtime:removeEventListener("enterFrame", mainloop)  
 isPause=true  
 text.text="PAUSE"  
 else  
 Runtime:addEventListener("enterFrame", mainloop)  
 isPause=false  
 text.text="RUNNING"  
  
 end  
  
 end  
  
end  
  
text:addEventListener("touch",pause)  

I feared it was about some unwanted side effects.
I had no idea how to countertrick :), and you gave me one.
It may be a little hard to put it up but I keep your solution.
Thx again. [import]uid: 9328 topic_id: 28146 reply_id: 113837[/import]