Moving physics bodies around creates collision problems

Hi!

I’m trying to do some tests (a basic retro stlye platformer, think Mario).

I’m now in the process of making the level scroll with when the character is moving.

I have two displayGroups set up:

mapLayer - it hold the map (just one big image) and the collision rectagles (several)

playerLayer - holds the player

Before the map layer starts to move, it’s all working great, the player can jump around on the ground and on the platforms.

As soon as I start to move the map layer, the graphics move but it seems like the phyics bodies stay in place.

When I’m running Hybrid Mode it looks like the physics bodies are moving as well but they are not.

The first thing I tried was moving the mapLayer.x

When this didn’t work I tried making the map layer to a kinematic body and moving it using setLinearVelocity and the same problem occured.

I think it’s the same problem as in this thread: http://forums.coronalabs.com/topic/3070-serious-collision-bug-or-else-very-odd-behavior/?hl=%2Bghost+%2Bbody#entry15569

But it’s an old thread and doesn’t seem to have any solution in it

Any ideas?

Thanks in advance

There are a few problems with moving physics bodies. The first is that if you have collidable objects in separate display groups, then moving one group causes the physics to break in the way you’ve described. Nesting the 2 groups inside another one way fix this, but don’t hold me to that.

Also, how are you moving the objects? Are you just doing:

myObject.x = myObject.x + 1

or something similar, as you would with a normal display object?

I have always found that this causes problems, with the display object moving but the physics body staying in the same place.

Instead I use touch joints (you don’t actually need to touch them), and use the setTarget function to move them:

--create object and add touch joint local myObject = display.newImageRect("foo.png", 100, 100) myObject.touchJoint = physics.newJoint( "touch", myObject, myObject.x, myObject.y ) myObject.touchJoint.maxForce = 100000 myObject.touchJoint.frequency = 100 myObject.touchJoint.dampingRatio = 1 --where you would normally set the position of the object try this instead, --where x and y are the position you want to move to local x, y = myObject.x + 1, myObject.y - 1 myObject.touchJoint:setTarget( x, y )

I’ve found it to be far less prone to errors, although you’ll probably need to fiddle with the maxForce, frequency and dampingRatio variables to make the objects move in the way you want.

Thanks for the answer!

I tried nesting the display groups first, but that didn’t quite work for me.

I then started wokring with the touchJoints, I added them to th eplayer display object and that worked fine.

After that I tried to do the same thing to the whole mapLayer, insteadof every indiviual piece of the level.

This did not work at all. Im I not supposed to be doing that on a display group or am I doing something wrong?

Please see this document: http://docs.coronalabs.com/daily/guide/physics/physicsSetup/index.html

Keeping your objects in one display group will work best.

To elaborate on what Danny says, you can put objects in multiple display groups (for layering purposes)… you just can’t move the groups independently of each other. If you need to move the “world” as a whole, nest your display groups into a parent one and move them as an entire unit.

Brent

There are a few problems with moving physics bodies. The first is that if you have collidable objects in separate display groups, then moving one group causes the physics to break in the way you’ve described. Nesting the 2 groups inside another one way fix this, but don’t hold me to that.

Also, how are you moving the objects? Are you just doing:

myObject.x = myObject.x + 1

or something similar, as you would with a normal display object?

I have always found that this causes problems, with the display object moving but the physics body staying in the same place.

Instead I use touch joints (you don’t actually need to touch them), and use the setTarget function to move them:

--create object and add touch joint local myObject = display.newImageRect("foo.png", 100, 100) myObject.touchJoint = physics.newJoint( "touch", myObject, myObject.x, myObject.y ) myObject.touchJoint.maxForce = 100000 myObject.touchJoint.frequency = 100 myObject.touchJoint.dampingRatio = 1 --where you would normally set the position of the object try this instead, --where x and y are the position you want to move to local x, y = myObject.x + 1, myObject.y - 1 myObject.touchJoint:setTarget( x, y )

I’ve found it to be far less prone to errors, although you’ll probably need to fiddle with the maxForce, frequency and dampingRatio variables to make the objects move in the way you want.

Thanks for the answer!

I tried nesting the display groups first, but that didn’t quite work for me.

I then started wokring with the touchJoints, I added them to th eplayer display object and that worked fine.

After that I tried to do the same thing to the whole mapLayer, insteadof every indiviual piece of the level.

This did not work at all. Im I not supposed to be doing that on a display group or am I doing something wrong?

Please see this document: http://docs.coronalabs.com/daily/guide/physics/physicsSetup/index.html

Keeping your objects in one display group will work best.

To elaborate on what Danny says, you can put objects in multiple display groups (for layering purposes)… you just can’t move the groups independently of each other. If you need to move the “world” as a whole, nest your display groups into a parent one and move them as an entire unit.

Brent