Creating curved boundaries/lines

Official forum for the Chipmunk2D Physics Library.

Creating curved boundaries/lines

Postby gibson10ma » Wed May 06, 2009 2:10 pm

Hi all,

I would think someone would have asked this by now, but I couldn't find anything in a search. Apologies if I missed something.

What is the best method for creating a curved boundary? I've attached an image illustrating some red balls in the type of space I am trying to create.

I am currently using the following code to create 4 static walls, but don't see an easy way to define a curved line.

// bottom
shape = cpSegmentShapeNew(staticBody, cpv(0,0), cpv(wins.width,0), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);

// top
shape = cpSegmentShapeNew(staticBody, cpv(0,wins.height), cpv(wins.width,wins.height), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);

// left
shape = cpSegmentShapeNew(staticBody, cpv(0,0), cpv(0,wins.height), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);

// right
shape = cpSegmentShapeNew(staticBody, cpv(wins.width,0), cpv(wins.width,wins.height), 0.0f);
shape->e = 1.0f; shape->u = 1.0f;
cpSpaceAddStaticShape(space, shape);

Do I just have to make lots of lines to approximate the curves? Can I create a polygon?

-Andrew
gibson10ma
 
Posts: 3
Joined: Wed Apr 22, 2009 5:49 pm

Re: Creating curved boundaries/lines

Postby maximile » Wed May 06, 2009 2:29 pm

For external (convex) corners, just use a circle shape.

The internal corners will be tricky, because polygons must be convex. Line segments would probably be easiest, perhaps with a thickness if things are moving quickly.
maximile
 
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK

Re: Creating curved boundaries/lines

Postby gibson10ma » Wed May 06, 2009 4:15 pm

maximile wrote:For external (convex) corners, just use a circle shape.

The internal corners will be tricky, because polygons must be convex. Line segments would probably be easiest, perhaps with a thickness if things are moving quickly.


Does the thickness help to avoid the objects falling through the boundaries?
gibson10ma
 
Posts: 3
Joined: Wed Apr 22, 2009 5:49 pm

Re: Creating curved boundaries/lines

Postby slembcke » Wed May 06, 2009 4:20 pm

Just approximate the curve using short line segments. Depending on the radius you could get away with a fairly chunky collision curve and not many people are going to notice. If you want perfectly round convex sections, use a circle. That is the best you can do at the moment.

I would also recommend giving your segments at least some radius like maximile suggested. It makes the gaps in the line segments less noticeable and smoother. If you don't need an inside/outside just offset the line segments a little to compensate for the thickness.
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