problem with imageGroups as physics bodies

I have some imageGroups that form rectangular areas. I want those same rectangles to serve as static physics bodies. Since these are rectangles, I thought that I would be able to just write the addBody call without specifying a shape and have the outline of the body fit the imageGroup’s rectangle automatically. However, when I set the drawMode to “hybrid” to help to figure out why things weren’t working as expected, I discovered that the rectangular shape of the physics body is the right size, but it is centered about the top left corner of the imageGroup rather than its center.

I tried to setReferencePoint to display.centerReferencePoint in the imageGroup and a few related ideas, and none of them helped. Am I missing a straightforward way to get the physics body to match the imageGroup’s bounds, or am I going to have to resort to creating custom shapes for all such imageGroups?

Thank you in advance for any help you can provide this rather puzzled newbie. [import]uid: 129551 topic_id: 30355 reply_id: 330355[/import]

This might sound a bit silly, but what order do you perform these steps? Do you declare the physics bodies after or before adding them to the image group?

If swapping the order doesn’t help, a safe backup for rectangles is to just declare a polygonal body with four corners based on the rectangle’s width and height. I know this shouldn’t be necessary since addBody is supposed to trace the bounds automatically (and does in almost all cases) but if you need to go this route, it’s just a few more bits of code.

It’s very possible this is a minor bug too, but try this workaround for now.

Brent
[import]uid: 9747 topic_id: 30355 reply_id: 121625[/import]

Don’t worry about sounding silly, Brent. Since these were the first few lines I’d ever written with the physics engine, I could well have done something silly.

First I added the various frames from my imageSheet to the imageGroup. After the imageGroup was complete, I then declared the imageGroup itself as the physics body since it has the width and height I want the body to have. I chose not to make any of the individual images in the group into physics bodies since I presumed that my app would perform better if I had fewer bodies on which to perform collision detection. [import]uid: 129551 topic_id: 30355 reply_id: 121637[/import]

OK, that answers it. You can’t apply physics bodies to groups as a whole, or at least it won’t work as expected if you try. But, you can certainly create one big rectangular body of the same size and add it to the group. You’re correct in wanting to reduce the overall number of bodies, only that you need to still apply them to objects, not groups.

Best of luck!
Brent
[import]uid: 9747 topic_id: 30355 reply_id: 121684[/import]

Ah, that explains it. Adding an invisible rectangle in the same spot worked perfectly, and I thank you for the information. [import]uid: 129551 topic_id: 30355 reply_id: 121725[/import]

This might sound a bit silly, but what order do you perform these steps? Do you declare the physics bodies after or before adding them to the image group?

If swapping the order doesn’t help, a safe backup for rectangles is to just declare a polygonal body with four corners based on the rectangle’s width and height. I know this shouldn’t be necessary since addBody is supposed to trace the bounds automatically (and does in almost all cases) but if you need to go this route, it’s just a few more bits of code.

It’s very possible this is a minor bug too, but try this workaround for now.

Brent
[import]uid: 9747 topic_id: 30355 reply_id: 121625[/import]

Don’t worry about sounding silly, Brent. Since these were the first few lines I’d ever written with the physics engine, I could well have done something silly.

First I added the various frames from my imageSheet to the imageGroup. After the imageGroup was complete, I then declared the imageGroup itself as the physics body since it has the width and height I want the body to have. I chose not to make any of the individual images in the group into physics bodies since I presumed that my app would perform better if I had fewer bodies on which to perform collision detection. [import]uid: 129551 topic_id: 30355 reply_id: 121637[/import]

OK, that answers it. You can’t apply physics bodies to groups as a whole, or at least it won’t work as expected if you try. But, you can certainly create one big rectangular body of the same size and add it to the group. You’re correct in wanting to reduce the overall number of bodies, only that you need to still apply them to objects, not groups.

Best of luck!
Brent
[import]uid: 9747 topic_id: 30355 reply_id: 121684[/import]

Ah, that explains it. Adding an invisible rectangle in the same spot worked perfectly, and I thank you for the information. [import]uid: 129551 topic_id: 30355 reply_id: 121725[/import]