groups and physics bodies

I’m currently working on creating hitboxes for characters and enemies that are not the bounding box of the sprite.
I’ve created a group for each character state (idle, attack, etc). In each group there is a sprite that plays the appropriate animation and a roundedRect display object with an attached physics body. The group is then moved to the appropriate position on the screen.

It seems that the collision detection does not take into account the transform of the group and just takes the position of the roundedRect in its local coordinates.

local physRect = display.newRoundedRect( -95, -5, 90, 35, 10 ) physRect:setFillColor( 255, 255, 255 ) physRect.alpha = 0.5 physics.addBody( physRect, "dynamic", { isSensor = true, density = 10.0, friction = 0.5, bounce = 0.2 } ) physRect.isBullet = true player[i]:insert( 1, physRect ) physics.addBody( player[i][1], "static", { density = 10.0, friction = 0.5, bounce = 0.2 } )

In the above code player[i] is the group.

The enemies’ have dynamic sensor body types and that used to work fine before I restructured things with groups. None of the combinations of body types with and without sensor flag set that I’ve tried seem to work. [import]uid: 6259 topic_id: 1663 reply_id: 301663[/import]

Ignore the second physics.addBody at the bottom. On that note, I cannot edit the post because the edit screen does not list Game Edition in the categories drop-down list. [import]uid: 6259 topic_id: 1663 reply_id: 4823[/import]

I think there’s some issue with turning vector objects into physics bodies, or perhaps with transforms in those cases – when we figure out what’s going on, we will post it as a “known issue” and fix it. In the meantime I’ve filed bug #667.

As a workaround theory, does it work properly when you substitute a similarly-sized PNG file for the rounded rect object?

[import]uid: 3007 topic_id: 1663 reply_id: 4861[/import]

Substituting a png matching the dimensions of the rounded rect produces the same results. Its coordinates are (28,0) which is near the top left corner, not center bottom where the group is located. [import]uid: 6259 topic_id: 1663 reply_id: 4907[/import]

OK, it does sound like a problem specifically with groups! My guess is that it’s due to a change in internal coordinate systems when an object is grouped.

Looking into this now… [import]uid: 3007 topic_id: 1663 reply_id: 4957[/import]

After some testing, I don’t think there is a bug in this (specific) case. Here’s what’s going on:

Ignoring the last line of your code, it still looks like you’re creating a physics body and THEN adding it to a display group. This will generally not work properly unless ALL your physics objects are added to that same group, as in the “EggBreaker” sample code, which uses a global display group for a “moving camera”.

The reason (as you may have guessed) is that Box2D assumes a global coordinate system for all the objects controlled by the engine. But objects added to a display group inherit the local coordinate system of the group.

But there’s an easy solution, if you want to use display groups as physics objects: construct your entire group first and THEN do physics.addBody() using the entire group as the target. At that point, you’re treating the entire group as an object in the global coordinate system, and it’ll work as expected.

The other thing to note, if you’re using our “automatic rectangular boundary” feature as above, is that it assumes that your rectangular target object is CENTERED around its local reference point. This is true by default for any new bitmap or vector object, but often not true for arbitrary display groups. Therefore, in the example above, be sure to set the local coordinates of your bounding-rectangle object to (x=0, y=0) before turning that group into a physics object.

(An alternative is to specify the collision boundaries manually, using a clockwise set of points, but in this case it’s probably easier to just center the rectangle and use the convenience method.) [import]uid: 3007 topic_id: 1663 reply_id: 4962[/import]

Thanks for the reply! If using the group’s local coordinates is the intended design then I will work around it. I was trying to use something visual so I can see the hitbox of the character.

Seems like the solution in this case is to specify a shape for the physics body. Adding a body to the whole group would not be appropriate because the bounding box of the image is larger than the hitbox. [import]uid: 6259 topic_id: 1663 reply_id: 4965[/import]

As an aside, we have visible hitboxes listed as one of the unfinished to-dos in our Game Edition documentation (known as “DebugDraw” mode in Box2D). [import]uid: 3007 topic_id: 1663 reply_id: 4967[/import]