Very slight object movement after resetting position

Before I describe my problem, I would like to apologize for the vague title. I couldn’t think of how to describe my problem briefly.

Anyway…

I have a game that involves creating a circle object, inserting it into a map loaded with ponytiled, and adding it to the physics engine. When the object touches an obstacle, the object is destroyed and an overlay is shown that allows the player to modify certain aspects of the game. Then, another, identical object is put in the place of the original when the overlay is hidden. The problem I am having is that whenever the second object comes along, it moves ever so slightly before it begins to be affected by the physics engine (normally it shifts up).

Normally, I wouldn’t be bugged by this as the movement is slight. However, I have noticed that this leads to some unpredictable behavior on certain levels. Due to the nature of the game, it would not work for me to reuse the same object; it must be a new one each time.

This should be all the applicable code: 

In scene1:

local playerLib = require "libs.player" local player = playerLib.player --creates player object function scene:create( event ) sceneGroup = self.view physics.start() physics.pause() local mapData = require ("maps.level".. composer.getVariable("levelNumber")) map = tiled.new(mapData, "maps") start = map:findObject("start") player.circle = display.newCircle(start.x, start.y, 10) --non applicable code here sceneGroup:insert(map) sceneGroup:insert(player.circle) end local function reset() physics.removeBody(player.circle) player.circle.x = start.x player.circle.y = start.y timer.performWithDelay(30, goToScene2()) --transitions to scene2 end local function onObstacleCollision(event) --non applicable code here timer.performWithDelay( 10, reset ) end local function scene:show(event) ​ goToScene2() end

scene2:

function scene:hide( event ) local sceneGroup = self.view local parent = event.parent player.circle = display.newCircle(start.x, start.y, 10) parent.view:insert(player.circle) physics.addBody(player.circle, { density=1, friction=0, bounce=player.bounce }) physics.setGravity(player.Xgravity, player.Ygravity) physics.start() end

I’m not sure what the problem could be. Any thoughts? If something in my code doesn’t make sense, just let me know and I will do my best to clarify.

Thanks :slight_smile:

Sounds like your bodies are overlapping. 

If the bodies are overlapping, the physics engine will adjust whichever body it can, usually the first “dynamic” body it can, to achieve no overlap.

How would I go about fixing that? Is there a way to check for extra physics bodies? As far as I can tell from some testing I just did there aren’t any bodies that shouldn’t be there. Thanks for your help!

physics.drawMode(‘hybrid’) to see bodies, but that wont help if you’re placing two objects too near to eachother, because the adjustment will happen too fast to see.

One what to see if this is actually the issue to to make all the bodies that are having this issue ‘sensors’

obj.isSensor = true

If they stop moving, then you’ll know the problem was caused by overlap adjustments.

I set the object to sensor mode, and I noticed that when the new object was created it no longer moved, which makes me think the bodies were overlapping, but I can’t find what’s causing it in my code. Do you have any debugging tips for this issue?

Thanks! (you’ve been super helpful btw)

See this example that shows what I think is going on with a fix (although my fix might be a pixel off.)

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/01/overlappingBodies.zip

overlap.gif

There is still a little movement in the ‘fixed’ body (right bottom).  

My custom bodyshape should probably be this:

local bodyShape = { -44, -44, 44, -44, 44, 44, -44, 44 }

Thanks so much! After looking over your example, I realized that all I needed to do was add a timer before adding the new object. You’ve been super helpful and patient. Thanks again!

Sounds like your bodies are overlapping. 

If the bodies are overlapping, the physics engine will adjust whichever body it can, usually the first “dynamic” body it can, to achieve no overlap.

How would I go about fixing that? Is there a way to check for extra physics bodies? As far as I can tell from some testing I just did there aren’t any bodies that shouldn’t be there. Thanks for your help!

physics.drawMode(‘hybrid’) to see bodies, but that wont help if you’re placing two objects too near to eachother, because the adjustment will happen too fast to see.

One what to see if this is actually the issue to to make all the bodies that are having this issue ‘sensors’

obj.isSensor = true

If they stop moving, then you’ll know the problem was caused by overlap adjustments.

I set the object to sensor mode, and I noticed that when the new object was created it no longer moved, which makes me think the bodies were overlapping, but I can’t find what’s causing it in my code. Do you have any debugging tips for this issue?

Thanks! (you’ve been super helpful btw)

See this example that shows what I think is going on with a fix (although my fix might be a pixel off.)

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/01/overlappingBodies.zip

overlap.gif

There is still a little movement in the ‘fixed’ body (right bottom).  

My custom bodyshape should probably be this:

local bodyShape = { -44, -44, 44, -44, 44, 44, -44, 44 }

Thanks so much! After looking over your example, I realized that all I needed to do was add a timer before adding the new object. You’ve been super helpful and patient. Thanks again!