Page 1 of 1

Creating curved boundaries/lines

Posted: Wed May 06, 2009 2:10 pm
by gibson10ma
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

Re: Creating curved boundaries/lines

Posted: Wed May 06, 2009 2:29 pm
by maximile
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.

Re: Creating curved boundaries/lines

Posted: Wed May 06, 2009 4:15 pm
by gibson10ma
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?

Re: Creating curved boundaries/lines

Posted: Wed May 06, 2009 4:20 pm
by slembcke
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.