newbie question

Official forum for the Chipmunk2D Physics Library.
Post Reply
funkaster
Posts: 7
Joined: Mon Dec 22, 2008 5:22 pm
Contact:

newbie question

Post by funkaster »

I've been reading the docs, the examples and trying to digest everything, but I think it's going to take a few more hours :-D
anyway... I have a question regarding something I might be doing wrong. I'm trying to create a very simple first demo: a box with a initial horizontal velocity inside a space with gravity, and the space is bounded by walls (defined by the window box), so far everything is working except that the box is falling down through the bottom border and not bouncing as I would have expected. this is my code (note: 0.0f is top-left, frame is my window frame):

Code: Select all

        // Initialization code
		space = cpSpaceNew();
		space->iterations = 10;
		space->elasticIterations = 10;
		space->gravity = cpv(0.0f, 300.0f);
		
		// add the walls
		wall = cpBodyNew(INFINITY, INFINITY);
		// WEST
		shape = cpSegmentShapeNew(wall, cpv(0.0f, 0.0f), cpv(0.0f, frame.size.height), 0.0f);
		shape->e = 1.0; shape->u = 1.0;
		cpSpaceAddStaticShape(space, shape);
		// EAST
		shape = cpSegmentShapeNew(wall, cpv(frame.size.width, 0.0f), cpv(frame.size.width, frame.size.height), 0.0f);
		shape->e = 1.0; shape->u = 1.0;
		cpSpaceAddStaticShape(space, shape);
		// SOUTH
		shape = cpSegmentShapeNew(wall, cpv(0.0f, frame.size.height), cpv(frame.size.width, frame.size.height), 0.0f);
		shape->e = 1.0; shape->u = 1.0;
		cpSpaceAddStaticShape(space, shape);
		// NORTH
		shape = cpSegmentShapeNew(wall, cpv(0.0f, 0.0f), cpv(frame.size.width, 0.0f), 0.0f);
		shape->e = 1.0; shape->u = 1.0;
		cpSpaceAddStaticShape(space, shape);

		// add the main body (a simple 50x50 square)
		cpVect verts[4] = {
			cpv(10.0f, 10.0f),
			cpv(60.0f, 10.0f),
			cpv(60.0f, 60.0f),
			cpv(10.0f, 60.0f)
		};
		cpBody *body = cpBodyNew(50.0f, cpMomentForPoly(50.0f, 4, verts, cpvzero));
		body->p = cpv(35.0f, 35.0f);
		body->v = cpv(100.0f, 0.0f);
//		body->w = 10.0f;
		
		cpSpaceAddBody(space, body);
		whiteSquare = cpPolyShapeNew(body, 4, verts, cpvzero);
		whiteSquare->e = 1.0f; whiteSquare->u = 0.0f;
		cpSpaceAddShape(space, whiteSquare);
and this is the function that draws the box each iteration (working on an iPhone):

Code: Select all

	cpSpaceStep(space, 1.0f/60.0f);
	CGContextRef ctxt = UIGraphicsGetCurrentContext();
	CGContextClearRect(ctxt, rect);
	CGContextSetRGBFillColor(ctxt, 0.0, 0.0, 1.0, 1.0);

	cpBody *body  = whiteSquare->body;
	cpVect *verts = ((cpPolyShape *)whiteSquare)->verts;
	for (int i=0; i < 4; i++) {
		cpVect v = cpvadd(body->p, cpvrotate(verts[i], body->rot));
		if (i == 0)
			CGContextMoveToPoint(ctxt, v.x, v.y);
		else
			CGContextAddLineToPoint(ctxt, v.x, v.y);
	}
	CGContextClosePath(ctxt);
	CGContextFillPath(ctxt);
What am I doing wrong regarding the walls? I created a body with infinite mass and inertia, added segment shapes to each side of the window, added the shapes to the space... but the object is not bouncing... I think I'm missing something really basic here :-P

regards,
rolando./
Sarge
Posts: 6
Joined: Thu Dec 04, 2008 1:47 pm
Contact:

Re: newbie question

Post by Sarge »

Try changing the direction of construction (winding) of your outer frame, also that of the whiteSquare poly (try the latter first). Also, gravity is a bit high, as is your initial velocity - maybe your timestep is too long for the dynamics to be caught. Also try setting the mass of the poly via the density - use cpPolyMass - so that mass and inertia tally. Finally, I'm not sure what effect a coefficient of friction of zero will have (as you set for whiteSquare) - try values away from the extremes. Personally I try to keep to SI units for everything (including dimensions) so that all properties are balanced, then use display scaling to view.

Sorry I can't try out your code - I use Delphi and OpenGL. Good luck.
funkaster
Posts: 7
Joined: Mon Dec 22, 2008 5:22 pm
Contact:

Re: newbie question

Post by funkaster »

Sarge wrote:Try changing the direction of construction (winding) of your outer frame, also that of the whiteSquare poly (try the latter first). Also, gravity is a bit high, as is your initial velocity - maybe your timestep is too long for the dynamics to be caught. Also try setting the mass of the poly via the density - use cpPolyMass - so that mass and inertia tally. Finally, I'm not sure what effect a coefficient of friction of zero will have (as you set for whiteSquare) - try values away from the extremes. Personally I try to keep to SI units for everything (including dimensions) so that all properties are balanced, then use display scaling to view.

Sorry I can't try out your code - I use Delphi and OpenGL. Good luck.
I tried your tips, but everything is just the same :-(
Also, I couldn't find cpPolyMass, where is that defined?
thanks!
Sarge
Posts: 6
Joined: Thu Dec 04, 2008 1:47 pm
Contact:

Re: newbie question

Post by Sarge »

Sorry, my mistake - seems that the helpers cpPolyArea and cpPolyMass are only in the Delphi port (by Paul Robello) that I use - I don't see them in the C code.

What about inserting the lines where the static and active hashes are defined immediately after defining the space (as per Demo2.c):

Code: Select all

cpSpaceResizeStaticHash(space, 40.0, 1000);
cpSpaceResizeActiveHash(space, 40.0, 1000);
funkaster
Posts: 7
Joined: Mon Dec 22, 2008 5:22 pm
Contact:

Re: newbie question

Post by funkaster »

well... it's working now :-)
the thing was that you have to define the vertices of the shape in body coordinates, not world coordinates :-P (pretty obvious after you think about it).
Just for reference, here's my code:

Code: Select all

		space = cpSpaceNew();
		space->iterations = 1;
		space->elasticIterations = 1;
		space->gravity = cpv(0, 98.0f);
		
		// add the walls
		cpBody *body = cpBodyNew(INFINITY, INFINITY);
		// WEST
		shape = cpSegmentShapeNew(body, cpv(0.0f, 0.0f), cpv(0.0f, frame.size.height), 0.0f);
		shape->e = 1.0; shape->u = 1.0;
		cpSpaceAddStaticShape(space, shape);
		// EAST
		shape = cpSegmentShapeNew(body, cpv(frame.size.width, 0.0f), cpv(frame.size.width, frame.size.height), 0.0f);
		shape->e = 1.0; shape->u = 1.0;
		cpSpaceAddStaticShape(space, shape);
		// SOUTH
		shape = cpSegmentShapeNew(body, cpv(0.0f, frame.size.height), cpv(frame.size.width, frame.size.height), 0.0f);
		shape->e = 1.0; shape->u = 1.0;
		cpSpaceAddStaticShape(space, shape);
		// NORTH
		shape = cpSegmentShapeNew(body, cpv(0.0f, 0.0f), cpv(frame.size.width, 0.0f), 0.0f);
		shape->e = 1.0; shape->u = 1.0;
		cpSpaceAddStaticShape(space, shape);

		// add the main body
		int num = 4;
		cpVect verts[] = {
			cpv(-15,-15),
			cpv(-15, 15),
			cpv( 15, 15),
			cpv( 15,-15),
		};
		cpFloat mass = 0.1f * 10.0f * 30.0f;
		body = cpBodyNew(mass, cpMomentForPoly(mass, num, verts, cpvzero));
		body->p = cpv(100.0f, 10.0f);
		cpBodySetAngle(body, 0.25);
		
		cpSpaceAddBody(space, body);
		whiteSquare = cpPolyShapeNew(body, num, verts, cpvzero);
		whiteSquare->e = 0.9f; whiteSquare->u = 0.1f;
		cpSpaceAddShape(space, whiteSquare);
thanks!
rolando./
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests