Implementing shape groups with layers

Official forum for the Chipmunk2D Physics Library.

Implementing shape groups with layers

Postby Phillemann » Fri Feb 19, 2010 12:04 pm

Hi

I'm currently transferring some collision/physics code from bullet to chipmunk, but I'm having a hard time with the cpShape::group and cpShape::layers attribute. Currently I've got the following system:

  • A shape can be added to zero or more groups. When a shape is created, it doesn't belong to any group and thus doesn't collide with any other shape.
  • A group can be set to collide with zero or more other groups.
  • A group can collide with itself, but doesn't have to.

So a typical scenario would be: 3 Groups: Players, enemies and bullets. The player shapes collide with bullet shapes, the enemy shapes collide with bullet shapes and the player shapes can collide with other players (to get some kind of shared power or something).

The problem is: How do I map this scenario to the groups and layers? I don't want transitivity as in: The players collide with the bullets, the bullets collide with the enemies, but the enemies should not collide with the players.
Phillemann
 
Posts: 1
Joined: Fri Feb 19, 2010 9:18 am

Re: Implementing shape groups with layers

Postby slembcke » Fri Feb 19, 2010 1:06 pm

You can use layers as rules. Think of each layer as a rule that two things collide, and you get to define 32 rules. (more if you redefine cpLayers to be a bigger int type)

Your scenario has two rules and needs two bits. Players collide with bullets (1), and Enemies collide with bullets (2). Players would use 1 for their layers, enemies 2 and bullets 3 (1 | 2). Players don't collide with enemies because 1 & 2 is 0. You'll need to use Chipmunk groups or collision callbacks to exclude collisions within a rule type if you don't want them to collide.

Make sense?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm


Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 0 guests

cron