creating a floor/border with cpSpaceGetStaticBody

Official forum for the Chipmunk2D Physics Library.
Post Reply
itsthejonnyboy
Posts: 2
Joined: Wed May 02, 2012 9:09 am
Contact:

creating a floor/border with cpSpaceGetStaticBody

Post 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
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: creating a floor/border with cpSpaceGetStaticBody

Post 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?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
itsthejonnyboy
Posts: 2
Joined: Wed May 02, 2012 9:09 am
Contact:

Re: creating a floor/border with cpSpaceGetStaticBody

Post 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
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: creating a floor/border with cpSpaceGetStaticBody

Post 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.
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 18 guests