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);
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);
regards,
rolando./