Mercurial > projects > chipmunkd
view trunk/tests/ChipmunkDemos/samples/PyramidTopple.d @ 25:5497d518b428
fixed memory corruption bug
author | Extrawurst |
---|---|
date | Mon, 13 Dec 2010 02:31:53 +0100 |
parents | af2f61a96318 |
children |
line wrap: on
line source
// written in the D programming language module samples.PyramidTopple; import chipmunkd.chipmunk; import samples.ChipmunkDemo; import std.math:PI; static cpSpace *space; static void update(int ticks) { int steps = 3; cpFloat dt = 1.0f/60.0f/cast(cpFloat)steps; for(int i=0; i<steps; i++) cpSpaceStep(space, dt); } static cpSpace * init() { cpResetShapeIdCounter(); space = cpSpaceNew(); space.iterations = 30; cpSpaceResizeActiveHash(space, 30.0f, 2999); cpSpaceResizeStaticHash(space, 30.0f, 999); space.gravity = cpv(0, -300); space.sleepTimeThreshold = 0.5f; cpBody *_body; cpShape *shape; // Vertexes for the dominos. int num = 4; cpVect verts[] = [ cpv(-3,-20), cpv(-3, 20), cpv( 3, 20), cpv( 3,-20), ]; // Add a floor. shape = cpSpaceAddShape(space, cpSegmentShapeNew(&space.staticBody, cpv(-600,-240), cpv(600,-240), 0.0f)); shape.e = 1.0f; shape.u = 1.0f; shape.layers = NOT_GRABABLE_MASK; // Shared friction constant. cpFloat u = 0.6f; // Add the dominoes. Skim over this. It doesn't do anything fancy, and it's hard to follow. int n = 9; for(int i=1; i<=n; i++){ cpVect offset = cpv(-i*60/2.0f, (n - i)*52); for(int j=0; j<i; j++){ _body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts.ptr, cpvzero))); _body.p = cpvadd(cpv(j*60, -220), offset); shape = cpSpaceAddShape(space, cpPolyShapeNew(_body, num, verts.ptr, cpvzero)); shape.e = 0.0f; shape.u = u; _body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts.ptr, cpvzero))); _body.p = cpvadd(cpv(j*60, -197), offset); cpBodySetAngle(_body, cast(float)PI/2.0f); shape = cpSpaceAddShape(space, cpPolyShapeNew(_body, num, verts.ptr, cpvzero)); shape.e = 0.0f; shape.u = u; if(j == (i - 1)) continue; _body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts.ptr, cpvzero))); _body.p = cpvadd(cpv(j*60 + 30, -191), offset); cpBodySetAngle(_body, cast(float)PI/2.0f); shape = cpSpaceAddShape(space, cpPolyShapeNew(_body, num, verts.ptr, cpvzero)); shape.e = 0.0f; shape.u = u; } _body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts.ptr, cpvzero))); _body.p = cpvadd(cpv(-17, -174), offset); shape = cpSpaceAddShape(space, cpPolyShapeNew(_body, num, verts.ptr, cpvzero)); shape.e = 0.0f; shape.u = u; _body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts.ptr, cpvzero))); _body.p = cpvadd(cpv((i - 1)*60 + 17, -174), offset); shape = cpSpaceAddShape(space, cpPolyShapeNew(_body, num, verts.ptr, cpvzero)); shape.e = 0.0f; shape.u = u; } // Give the last domino a little tap. // body.w = -1; // body.v = cpv(-body.w*20, 0); return space; } static void destroy() { cpSpaceFreeChildren(space); cpSpaceFree(space); } chipmunkDemo PyramidTopple = { "Pyramid Topple", null, &init, &update, &destroy, };