Rotate A Physical Object Of 90 Degrees Sharp

Dear All,

 

I was wondering if someone has a trick to turn a physical object (it as to be dynamic), for instance a rectangle, to 90 degrees sharp.

 

  • as the documentation mentioned you cannot used myrect.rotation=90 with dynamic body, as unpredictable results can occur, which is true

 

  • I tried to apply torque or angular impulse and monitoring the angle of my object on enterFrame listener (with a fps sets at 60), but, and it is logical, you cannot reach a precise angle.

 

Any help would be appreciated.

 

Best,

Jérôme.

Hi Jérôme,

Can you point me to the documentation that says you shouldn’t use “object.rotation” on physical bodies? This is actually acceptable and I use it frequently. If certain documentation says otherwise, I need to correct it.

 

Thanks,

Brent

Hi Brent,

 

I found it here : http://docs.coronalabs.com/api/type/DisplayObject/rotation.html (in the Gotchas section)

 

and i am experiencing these famous unpredictable results.

 

Thanks,

Jérôme.

 

P.S. : my current Version is 2013.1056

Hi Jérôme,

Are you rotating the rectangle around an exterior reference point (top-left, etc.) instead of its center? I just tried the following code and it works as expected:

 

[lua]

local physics = require(“physics”) ; physics.start() ; physics.setGravity( 0,2 ) ; physics.setDrawMode( “hybrid” )

–make box

local box = display.newRect( 0,0,400,160 )

box.x, box.y = display.contentWidth/2, display.contentHeight/2

physics.addBody ( box, “static”, { friction=1.0 } )

–make ball

local ball = display.newCircle( 0,0,20,20 )

ball.x, ball.y = display.contentWidth/2+40, display.contentHeight/2-200

physics.addBody ( ball, “dynamic”, { radius=20, bounce=0.6, friction=0.3 } )

–rotate box after 4 seconds

local function boxRotate()

box.rotation = 45

end

timer.performWithDelay(4000,boxRotate,1)

[/lua]

 

Try this and see if you get the expected results…

Brent

Thanks Brent,

 

in fact, what I am trying to do is to rotate a group as :

local physics = require("physics") ; physics.start() ; physics.setGravity( 0,0 ) ; physics.setDrawMode( "hybrid" ) --make box local grpBox = display.newGroup() local box1 = display.newRect( 0,0,40,40 ) box1.x, box1.y = display.contentWidth/2, display.contentHeight/2 local box2 = display.newRect( 0,0,40,40 ) box2.x, box2.y = box1.x+40, box1.y local box3 = display.newRect( 0,0,40,40 ) box3.x, box3.y = box2.x+40, box1.y grpBox:insert(box1) grpBox:insert(box2) grpBox:insert(box3) grpBox:setReferencePoint(display.CenterReferencePoint) physics.addBody ( box1, "dynamic", { friction=1.0 } ) physics.addBody ( box2, "dynamic", { friction=1.0 } ) physics.addBody ( box3, "dynamic", { friction=1.0 } ) --rotate box after 4 seconds local function boxRotate() grpBox.rotation = grpBox.rotation + 45 end timer.performWithDelay(400,boxRotate,0)

 

 

 

so you can see that the physics not follow, that why I was thinking to use physic to handle my rotation.

 

Txs,

Jérôme.

Hi Jérôme,

OK, this is expected behavior then. You can’t rotate or shift display groups of physics items around independently of each other, or the collision system will become inaccurate, as you’re seeing. I recently suggested a method to another user which basically “pre-calculates” the location of where physics objects would be when rotated around a non-center reference point (or in this case, rotated inside a group). Then it moves and rotates the objects to that position but keeps the group in the same coordinate space so collisions maintain their integrity.

 

http://forums.coronalabs.com/topic/32895-using-eg-topleftalign-on-a-physics-body-only-applies-to-its-appearance-not-its-body-help/

 

I don’t know if this will work for your scenario, but you could try it…

 

Brent

Hi Brent,

 

It works like a charm. 

 

Many thanks for the trick,

Best,

Jérôme.

Hi Jérôme,

Can you point me to the documentation that says you shouldn’t use “object.rotation” on physical bodies? This is actually acceptable and I use it frequently. If certain documentation says otherwise, I need to correct it.

 

Thanks,

Brent

Hi Brent,

 

I found it here : http://docs.coronalabs.com/api/type/DisplayObject/rotation.html (in the Gotchas section)

 

and i am experiencing these famous unpredictable results.

 

Thanks,

Jérôme.

 

P.S. : my current Version is 2013.1056

Hi Jérôme,

Are you rotating the rectangle around an exterior reference point (top-left, etc.) instead of its center? I just tried the following code and it works as expected:

 

[lua]

local physics = require(“physics”) ; physics.start() ; physics.setGravity( 0,2 ) ; physics.setDrawMode( “hybrid” )

–make box

local box = display.newRect( 0,0,400,160 )

box.x, box.y = display.contentWidth/2, display.contentHeight/2

physics.addBody ( box, “static”, { friction=1.0 } )

–make ball

local ball = display.newCircle( 0,0,20,20 )

ball.x, ball.y = display.contentWidth/2+40, display.contentHeight/2-200

physics.addBody ( ball, “dynamic”, { radius=20, bounce=0.6, friction=0.3 } )

–rotate box after 4 seconds

local function boxRotate()

box.rotation = 45

end

timer.performWithDelay(4000,boxRotate,1)

[/lua]

 

Try this and see if you get the expected results…

Brent

Thanks Brent,

 

in fact, what I am trying to do is to rotate a group as :

local physics = require("physics") ; physics.start() ; physics.setGravity( 0,0 ) ; physics.setDrawMode( "hybrid" ) --make box local grpBox = display.newGroup() local box1 = display.newRect( 0,0,40,40 ) box1.x, box1.y = display.contentWidth/2, display.contentHeight/2 local box2 = display.newRect( 0,0,40,40 ) box2.x, box2.y = box1.x+40, box1.y local box3 = display.newRect( 0,0,40,40 ) box3.x, box3.y = box2.x+40, box1.y grpBox:insert(box1) grpBox:insert(box2) grpBox:insert(box3) grpBox:setReferencePoint(display.CenterReferencePoint) physics.addBody ( box1, "dynamic", { friction=1.0 } ) physics.addBody ( box2, "dynamic", { friction=1.0 } ) physics.addBody ( box3, "dynamic", { friction=1.0 } ) --rotate box after 4 seconds local function boxRotate() grpBox.rotation = grpBox.rotation + 45 end timer.performWithDelay(400,boxRotate,0)

 

 

 

so you can see that the physics not follow, that why I was thinking to use physic to handle my rotation.

 

Txs,

Jérôme.

Hi Jérôme,

OK, this is expected behavior then. You can’t rotate or shift display groups of physics items around independently of each other, or the collision system will become inaccurate, as you’re seeing. I recently suggested a method to another user which basically “pre-calculates” the location of where physics objects would be when rotated around a non-center reference point (or in this case, rotated inside a group). Then it moves and rotates the objects to that position but keeps the group in the same coordinate space so collisions maintain their integrity.

 

http://forums.coronalabs.com/topic/32895-using-eg-topleftalign-on-a-physics-body-only-applies-to-its-appearance-not-its-body-help/

 

I don’t know if this will work for your scenario, but you could try it…

 

Brent

Hi Brent,

 

It works like a charm. 

 

Many thanks for the trick,

Best,

Jérôme.