Multigrab and static objects (Objective Chipmunk)

Official forum for the Chipmunk2D Physics Library.
Post Reply
Derek Huby
Posts: 6
Joined: Wed Jan 25, 2012 11:31 am
Contact:

Multigrab and static objects (Objective Chipmunk)

Post by Derek Huby »

I have a scene with a set of game objects resting on a static shape (the floor). I've implemented multigrab so that I can move the game objects around, and that works fine. The problem is that when I touch on the 'floor' object, the game objects fall straight through it!

I've tried reading up on this, and found plenty relating to the 'old' method of doing this, cpMouse. With this approach, it was necessary to set the GRABABLE_MASK_BIT on the layers property of shapes that you wanted to ignore when clicking. Is there something similar that I should be doing with multigrab?

Thanks in anticipation...
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Multigrab and static objects (Objective Chipmunk)

Post by slembcke »

The issues is that you are creating a constraint between two infinite mass objects (the ground and the control body used by ChipmunkMultiGrap). When that happens, you cause a lot of the properties of the two bodies to end up as NaNs and all sorts of bad things happen. So that's what setting up the filtering using GRABABLE_MASK_BIT does, is prevents those objects from being grabbed in the first place.

I should add an assertion for that. I could have it ignore grabs to infinite mass bodies as well, but then it's possible that you might have an infinite mass object cover up a grabable one, preventing it from being selected.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Derek Huby
Posts: 6
Joined: Wed Jan 25, 2012 11:31 am
Contact:

Re: Multigrab and static objects (Objective Chipmunk)

Post by Derek Huby »

Thanks for the swift reply! I think I realised that I had to do something about setting the appropriate bit, but my problem is that I don't know how to do it! :cry:

Here's what I've tried so far.

First, I tried doing an XOR on the layers property:

Code: Select all

ground.layers ^= GRABABLE_MASK_BIT;
- like the example on p.433 of Learning Cocos 2D by Rod'n'Ray. Now, this doesn't work for the very good reason that GRABABLE_MASK_BIT isn't defined. So, I dug around a little and found that the definition of GRABABLE_MASK_BIT was in the old cpMouse header file, and I copied that into my own header file:

Code: Select all

#define GRABABLE_MASK_BIT (1<<31)
#define NOT_GRABABLE_MASK (~GRABABLE_MASK_BIT)
Nope - no good. I'm still stuck I'm afraid, although I'm probably missing something obvious; but in Objective Chipmunk, how exactly do I set the bit mask to make sure that static objects aren't grabbed?
Derek Huby
Posts: 6
Joined: Wed Jan 25, 2012 11:31 am
Contact:

Re: Multigrab and static objects (Objective Chipmunk)

Post by Derek Huby »

I've worked out how to do this... let me know if the following is wrong, but it seems to work.

Basically, we've got 32 bits (layers) to play with, and we choose how we want to use them. In the simple example I'm working on, I've got static objects and movable objects which need to be able to interact, and a multiGrab thingy that needs to interact with the movable objects only. So I could set up just two layers:

Code: Select all

enum {
	PhysicsLayerDefault = (1<<0),
	PhysicsLayerGrab = (1<<31),
};
Now, if I want an object to interact with others and to be grabable, I can just set all of its bits to 1:

Code: Select all

someShape.layers = CP_ALL_LAYERS;
Now, if I need an object that can interact with others without being grabable, I can just add it to the first layer:

Code: Select all

someShape.layers = PhysicsLayerDefault;
- or I could add it to all of the layers as before, and then switch off the leftmost bit with a bitwise XOR, like I tried earlier.

And finally, when I add the multiGrab object, I set its layers to be PhysicsLayerGrab only. It's nice when you figure things out, isn't it?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Multigrab and static objects (Objective Chipmunk)

Post by slembcke »

Yup. That's the general idea.

I have a little description of how you can use the layers as rules in the docs here:
http://chipmunk-physics.net/release/Chi ... -Filtering

I've thought about making a little API to help with that. It's a little confusing to do on paper and type in lists of hex numbers with specific bits set. I've done it a couple of times now though. It's pretty handy.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests