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
Jump to content
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.
@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.
Runtime:addEventListener( 'enterFrame', yourFunctionToMoveGroups )
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.
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.
Community Forum Software by IP.Board