Page 1 of 1

creating a floor/border with cpSpaceGetStaticBody

Posted: Wed May 02, 2012 12:03 pm
by itsthejonnyboy
hi all,

first post :) hello, everyone,

i'm very new to Chipmunk, and currently only using it for a very small labyrinth game-style widget, with balls rolling around a bordered box. i implemented this fine using Chipmunk-Pro, so decided to go back and do the same using vanilla Chipmunk.

Everything has gone fine except for creating the border for the game board. i've tried everything, but nothing seems to create a shape. for the time being, i tried just creating a single "floor" shape, but no luck with this either:

Code: Select all

self.space = cpSpaceNew();
cpSpaceSetGravity(self.space, cpv(0, kGravityMultiplier));

cpShape *shape;

// shape
shape = cpSpaceAddShape(self.space, 
                      cpSegmentShapeNew(cpSpaceGetStaticBody(self.space), 
                                        cpv(0, 0), 
                                        cpv(320, 0), 
                                        0.0f)
                      );
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);
ignore the hardcoding of sizes, i'm just trying to get some effect first, but nothing. i have all my balls set to have no specific layer, so that's not the problem.

this seems like such a simple thing, and is so easy in Chipmunk Pro that i have no idea where i'm going wrong here. also, it seems rather odd there isn't a more convenient way to add a rectangular bounds around a cpSpace... anyway, hope some can help!

thanks

Re: creating a floor/border with cpSpaceGetStaticBody

Posted: Wed May 02, 2012 3:30 pm
by slembcke
I don't see any reason why that wouldn't be working. There isn't really any tricks to it: https://github.com/slembcke/Chipmunk-Ph ... midStack.c (Setting the layers, elasticity and friction are optional)

Dumb question, are you sure you are adding the shapes to the same space instance? Do the other shapes simply not collide with the ground or something?

Re: creating a floor/border with cpSpaceGetStaticBody

Posted: Wed May 09, 2012 8:35 am
by itsthejonnyboy
hi,

ok! solved! i ended up completely rewriting things using the source that you posted, so thanks for that :) i think that the problem was that i wasn't adding both the body AND the shape to the space... is that handled automagically by ObjectiveChipmunk?

in any case, glad to have the widget working now with no need for a license fee!

thanks again!

J

Re: creating a floor/border with cpSpaceGetStaticBody

Posted: Wed May 09, 2012 9:45 am
by slembcke
Hmm. Not quite sure I understand what your issue was but glad you got it working. Objective-Chipmunk doesn't really make anything automatic no. It just provides an Objective-C API on top of the C one and I use that to simplify as many things as possible like memory management or working with composite objects.