Moving group of phyics objects

Good day, I’m trying to move a group of physics objects, but it seems that the physics body, and the object itself is not the same? How can I move a group with multiplte physics body in it

A bit more information would be helpful.

Once you give a physics body to a display object, then that object is pretty much owned by the physics engine. You can still move display objects and display groups around, but this may result in weird and jittery motion as the physics engine is trying to control the objects also.

I’m not sure about your particular case, but you should be able to just move the group and everything will work just fine. If you need to move a group of objects and not an entire display group, I’d just loop through the objects and move them that way. Note, however, that when I say move, I mean “object.x = value” kind of moving. Using transitions with dynamic physics objects is a no go.

for example objects A,B,C,D ha physics body. they are all inserted on a group named as MyGroup, once I modify the position of the MyGroup group, it seems that A,B,C,D’s Physics body are left behind, while their game object position is moved.

I’m presuming that you are using hybrid draw mode, which shows that the bodies and the assets don’t match up?

If your code is as simple as (or something along these lines):

MyGroup.x = MyGroup.x + 100

Then they really should move along with the group.

I’m using transition.to? WHy doesn’t it work?

https://docs.coronalabs.com/guide/physics/limitations/index.html#transitions

Thanks for the help

@vonncc123 You can move the display group with physics objects in it but you will need to move all the other display groups with physics objects in them as well.  Don’t use transition, use hard coordinates but you can use an enterFrame function to simulate transitions.

[lua]

Runtime:addEventListener( ‘enterFrame’, yourFunctionToMoveGroups )

[/lua]

Let us know if you have any hard code you would like us to take a look at.

local rect = display.newRect(\_W/2,\_H/2,50,50) physics.addBody(rect) rect.bodyType = "dynamic" local rect2 = display.newRect(\_W/2,\_H/2 + 70,50,50) physics.addBody(rect2) rect2.bodyType = "static" group:insert(rect) local function moveMe() group.x = group.x + 1 end

Here is my code, the group is still not falling, the object is moving to the right, but it still doesn’t fall.

What are you trying to achieve?

Based on your code, all you do is create two objects, one dynamic, one static, and place the dynamic object in the group. Then you just move the group to the right. So, what’s wrong?

Yup, exactly what you said, but the moving group’s objects are not falling, they’re just moving to the right. (Assume that the group has may objects), when I do it 1 by 1, the moving objects are falling, but when I do it as a group, they don’t fall

What I’m trying to achieve is that, the Group will fall as one.

My suggestion don’t move the group.  

I know it would be nice, but this simply won’t work the way you want it to with with physics bodies.

You need move the objects individually and keep all your groups aligned, otherwise your physics bodies will misbehave.

To @roaminggamer’s point, as far as I’ve understood, you can move a group with physics objects in it, but you need to be careful about it. The physics simulations will run correctly since all objects in the said group will be offset by the same amount. However, having multiple groups with physics objects in them will likely run into issues.

Taking your sample code and adding the necessary bits there, i.e.
 

local physics = require("physics") physics.setDrawMode( "hybrid" ) physics.start() local group = display.newGroup() local rect = display.newRect( 130, 100, 50, 50 ) physics.addBody( rect ) group:insert( rect ) local rect2 = display.newRect( rect.x+60, 100, 50, 50 ) physics.addBody( rect2, "static" ) group:insert( rect2 ) local function moveMe() group.x = group.x + 1 end Runtime:addEventListener( 'enterFrame', moveMe )

does result in the dynamic object(s) falling, as they should, and every display object on the screen moves right as per moveMe function.

If you want for the group to fall, then you’d have to move the group down (if I am understanding you correctly). If you are having problems with not all objects falling, then that’s due to one of them being a static object, which does not react to gravity.

That’s very unfortunate, Thanks for the answers though, had to do it manually. Had to manually move the x and y axis of everything, but the scaling will be as a group

A bit more information would be helpful.

Once you give a physics body to a display object, then that object is pretty much owned by the physics engine. You can still move display objects and display groups around, but this may result in weird and jittery motion as the physics engine is trying to control the objects also.

I’m not sure about your particular case, but you should be able to just move the group and everything will work just fine. If you need to move a group of objects and not an entire display group, I’d just loop through the objects and move them that way. Note, however, that when I say move, I mean “object.x = value” kind of moving. Using transitions with dynamic physics objects is a no go.

for example objects A,B,C,D ha physics body. they are all inserted on a group named as MyGroup, once I modify the position of the MyGroup group, it seems that A,B,C,D’s Physics body are left behind, while their game object position is moved.

I’m presuming that you are using hybrid draw mode, which shows that the bodies and the assets don’t match up?

If your code is as simple as (or something along these lines):

MyGroup.x = MyGroup.x + 100

Then they really should move along with the group.

I’m using transition.to? WHy doesn’t it work?

https://docs.coronalabs.com/guide/physics/limitations/index.html#transitions