physics not behaving as intended

hi,

I am using a drag function as seen in this thread - http://forums.coronalabs.com/topic/49510-drag-object-returning-nil-value/

but I am seeing some undesirable effects with the physics behaviour of the other functionality related to collisions -

I have some objects that move from a to b via transition.to. the dragged static body when dragged behind the transitioning object will knock the transitioning object off course as if reacting with the transitioned path. this can be exaggerated by setting the physics.setScale up quite high. this doesn’t happen if I test with gravity instead of transition. it’s just a basic transition.to statement with reasonable sized dynamic objects.

also, the dragged object if dynamic is overlapping the transitioning objects which are also dynamic. hence it is dynamic reacting with dynamic (transitioning object). if I change the dragged object to static then it doesn’t really overlap the transitioning objects… why would that be happening? it means that the objects might not bounce off each other until they are completely overlapped sometimes and then others it may react on the touch… the speed that you move the dragged object can have an effect on this also, but it’s still happening sometimes when it’s not even being dragged very fast…

Can you post your code? Can’t really help without it.

okay, this is a trimmed down version that still gives the described results. If you then comment out the transition, uncomment gravity and set the dragged object to dynamic, you will see how it noticeably overlaps the other dragged object before reacting… this is an issue for my application as I am using a combination of static and dynamic… i may switch to a multi body element… however for my own understanding I am still interested to learn more about why these undesirable behaviours are occurring.

thank you for any assistance!

[lua]

local physics = require( “physics” )

physics.start( )

–physics.setGravity( 0, -0.3 )

 

physics.setDrawMode( “debug” )

 

local ballMain = display.newCircle( display.contentCenterX, display.contentCenterY - 50, 10 )

physics.addBody( ballMain, “static”, {density=1, radius=10, friction=0.3} )

 

local ballMove = display.newCircle( display.contentCenterX, display.contentHeight - 50, 15 )

physics.addBody( ballMove, “dynamic”, {density=1, friction=0.3, radius=15 } )

transition.to( ballMove, {time=10000, y=20, iterations=-1} )

 

function onTouchMove(self, event)

if(event.phase == “began”) then

display.getCurrentStage( ):setFocus(self)

 

self.markX = self.x

self.markY = self.y

 

elseif(event.phase == “moved”) then

self.x = (event.x - event.xStart) + self.markX

self.y = (event.y - event.yStart) + self.markY

elseif(event.phase == “ended” or event.phase == “cancelled”) then

display.getCurrentStage( ):setFocus( nil )

end

return true

end

ballMain.touch = onTouchMove

 

Runtime:addEventListener( “touch”, ballMain)

[/lua]

Hi @Ducky,

I think basically what’s happening here is, because you have this drag functionality, you’re somewhat “fighting against” the physics engine. Meaning that, the physics engine is trying to do its job, but you’re forcing positional updates, and the engine can only sense collisions each frame, which will result in the occasional overlap as Box2D attempts to catch up. Hope that makes sense…

Brent

but it does the same thing if you move the object via transition.to instead of the drag function… sometimes I’m seeing the ball pass straight through one of the other balls.

Hi @Ducky,

The same thing can potentially happen with transitions, if the transition is fast enough… meaning, if the two objects are “forced” through each other as a result of the transition, in the span of 1 app time step, then they can pass through each other.

That being said, there are methods to help prevent this. You could change the ball to a “bullet” body type, which increases the collision sensing on it (but comes with a slight performance impact). You could also increase the overall physics collision detection rate and sensitivity, but that also comes with a slight performance impact so you should use it carefully.

Brent

okay, so what is the desired way to manipulate and move objects and then react with others so that the physics engine behaves as intended?

Hi @Ducky,

My general advice is, if you’re using physics, use it all the way. Move objects using the physical methods, and things will generally work out as you need. I understand that this isn’t always possible, and if you need something to absolutely and strictly follow the user’s touch, or do a transition, then sense for and react to a collision immediately, and take the proper action (stop the transition, for example). And maybe turn up the collision sensory properties to prevent overlap, etc. etc. Just remember that Box2D is a physics simulation, not the perfect real world, so you need to handle it as best as possible.

Have fun,

Brent

Can you post your code? Can’t really help without it.

okay, this is a trimmed down version that still gives the described results. If you then comment out the transition, uncomment gravity and set the dragged object to dynamic, you will see how it noticeably overlaps the other dragged object before reacting… this is an issue for my application as I am using a combination of static and dynamic… i may switch to a multi body element… however for my own understanding I am still interested to learn more about why these undesirable behaviours are occurring.

thank you for any assistance!

[lua]

local physics = require( “physics” )

physics.start( )

–physics.setGravity( 0, -0.3 )

 

physics.setDrawMode( “debug” )

 

local ballMain = display.newCircle( display.contentCenterX, display.contentCenterY - 50, 10 )

physics.addBody( ballMain, “static”, {density=1, radius=10, friction=0.3} )

 

local ballMove = display.newCircle( display.contentCenterX, display.contentHeight - 50, 15 )

physics.addBody( ballMove, “dynamic”, {density=1, friction=0.3, radius=15 } )

transition.to( ballMove, {time=10000, y=20, iterations=-1} )

 

function onTouchMove(self, event)

if(event.phase == “began”) then

display.getCurrentStage( ):setFocus(self)

 

self.markX = self.x

self.markY = self.y

 

elseif(event.phase == “moved”) then

self.x = (event.x - event.xStart) + self.markX

self.y = (event.y - event.yStart) + self.markY

elseif(event.phase == “ended” or event.phase == “cancelled”) then

display.getCurrentStage( ):setFocus( nil )

end

return true

end

ballMain.touch = onTouchMove

 

Runtime:addEventListener( “touch”, ballMain)

[/lua]

Hi @Ducky,

I think basically what’s happening here is, because you have this drag functionality, you’re somewhat “fighting against” the physics engine. Meaning that, the physics engine is trying to do its job, but you’re forcing positional updates, and the engine can only sense collisions each frame, which will result in the occasional overlap as Box2D attempts to catch up. Hope that makes sense…

Brent

but it does the same thing if you move the object via transition.to instead of the drag function… sometimes I’m seeing the ball pass straight through one of the other balls.

Hi @Ducky,

The same thing can potentially happen with transitions, if the transition is fast enough… meaning, if the two objects are “forced” through each other as a result of the transition, in the span of 1 app time step, then they can pass through each other.

That being said, there are methods to help prevent this. You could change the ball to a “bullet” body type, which increases the collision sensing on it (but comes with a slight performance impact). You could also increase the overall physics collision detection rate and sensitivity, but that also comes with a slight performance impact so you should use it carefully.

Brent

okay, so what is the desired way to manipulate and move objects and then react with others so that the physics engine behaves as intended?

Hi @Ducky,

My general advice is, if you’re using physics, use it all the way. Move objects using the physical methods, and things will generally work out as you need. I understand that this isn’t always possible, and if you need something to absolutely and strictly follow the user’s touch, or do a transition, then sense for and react to a collision immediately, and take the proper action (stop the transition, for example). And maybe turn up the collision sensory properties to prevent overlap, etc. etc. Just remember that Box2D is a physics simulation, not the perfect real world, so you need to handle it as best as possible.

Have fun,

Brent