Moving objects as a group + collision...

I’ve posted this in the game edition forums and got no replies… I think it’s a bug. Basically I have a rect that has collisions (static or dynamic, doesn’t matter) and if I put it in a group, and move it using group.x I can’t collide with the rect at it’s new location, but if I move my character to his old location I collide with a invisible box… the only way to move the object in a group and update collisions would be using group[1].x = 100 but that would move one object and not the whole group. The only other solution I have is using:

for i = 1,group.numChildren do
group[i].x = 100 + (group.numChildren*5)
end

that will basically move each one farther to the right, which won’t work for what I’m doing, I need them to all move at once (without for loops) cause I need to call transitions using the group and have the collisions work. Understand?

Anyone know if this is a bug? [import]uid: 9033 topic_id: 2087 reply_id: 302087[/import]

This is not actually a bug, but see the explanation here:

http://developer.anscamobile.com/content/game-edition-box2d-physics-engine#physics.setDrawMode

_When working with Corona display groups and Box2D, it is important to keep in mind that Box2D expects all physics objects to share a GLOBAL coordinate system. This means that a set of ungrouped Corona objects will work well, and a set of objects that are all added to the SAME display group should also work, since they will share the internal coordinates of that group – this is demonstrated in the “EggBreaker” sample code, which uses a single moving display group to create a “moving camera” effect.

However, unexpected results may occur if physical objects are added to DIFFERENT display groups, especially if the groups are then moved in different directions. The “hybrid” draw mode makes these issues much easier to diagnose, since it lets you overlay the physics engine’s collision boxes and shapes on top of the Corona renderer._

Also, if you’re trying to treat a group of objects as a single physical body, you may want to re-center the objects in that group around (0,0) in the group’s internal coordinates, since the simpler body constructors assume a centered object. You can use physics.setDrawMode( “hybrid” ) to see a wireframe overlay of where the physics engine thinks the bodies are. If there’s an alignment problem, either center your objects within the group, or pass the desired boundary coordinates to Box2D as a table of x,y values:

http://developer.anscamobile.com/content/game-edition-physics-bodies#Polygon_bodies
[import]uid: 3007 topic_id: 2087 reply_id: 6243[/import]

So you’re saying I should put all objects I want to collide in the same group? What if I have 2 groups (say enemy and player) and they’re in a group together called mainGroup, would they collide? [import]uid: 9033 topic_id: 2087 reply_id: 6244[/import]

> So you’re saying I should put all objects I want to collide in the same group?

Basically, yes.
> What if I have 2 groups (say enemy and player) and they’re in a group together called mainGroup, would they collide?

Probably, but the behavior will generally be easier to understand if you simplify your group structure.

You are correct that you can treat a “player” or “enemy” group as a single Box2D body, since a display group is just another kind of display object as far as Corona is concerned. But the question is: do you really want to put the enemy and player into their own little groups?

Recall that Box2D is a “rigid-body engine”, so it assumes that physics bodies in the world have no moving parts and cannot change shape. Therefore, to make a more complicated physics object, for example a thing with multiple parts or spinning wheels, the standard solution is to use joints to attach multiple bodies to each other:

http://developer.anscamobile.com/content/game-edition-physics-joints

Or, for a complicated shape that doesn’t need separate moving parts, you can make a more complex Box2D outline by adding multiple body-element definitions to the same body, each with its own physical properties:

http://developer.anscamobile.com/content/game-edition-physics-bodies#Complex_body_construction
[import]uid: 3007 topic_id: 2087 reply_id: 6246[/import]

Hmmm. The easiest way to put it would be like this:

Enemy has 2 parts, body and a radar (the radar is a line object with simple box collisions) and the radar is infront of the enemy. So I put them in a group so I could move one group and it would move both the enemy and his radar. The player doesn’t have to be in his group, but he can’t be in the same group as the enemy, because then the player would move with the enemy. Here’s my group structure:
Enemy
>> Main Body (static, but it is a body)
>> Radar (It’s also static and a body)
Player
>> Player object (also a body, but he’s dynamic

Should I make the enemy group a body instead of objects inside the group?

I hope you understand xD [import]uid: 9033 topic_id: 2087 reply_id: 6248[/import]

Yes, in this case you should make the enemy either one body or two joined bodies, not a display group.

It sounds like you want specific collision information from the “radar” part, which means that you probably want two joined bodies. You can use a “weld” joint if you simply want to attach one body to another without pivoting or moving. [import]uid: 3007 topic_id: 2087 reply_id: 6249[/import]

Ah figured it out… just removed the radar and made my group have a poly body so I could carve out the radar as a sensor… great :slight_smile: [import]uid: 9033 topic_id: 2087 reply_id: 6250[/import]

how to move the camera?
[import]uid: 40515 topic_id: 2087 reply_id: 27771[/import]

Apologies for sorta resurrecting an old thread, but I’m having the exact same issue.

However I simply can’t just have 2 or more joined parts - I’d need each object to be a display object.

Has anyone got a workaround regarding this?
Thanks in advance [import]uid: 40538 topic_id: 2087 reply_id: 29377[/import]

Yeah, I have same Q as Wazzup…

To explain again, cause joints wont work for me either…

I want to add several display objects to one group and then use the group to move them.

I could move them individually using a loop, but the more complex the group of objects gets the less I want to do this. Surely I should be able to add them all to the same group and just translate the group?

Whats happening at the moment is, if I create a single display group, create 3 display objects, add physics to the objects, then add the objects to the group… and then move the group… the objects move “visually” in the simulator, but the collisions still occur at original position the objects were created at… and not at the groups new position.

NOTE: in physics hybrid mode… it “appears” that the collision spheres have moved but they have not.

Anyone any ideas?
Cheers
R [import]uid: 12583 topic_id: 2087 reply_id: 104253[/import]

rgreene,

When you say: “but the collisions still occur at original position the objects were created at,” what are your objects colliding with?

Let’s say you have 3 orbs, and they need to collide with a square. Add your orbs to “orbGroup,” then add your square to “squareGroup.” Now add orbGroup and squareGroup to your masterGroup.

I made a skateboarding halfpipe game which has many groups, one for the skater, (board and player objects) one for each parallax layer, one for the halfpipe, (about 40 objects) and so on. All of these groups were added to my masterGroup and the camera controlled the y position of the masterGroup to simulate the player going up in the air or falling etc. Is that the kind of thing you guys are looking to acheive? [import]uid: 67933 topic_id: 2087 reply_id: 104261[/import]

Hey Spider

I have a player object on the left of screen and 3 coins on the right of screen.

My player is in a player display group

My coins are all in a coins display group

The player and coins display groups are added to a game display group which I am returning

Physics has been added to the player and coins individually.

Instead of translating the coins individually, I was hoping to just translate the coins group toward the player. However, the physics bodies associated with each coin seems to remain offset to the images being displayed… if that makes sense!

Put it this way:

Create a display group at 0, 0
Create a coin at 240, 160 and add physics to it
Add coin to display group
Set displaygroup.x = 100
Coin “image” has now moved +100 in x to 340, 160… but collisions occur at 240, 160

Egg breaker sample seems to work perfect… I must be missing something really stupid! Or the multiple display groups is an issue? (egg Breaker only has one)

Cheers for any help
R
[import]uid: 12583 topic_id: 2087 reply_id: 104434[/import]

Came acroos this

http://developer.anscamobile.com/forum/2012/03/14/intereseting-physics-when-using-displaynewgroup

I am creating a table of coins using a for loop and indexing… Does/Should it matter?

I basically end up with a table called Coins… with 3 elements… each with physics added and each put in the coins display group.

[lua]local rowY = 160
local colX = 240

for i=1,3 do

coin[i] = display.newImageRect( “coin.png”, 30, 30 )
coinGroup:insert( coin[i] )

physics.addBody( coin[i], “static”,{ isSensor = true, density = 0, friction = 0, bounce = 0, radius=15 } )

coin[i].myName = “coin”
coin[i].y = rowY
coin[i].x = colX

colX = colX + 35
end[/lua]

[import]uid: 12583 topic_id: 2087 reply_id: 104436[/import]

Found the answer… doesnt work for me… but an answer all the same.
I’d have to add the player and coins to the one group… but then moving that group would also move the player…

http://developer.anscamobile.com/forum/2011/11/13/physics-not-updating-when-moving-group

Found alot of posts on the forums with similar issues… but all end with no answer. This one seems to clear it up. So hopefully others with same issue will find it here

Thanks
R [import]uid: 12583 topic_id: 2087 reply_id: 104447[/import]

I see the problem and sadly no, I don’t think it’s possible. My skater example worked because I was only moving the masterGroup and not any sub groups.

That said, there may still be a work around. I understand you don’t want to move each individual object, but depending on your needs, you could simulate a similar action by iterating through the group and setting the linearVelocity of each object to a given amount.

For instance, I made a simple endless runner in which a car moving vertically via tilt controls, has to avoid objects which spawn off the top of the screen. Each of the obstacles is added to a group, and their speed is amended universally by iterating the table and changing the velocity, like so:

  
local function setSpeed()  
 if(table.getn(cTable) \>= 1) then  
 for i=1, table.getn(cTable) do  
 cTable[i]:setLinearVelocity( 0, scrollSpeed )  
 --print(scrollSpeed)  
 end  
 end  
end  
Runtime:addEventListener("enterFrame", setSpeed)   
  
local function changeSpeed()  
 scrollSpeed = mRand(10,100)  
 return scrollSpeed;  
end  
  
stmr = timer.performWithDelay(3000, changeSpeed, 0)  

Don’t know if that will meet your requirements but may be worth baring in mind if you’re trying to “push” objects collectively and still keep their physics functionality intact.

Dan [import]uid: 67933 topic_id: 2087 reply_id: 104466[/import]

Cheers Dan… thats pretty much what I’ve resorted to since I couldnt get the display groups working.

Its a shame really, cause life would be much easier if we could just translate the group eh. And thinking about it, my coin groups parent is at 0 and doesnt move… so you would think it would all work as if you had only one group… because there are no funky coords being inherited from above!

Thanks for the help mate… and the insight… appreciate it
Richie [import]uid: 12583 topic_id: 2087 reply_id: 104498[/import]