diff --git a/chipmunk/include/chipmunk/chipmunk_types.h b/chipmunk/include/chipmunk/chipmunk_types.h index 7d38472ef1..0354206f7b 100644 --- a/chipmunk/include/chipmunk/chipmunk_types.h +++ b/chipmunk/include/chipmunk/chipmunk_types.h @@ -5,8 +5,9 @@ #ifndef AIRPLAYUSECHIPMUNK #import "TargetConditionals.h" #endif +#endif -// dont' use CGPoints in cocos2d-x to make your code multi-platform +// cocos2d-x: dont' use CGPoints to make your code multi-platform // #if (defined TARGET_OS_IPHONE) && (!defined CP_USE_CGPOINTS) // #define CP_USE_CGPOINTS // #endif @@ -27,7 +28,8 @@ #ifndef CP_USE_DOUBLES // use doubles by default for higher precision - #define CP_USE_DOUBLES 1 + // cocos2d-x: GL_DOUBLE isn't support on all platforms + // #define CP_USE_DOUBLES 1 #endif #if CP_USE_DOUBLES diff --git a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id index 20c6fe7118..7e1b7984d8 100644 --- a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -9f36b35f636dbc0d59f5e319e14cadb04630172a \ No newline at end of file +f2d59cd9d3ffc34cf32827bfaf1bf9315c6b08a2 \ No newline at end of file diff --git a/tests/tests/ChipmunkTest/Bounce.cpp b/tests/tests/ChipmunkTest/Bounce.cpp index 292bf095ae..ec9f05ad94 100644 --- a/tests/tests/ChipmunkTest/Bounce.cpp +++ b/tests/tests/ChipmunkTest/Bounce.cpp @@ -28,7 +28,6 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; static void update(int ticks) @@ -67,37 +66,36 @@ add_box() static cpSpace * init(void) { - staticBody = cpBodyNew(INFINITY, INFINITY); - cpResetShapeIdCounter(); space = cpSpaceNew(); cpSpaceResizeActiveHash(space, 30.0f, 1000); space->iterations = 10; - + + cpBody *body, *staticBody = &space->staticBody; cpShape *shape; - + // Create segments around the edge of the screen. - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; for(int i=0; i<10; i++) add_box(); - cpBody *body = cpSpaceAddBody(space, cpBodyNew(100.0f, 10000.0f)); + body = cpSpaceAddBody(space, cpBodyNew(100.0f, 10000.0f)); shape = cpSpaceAddShape(space, cpSegmentShapeNew(body, cpv(-75,0), cpv(75,0), 5.0f)); shape->e = 1.0f; shape->u = 1.0f; @@ -110,7 +108,6 @@ init(void) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); } diff --git a/tests/tests/ChipmunkTest/Joints.cpp b/tests/tests/ChipmunkTest/Joints.cpp index 03f8af4677..583975b3f0 100644 --- a/tests/tests/ChipmunkTest/Joints.cpp +++ b/tests/tests/ChipmunkTest/Joints.cpp @@ -28,7 +28,6 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; static cpBody * addBall(cpVect pos, cpVect boxOffset) @@ -117,52 +116,52 @@ addChassis(cpVect pos, cpVect boxOffset) static cpSpace * init(void) { - staticBody = cpBodyNew(INFINITY, INFINITY); - space = cpSpaceNew(); space->iterations = 10; space->gravity = cpv(0, -100); + space->sleepTimeThreshold = 0.5f; + cpBody *staticBody = &space->staticBody; cpShape *shape; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,120), cpv(320,120), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,120), cpv(320,120), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,0), cpv(320,0), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,0), cpv(320,0), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-120), cpv(320,-120), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-120), cpv(320,-120), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-240), cpv(-160,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-240), cpv(-160,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(0,-240), cpv(0,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0,-240), cpv(0,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(160,-240), cpv(160,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(160,-240), cpv(160,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; @@ -226,7 +225,7 @@ init(void) cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Hold their rotation within 90 degrees of each other. - cpSpaceAddConstraint(space, cpRotaryLimitJointNew(body1, body2, -(cpFloat)M_PI_2, (cpFloat)M_PI_2)); + cpSpaceAddConstraint(space, cpRotaryLimitJointNew(body1, body2, (cpFloat)-M_PI_2, (cpFloat)M_PI_2)); // Ratchet Joint - A rotary ratchet, like a socket wrench boxOffset = cpv(160, -120); @@ -282,7 +281,6 @@ update(int ticks) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); } diff --git a/tests/tests/ChipmunkTest/LogoSmash.cpp b/tests/tests/ChipmunkTest/LogoSmash.cpp index 7e1ea9fb50..33fa9ee844 100644 --- a/tests/tests/ChipmunkTest/LogoSmash.cpp +++ b/tests/tests/ChipmunkTest/LogoSmash.cpp @@ -103,6 +103,7 @@ init(void) { space = cpSpaceNew(); cpSpaceResizeActiveHash(space, 2.0f, 10000); + cpSpaceResizeStaticHash(space, 2.0f, 10000); space->iterations = 1; cpBody *body; diff --git a/tests/tests/ChipmunkTest/MagnetsElectric.cpp b/tests/tests/ChipmunkTest/MagnetsElectric.cpp index 78997d86b5..e5ccb26f3f 100644 --- a/tests/tests/ChipmunkTest/MagnetsElectric.cpp +++ b/tests/tests/ChipmunkTest/MagnetsElectric.cpp @@ -71,7 +71,6 @@ typedef struct DataforForce{ // Global Varibales static cpSpace *space; -static cpBody *staticBody; // **** Forces ****** // @@ -99,13 +98,13 @@ MagDipoleForce(ForceData* data){ // Components in polar coordinates - Fr=((cpFloat)2.0e0*cpfcos(alpha)*cpfcos(beta) - cpfsin(alpha)*cpfsin(beta)); - Fphi=cpfsin(alpha+beta); + Fr=(2.0e0f*cosf(alpha)*cosf(beta) - sinf(alpha)*sinf(beta)); + Fphi=sinf(alpha+beta); // printf("%g %g %g %g %g\n",phi,alpha,beta,Fphi); // Cartesian coordinates - data->F=cpv(Fr*cpfcos(phi)-Fphi*cpfsin(phi),Fr*cpfsin(phi)+Fphi*cpfcos(phi)); - data->F=cpvmult(data->F,(cpFloat)-3.e0*(cpFloat)MAG_MKS/(data->r[1]*data->r[1])); + data->F=cpv(Fr*cosf(phi)-Fphi*sinf(phi),Fr*sinf(phi)+Fphi*cosf(phi)); + data->F=cpvmult(data->F,-3.e0f*(cpFloat)MAG_MKS/(data->r[1]*data->r[1])); } static void @@ -121,7 +120,7 @@ MagDipoleTorque(ForceData* data){ // Torque. Though we could use a component of F to save some space, // we use another variables for the sake of clarity. - data->T=((cpFloat)MAG_MKS/data->r[2])*((cpFloat)3.0e0*cpfcos(alpha)*cpfsin(beta) + cpfsin(alpha-beta)); + data->T=((cpFloat)MAG_MKS/data->r[2])*(3.0e0f*cosf(alpha)*sinf(beta) + sinf(alpha-beta)); } // ******* // @@ -214,7 +213,7 @@ ChargedBodyUpdatePositionVerlet(cpBody *body, cpFloat dt) } cpVect dp = cpvmult(cpvadd(body->v, body->v_bias), dt); - dp = cpvadd(dp,cpvmult(cpvmult(body->f, body->m_inv), (cpFloat)0.5e0*dt*dt)); + dp = cpvadd(dp,cpvmult(cpvmult(body->f, body->m_inv), 0.5e0f*dt*dt)); body->p = cpvadd(body->p, dp); cpBodySetAngle(body, body->a + (body->w + body->w_bias)*dt @@ -238,11 +237,11 @@ ChargedBodyUpdatePositionVerlet(cpBody *body, cpFloat dt) static void ChargedBodyUpdateVelocityVerlet(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) { - body->v = cpvadd(body->v, cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), (cpFloat)0.5e0*dt)); - body->w = body->w + body->t*body->i_inv*(cpFloat)0.5e0*dt; + body->v = cpvadd(body->v, cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), 0.5e0f*dt)); + body->w = body->w + body->t*body->i_inv*0.5e0f*dt; body->f = cpvzero; - body->t = 0; + body->t = 0.0e0f; // Long range interaction cpArray *bodies = space->bodies; @@ -262,8 +261,8 @@ ChargedBodyUpdateVelocityVerlet(cpBody *body, cpVect gravity, cpFloat damping, c LRangeForceApply(body, B); } } - body->v = cpvadd(cpvmult(body->v,damping), cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), (cpFloat)0.5e0*dt)); - body->w = body->w*damping + body->t*body->i_inv*(cpFloat)0.5e0*dt; + body->v = cpvadd(cpvmult(body->v,damping), cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), 0.5e0f*dt)); + body->w = body->w*damping + body->t*body->i_inv*0.5e0f*dt; } static void @@ -296,11 +295,11 @@ make_mag(cpVect p, cpFloat ang, cpFloat mag) cpv( 10,-10) }; - cpBody *body = cpBodyNew(1, cpMomentForPoly(1, nverts, verts, cpvzero)); + cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, nverts, verts, cpvzero)); body->p = p; body->v = cpvzero; cpBodySetAngle(body, ang); - body->w = 0; + body->w = 0.0e0f; // Load the singularities Sing *magnet=(Sing*)cpmalloc(sizeof(Sing)); @@ -324,7 +323,7 @@ make_mag(cpVect p, cpFloat ang, cpFloat mag) cpSpaceAddBody(space, body); cpShape *shape = cpPolyShapeNew(body, nverts, verts, cpvzero); - shape->e = 0; shape->u = 0.7f; + shape->e = 0.0f; shape->u = 0.7f; cpSpaceAddShape(space, shape); } @@ -339,11 +338,11 @@ make_charged(cpVect p, cpFloat chg) cpv( 10,-10) }; - cpBody *body = cpBodyNew(1, cpMomentForPoly(1, nverts, verts, cpvzero)); + cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, nverts, verts, cpvzero)); body->p = p; body->v = cpvzero; cpBodySetAngle(body, 0); - body->w = 0; + body->w = 0.0e0f; // Load the singularities Sing *charge=(Sing*)cpmalloc(sizeof(Sing));; @@ -366,7 +365,7 @@ make_charged(cpVect p, cpFloat chg) cpSpaceAddBody(space, body); cpShape *shape = cpPolyShapeNew(body, nverts, verts, cpvzero); - shape->e = 0; shape->u = 0.7f; + shape->e = 0.0f; shape->u = 0.7f; cpSpaceAddShape(space, shape); } void @@ -381,11 +380,11 @@ make_mix(cpVect p, cpFloat ang, cpFloat mag,cpFloat chg) cpv( 10,-10) }; - cpBody *body = cpBodyNew(1, cpMomentForPoly(1, nverts, verts, cpvzero)); + cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, nverts, verts, cpvzero)); body->p = p; body->v = cpvzero; cpBodySetAngle(body, ang); - body->w = 0; + body->w = 0.0e0f; // Load the singularities Sing *mix=(Sing*)cpmalloc(sizeof(Sing));; @@ -415,7 +414,7 @@ make_mix(cpVect p, cpFloat ang, cpFloat mag,cpFloat chg) cpSpaceAddBody(space, body); cpShape *shape = cpPolyShapeNew(body, nverts, verts, cpvzero); - shape->e = 0; shape->u = 0.7f; + shape->e = 0.0f; shape->u = 0.7f; cpSpaceAddShape(space, shape); } @@ -423,67 +422,64 @@ make_mix(cpVect p, cpFloat ang, cpFloat mag,cpFloat chg) static cpSpace* init(void) { - - staticBody = cpBodyNew(INFINITY, INFINITY); - cpResetShapeIdCounter(); space = cpSpaceNew(); space->iterations = 5; space->gravity = cpvzero; //cpv(0,-100); - cpSpaceResizeActiveHash(space, 30, 2999); - + cpSpaceResizeActiveHash(space, 30.0f, 2999); + // Screen border /* shape = cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f); shape->e = 1.0; shape->u = 1.0; - cpSpaceAddStaticShape(space, shape); + cpSpaceAddShape(space, shape); shape = cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f); shape->e = 1.0; shape->u = 1.0; - cpSpaceAddStaticShape(space, shape); + cpSpaceAddShape(space, shape); shape = cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f); shape->e = 1.0; shape->u = 1.0; - cpSpaceAddStaticShape(space, shape); + cpSpaceAddShape(space, shape); // Reference line // Does not collide with other objects, we just want to draw it. shape = cpSegmentShapeNew(staticBody, cpv(-320,0), cpv(320,0), 0.0f); shape->collision_type = 1; - cpSpaceAddStaticShape(space, shape); + cpSpaceAddShape(space, shape); // Add a collision pair function to filter collisions cpSpaceAddCollisionPairFunc(space, 0, 1, NULL, NULL); */ - srand(time(NULL)); + srand((unsigned int) time(NULL)); cpVect p; cpFloat ang; // Create magnets for(int i=0; idata); - - if(cpArrayContains(platform->passThruList, b)){ - // The object is in the pass thru list, ignore it until separates. - return 0; - } else { - cpFloat dot = cpvdot(cpArbiterGetNormal(arb, 0), platform->n); + OneWayPlatform *platform = (OneWayPlatform *)a->data; - if(dot < 0){ - // Add the object to the pass thrru list - cpArrayPush(platform->passThruList, b); - return 0; - } else { - return 1; - } + if(cpvdot(cpArbiterGetNormal(arb, 0), platform->n) < 0){ + cpArbiterIgnore(arb); + return cpFalse; } -} - -static void -separate(cpArbiter *arb, cpSpace *space, void *ignore) -{ - CP_ARBITER_GET_SHAPES(arb, a, b); - // remove the object from the pass thru list - cpArrayDeleteObj(((OneWayPlatform *)a->data)->passThruList, b); + return cpTrue; } static void @@ -82,32 +64,30 @@ update(int ticks) static cpSpace * init(void) { - staticBody = cpBodyNew(INFINITY, INFINITY); - cpResetShapeIdCounter(); space = cpSpaceNew(); space->iterations = 10; space->gravity = cpv(0, -100); - - cpBody *body; + + cpBody *body, *staticBody = &space->staticBody; cpShape *shape; - + // Create segments around the edge of the screen. - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; // Add our one way segment - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-100), cpv(160,-100), 10.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->collision_type = 1; shape->layers = NOT_GRABABLE_MASK; @@ -128,7 +108,7 @@ init(void) shape->e = 0.0f; shape->u = 0.9f; shape->collision_type = 2; - cpSpaceAddCollisionHandler(space, 1, 2, NULL, preSolve, NULL, separate, NULL); + cpSpaceAddCollisionHandler(space, 1, 2, NULL, preSolve, NULL, NULL, NULL); return space; } @@ -136,7 +116,6 @@ init(void) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); diff --git a/tests/tests/ChipmunkTest/Planet.cpp b/tests/tests/ChipmunkTest/Planet.cpp index e76b4f2af5..0899c690d4 100644 --- a/tests/tests/ChipmunkTest/Planet.cpp +++ b/tests/tests/ChipmunkTest/Planet.cpp @@ -28,7 +28,9 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; +static cpBody *planetBody; + +static cpFloat gravityStrength = 5.0e6f; static void update(int ticks) @@ -40,15 +42,19 @@ update(int ticks) cpSpaceStep(space, dt); // Update the static body spin so that it looks like it's rotating. - cpBodyUpdatePosition(staticBody, dt); + cpBodyUpdatePosition(planetBody, dt); } } static void planetGravityVelocityFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt) { + // Gravitational acceleration is proportional to the inverse square of + // distance, and directed toward the origin. The central planet is assumed + // to be massive enough that it affects the satellites but not vice versa. cpVect p = body->p; - cpVect g = cpvmult(p, -50000.0f/cpvdot(p, p)); + cpFloat sqdist = cpvlengthsq(p); + cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist))); cpBodyUpdateVelocity(body, g, damping, dt); } @@ -59,7 +65,7 @@ rand_pos(cpFloat radius) cpVect v; do { v = cpv(frand()*(640 - 2*radius) - (320 - radius), frand()*(480 - 2*radius) - (240 - radius)); - } while(cpvlength(v) < 100.0f); + } while(cpvlength(v) < 85.0f); return v; } @@ -82,7 +88,17 @@ add_box() cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 4, verts, cpvzero))); body->velocity_func = planetGravityVelocityFunc; body->p = rand_pos(radius); - body->v = cpvmult(cpv(2*frand() - 1, 2*frand() - 1), 200); + + // Set the box's velocity to put it into a circular orbit from its + // starting position. + cpFloat r = cpvlength(body->p); + cpFloat v = cpfsqrt(gravityStrength / r) / r; + body->v = cpvmult(cpvperp(body->p), v); + + // Set the box's angular velocity to match its orbital period and + // align its initial angle with its position. + body->w = v; + cpBodySetAngle(body, cpfatan2(body->p.y, body->p.x)); cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(body, 4, verts, cpvzero)); shape->e = 0.0f; shape->u = 0.7f; @@ -91,8 +107,8 @@ add_box() static cpSpace * init(void) { - staticBody = cpBodyNew(INFINITY, INFINITY); - staticBody->w = 0.2f; + planetBody = cpBodyNew(INFINITY, INFINITY); + planetBody->w = 0.2f; cpResetShapeIdCounter(); @@ -103,7 +119,7 @@ init(void) for(int i=0; i<30; i++) add_box(); - cpShape *shape = cpSpaceAddStaticShape(space, cpCircleShapeNew(staticBody, 70.0f, cpvzero)); + cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(planetBody, 70.0f, cpvzero)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; @@ -113,7 +129,7 @@ init(void) static void destroy(void) { - cpBodyFree(staticBody); + cpBodyFree(planetBody); cpSpaceFreeChildren(space); cpSpaceFree(space); } diff --git a/tests/tests/ChipmunkTest/Player.cpp b/tests/tests/ChipmunkTest/Player.cpp index 32c5a6c713..508157841c 100644 --- a/tests/tests/ChipmunkTest/Player.cpp +++ b/tests/tests/ChipmunkTest/Player.cpp @@ -28,7 +28,6 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; typedef struct PlayerStruct { cpFloat u; @@ -39,27 +38,27 @@ typedef struct PlayerStruct { PlayerStruct playerInstance; -static int +static cpBool begin(cpArbiter *arb, cpSpace *space, void *ignore) { CP_ARBITER_GET_SHAPES(arb, a, b); - PlayerStruct *player = (PlayerStruct*)(a->data); + PlayerStruct *player = (PlayerStruct *)a->data; cpVect n = cpvneg(cpArbiterGetNormal(arb, 0)); if(n.y > 0.0f){ cpArrayPush(player->groundShapes, b); } - return 1; + return cpTrue; } -static int +static cpBool preSolve(cpArbiter *arb, cpSpace *space, void *ignore) { CP_ARBITER_GET_SHAPES(arb, a, b); - PlayerStruct *player = (PlayerStruct*)(a->data); + PlayerStruct *player = (PlayerStruct *)a->data; - if(arb->stamp > 0){ + if(cpArbiterIsFirstContact(arb)){ a->u = player->u; // pick the most upright jump normal each frame @@ -69,19 +68,20 @@ preSolve(cpArbiter *arb, cpSpace *space, void *ignore) } } - return 1; + return cpTrue; } static void separate(cpArbiter *arb, cpSpace *space, void *ignore) { CP_ARBITER_GET_SHAPES(arb, a, b); - PlayerStruct *player = (PlayerStruct*)(a->data); + PlayerStruct *player = (PlayerStruct *)a->data; cpArrayDeleteObj(player->groundShapes, b); if(player->groundShapes->num == 0){ a->u = 0.0f; + player->groundNormal = cpvzero; } } @@ -100,19 +100,21 @@ update(int ticks) static int lastJumpState = 0; int jumpState = (arrowDirection.y > 0.0f); + cpBody *body = playerInstance.shape->body; + cpVect groundNormal = playerInstance.groundNormal; if(groundNormal.y > 0.0f){ playerInstance.shape->surface_v = cpvmult(cpvperp(groundNormal), 400.0f*arrowDirection.x); + if(arrowDirection.x) cpBodyActivate(body); } else { playerInstance.shape->surface_v = cpvzero; } - cpBody *body = playerInstance.shape->body; - // apply jump if(jumpState && !lastJumpState && cpvlengthsq(groundNormal)){ // body->v = cpvmult(cpvslerp(groundNormal, cpv(0.0f, 1.0f), 0.5f), 500.0f); body->v = cpvadd(body->v, cpvmult(cpvslerp(groundNormal, cpv(0.0f, 1.0f), 0.75f), 500.0f)); + cpBodyActivate(body); } if(playerInstance.groundShapes->num == 0){ @@ -124,7 +126,6 @@ update(int ticks) int steps = 3; cpFloat dt = 1.0f/60.0f/(cpFloat)steps; - playerInstance.groundNormal = cpvzero; for(int i=0; iiterations = 10; space->gravity = cpv(0, -1500); - - cpBody *body; + + cpBody *body, *staticBody = &space->staticBody; cpShape *shape; // Create segments around the edge of the screen. - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape->collision_type = 2; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape->collision_type = 2; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape->collision_type = 2; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape->collision_type = 2; // add some other segments to play with - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-220,-200), cpv(-220,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-220,-200), cpv(-220,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape->collision_type = 2; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(0,-240), cpv(320,-200), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0,-240), cpv(320,-200), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape->collision_type = 2; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(200,-240), cpv(320,-100), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(200,-240), cpv(320,-100), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape->collision_type = 2; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-220,-80), cpv(200,-80), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-220,-80), cpv(200,-80), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; shape->collision_type = 2; @@ -211,7 +210,6 @@ init(void) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); diff --git a/tests/tests/ChipmunkTest/Plink.cpp b/tests/tests/ChipmunkTest/Plink.cpp index bdbe05df96..9bc37786c3 100644 --- a/tests/tests/ChipmunkTest/Plink.cpp +++ b/tests/tests/ChipmunkTest/Plink.cpp @@ -28,13 +28,12 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; // Iterate over all of the bodies and reset the ones that have fallen offscreen. static void eachBody(cpBody *body, void *unused) { - if(body->p.y < -260 || fabsf(body->p.x) > 340){ + if(body->p.y < -260 || cpfabs(body->p.x) > 340){ cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320; body->p = cpv(x, 260); } @@ -57,8 +56,6 @@ update(int ticks) static cpSpace * init(void) { - staticBody = cpBodyNew(INFINITY, INFINITY); - cpResetShapeIdCounter(); space = cpSpaceNew(); @@ -68,14 +65,14 @@ init(void) cpSpaceResizeStaticHash(space, 40.0f, 999); cpSpaceResizeActiveHash(space, 30.0f, 2999); - cpBody *body; + cpBody *body, *staticBody = &space->staticBody; cpShape *shape; // Create vertexes for a pentagon shape. cpVect verts[NUM_VERTS]; for(int i=0; ie = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; } @@ -112,7 +109,6 @@ init(void) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); } diff --git a/tests/tests/ChipmunkTest/Pump.cpp b/tests/tests/ChipmunkTest/Pump.cpp index 56e52333a4..a7cb6e4456 100644 --- a/tests/tests/ChipmunkTest/Pump.cpp +++ b/tests/tests/ChipmunkTest/Pump.cpp @@ -28,7 +28,6 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; static cpConstraint *motor; #define numBalls 5 @@ -39,8 +38,9 @@ update(int ticks) { cpFloat coef = (2.0f + arrowDirection.y)/3.0f; cpFloat rate = arrowDirection.x*30.0f*coef; + cpSimpleMotorSetRate(motor, rate); - motor->maxForce = (rate) ? 1000000.0f : 0.0f; + motor->maxForce = (rate ? 1000000.0f : 0.0f); int steps = 2; cpFloat dt = 1.0f/60.0f/(cpFloat)steps; @@ -73,39 +73,38 @@ add_ball(cpVect pos) static cpSpace * init(void) { - staticBody = cpBodyNew(INFINITY, INFINITY); - space = cpSpaceNew(); space->gravity = cpv(0, -600); + cpBody *staticBody = &space->staticBody; cpShape *shape; - // beveling all of the line segments helps prevent things from getting stuck on cracks - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-256,240), 2.0f)); + // beveling all of the line segments slightly helps prevent things from getting stuck on cracks + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-256,300), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-192,0), 2.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-192,0), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-192,0), cpv(-192, -64), 2.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,0), cpv(-192, -64), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-128,-64), cpv(-128,144), 2.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,-64), cpv(-128,144), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-192,80), cpv(-192,176), 2.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,80), cpv(-192,176), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-192,176), cpv(-128,240), 2.0f)); - shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,176), cpv(-128,240), 2.0f)); + shape->e = 0.0f; shape->u = 0.0f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-128,144), cpv(192,64), 2.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,144), cpv(192,64), 2.0f)); shape->e = 0.0f; shape->u = 0.5f; shape->layers = 1; shape->layers = NOT_GRABABLE_MASK; @@ -124,7 +123,7 @@ init(void) // add balls to hopper for(int i=0; iiterations = 20; + space->iterations = 30; cpSpaceResizeStaticHash(space, 40.0f, 1000); cpSpaceResizeActiveHash(space, 40.0f, 1000); space->gravity = cpv(0, -100); + space->sleepTimeThreshold = 0.5f; - cpBody *body; + cpBody *body, *staticBody = &space->staticBody; cpShape *shape; - int num = 4; - cpVect verts[] = { - cpv(-15,-15), - cpv(-15, 15), - cpv( 15, 15), - cpv( 15,-15), - }; - // Create segments around the edge of the screen. - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; // Add lots of boxes. for(int i=0; i<14; i++){ for(int j=0; j<=i; j++){ - body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero)); + body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForBox(1.0f, 30.0f, 30.0f))); body->p = cpv(j*32 - i*16, 300 - i*32); - cpSpaceAddBody(space, body); - shape = cpPolyShapeNew(body, num, verts, cpvzero); + + shape = cpSpaceAddShape(space, cpBoxShapeNew(body, 30.0f, 30.0f)); shape->e = 0.0f; shape->u = 0.8f; - cpSpaceAddShape(space, shape); } } @@ -104,7 +93,6 @@ init(void) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); } diff --git a/tests/tests/ChipmunkTest/PyramidTopple.cpp b/tests/tests/ChipmunkTest/PyramidTopple.cpp index 64abfd76d6..abc7c7f678 100644 --- a/tests/tests/ChipmunkTest/PyramidTopple.cpp +++ b/tests/tests/ChipmunkTest/PyramidTopple.cpp @@ -28,12 +28,11 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; static void update(int ticks) { - int steps = 2; + int steps = 3; cpFloat dt = 1.0f/60.0f/(cpFloat)steps; for(int i=0; iiterations = 20; + space->iterations = 30; cpSpaceResizeActiveHash(space, 30.0f, 2999); cpSpaceResizeStaticHash(space, 30.0f, 999); space->gravity = cpv(0, -300); + space->sleepTimeThreshold = 0.5f; cpBody *body; @@ -67,7 +65,7 @@ init(void) }; // Add a floor. - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-600,-240), cpv(600,-240), 0.0f)); + 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; @@ -127,7 +125,6 @@ init(void) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); } diff --git a/tests/tests/ChipmunkTest/Query.cpp b/tests/tests/ChipmunkTest/Query.cpp index 10b7ca01a8..127f455230 100644 --- a/tests/tests/ChipmunkTest/Query.cpp +++ b/tests/tests/ChipmunkTest/Query.cpp @@ -30,16 +30,10 @@ #include "chipmunk_unsafe.h" -// extern cpSpace *space; -// extern cpBody *staticBody; -// extern cpVect mousePoint; - static cpSpace *space; -static cpBody *staticBody; -static cpVect mousePoint; +extern cpVect mousePoint; - -cpShape *querySeg = NULL; +static cpShape *querySeg = NULL; static void @@ -58,7 +52,7 @@ update(int ticks) } cpSegmentQueryInfo info = {}; - if(cpSpaceSegmentQueryFirst(space, start, end, -1, 0, &info)){ + if(cpSpaceSegmentQueryFirst(space, start, end, CP_ALL_LAYERS, CP_NO_GROUP, &info)){ cpVect point = cpSegmentQueryHitPoint(start, end, info); lineEnd = cpvadd(point, cpvzero);//cpvmult(info.n, 4.0f)); @@ -69,7 +63,8 @@ update(int ticks) strcat(messageString, "Segment Query (None)"); } - cpSegmentShapeSetEndpoints(querySeg, cpvzero, lineEnd); + cpSegmentShapeSetEndpoints(querySeg, start, lineEnd); + cpShapeCacheBB(querySeg); // force it to update it's collision detection data so it will draw // normal other stuff. int steps = 1; @@ -83,22 +78,20 @@ update(int ticks) static cpSpace * init(void) { - staticBody = cpBodyNew(INFINITY, INFINITY); - cpResetShapeIdCounter(); space = cpSpaceNew(); space->elasticIterations = 0; space->iterations = 5; - + cpSpaceResizeStaticHash(space, 40.0f, 999); cpSpaceResizeActiveHash(space, 30.0f, 2999); - + + cpBody *staticBody = &space->staticBody; cpShape *shape; // add a non-collidable segment as a quick and dirty way to draw the query line - shape = cpSegmentShapeNew(staticBody, cpvzero, cpv(100.0f, 0.0f), 4.0f); - cpSpaceAddStaticShape(space, shape); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpvzero, cpv(100.0f, 0.0f), 4.0f)); shape->layers = 0; querySeg = shape; @@ -114,7 +107,7 @@ init(void) } { // add a static segment - cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(0, 300), cpv(300, 0), 0.0f)); + cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0, 300), cpv(300, 0), 0.0f)); } { // add a pentagon @@ -124,7 +117,7 @@ init(void) cpVect verts[NUM_VERTS]; for(int i=0; idata); + Emitter *emitter = (Emitter *) a->data; emitter->blocked++; - return 0; // Return values from sensors callbacks are ignored, + return cpFalse; // Return values from sensors callbacks are ignored, } static void blockerSeparate(cpArbiter *arb, cpSpace *space, void *unused) { CP_ARBITER_GET_SHAPES(arb, a, b); - Emitter *emitter = (Emitter*)(a->data); + Emitter *emitter = (Emitter *) a->data; emitter->blocked--; } @@ -74,16 +73,16 @@ postStepRemove(cpSpace *space, cpShape *shape, void *unused) cpShapeFree(shape); } -static int +static cpBool catcherBarBegin(cpArbiter *arb, cpSpace *space, void *unused) { cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b); - Emitter *emitter = (Emitter*)(a->data); + Emitter *emitter = (Emitter *) a->data; emitter->queue++; cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemove, b, NULL); - return 0; + return cpFalse; } static cpFloat frand_unit(){return 2.0f*((cpFloat)rand()/(cpFloat)RAND_MAX) - 1.0f;} @@ -113,14 +112,13 @@ update(int ticks) static cpSpace * init(void) { - staticBody = cpBodyNew(INFINITY, INFINITY); - cpResetShapeIdCounter(); space = cpSpaceNew(); space->iterations = 10; space->gravity = cpv(0, -100); + cpBody *staticBody = &space->staticBody; cpShape *shape; // Data structure for our ball emitter @@ -131,13 +129,13 @@ init(void) emitterInstance.position = cpv(0, 150); // Create our blocking sensor, so we know when the emitter is clear to emit another ball - shape = cpSpaceAddStaticShape(space, cpCircleShapeNew(staticBody, 15.0f, emitterInstance.position)); + shape = cpSpaceAddShape(space, cpCircleShapeNew(staticBody, 15.0f, emitterInstance.position)); shape->sensor = 1; shape->collision_type = BLOCKING_SENSOR_TYPE; shape->data = &emitterInstance; // Create our catch sensor to requeue the balls when they reach the bottom of the screen - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-2000, -200), cpv(2000, -200), 15.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-2000, -200), cpv(2000, -200), 15.0f)); shape->sensor = 1; shape->collision_type = CATCH_SENSOR_TYPE; shape->data = &emitterInstance; @@ -151,7 +149,6 @@ init(void) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); } diff --git a/tests/tests/ChipmunkTest/Simple.cpp b/tests/tests/ChipmunkTest/Simple.cpp index f7ad193c83..208818ae89 100644 --- a/tests/tests/ChipmunkTest/Simple.cpp +++ b/tests/tests/ChipmunkTest/Simple.cpp @@ -28,18 +28,11 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; // Init is called by the demo code to set up the demo. static cpSpace * init(void) { - // Create an infinite mass body to attach ground segments and other static geometry to. - // We won't be adding this body to the space, because we don't want it to be simulated at all. - // Adding bodies to the space simulates them. (fall under the influence of gravity, etc) - // We want the static body to stay right where it is at all times. - staticBody = cpBodyNew(INFINITY, INFINITY); - // Create a space, a space is a simulation world. It simulates the motions of rigid bodies, // handles collisions between them, and simulates the joints between them. space = cpSpaceNew(); @@ -55,7 +48,8 @@ init(void) space->gravity = cpv(0, -100); // Create A ground segment along the bottom of the screen - cpShape *ground = cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f); + // By attaching it to &space->staticBody instead of a body, we make it a static shape. + cpShape *ground = cpSegmentShapeNew(&space->staticBody, cpv(-320,-240), cpv(320,-240), 0.0f); // Set some parameters of the shape. // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpShape ground->e = 1.0f; ground->u = 1.0f; @@ -64,7 +58,7 @@ init(void) // If a shape never changes position, add it as static so Chipmunk knows it only needs to // calculate collision information for it once when it is added. // Do not change the postion of a static shape after adding it. - cpSpaceAddStaticShape(space, ground); + cpSpaceAddShape(space, ground); // Add a moving circle object. cpFloat radius = 15.0f; @@ -73,7 +67,8 @@ init(void) cpBody *ballBody = cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)); // Set some parameters of the body: // For more info: http://code.google.com/p/chipmunk-physics/wiki/cpBody - ballBody->p = cpv(0, -240 + radius+5); + ballBody->p = cpv(0, -240 + radius+50); + ballBody->v = cpv(0, -20); // Add the body to the space so it will be simulated and move around. cpSpaceAddBody(space, ballBody); @@ -101,7 +96,6 @@ update(int ticks) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); } diff --git a/tests/tests/ChipmunkTest/Springies.cpp b/tests/tests/ChipmunkTest/Springies.cpp index 9f203b99a0..23a972e361 100644 --- a/tests/tests/ChipmunkTest/Springies.cpp +++ b/tests/tests/ChipmunkTest/Springies.cpp @@ -29,7 +29,6 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; static cpFloat springForce(cpConstraint *spring, cpFloat dist) @@ -77,9 +76,8 @@ add_bar(cpVect a, cpVect b, int group) static cpSpace * init(void) { - staticBody = cpBodyNew(INFINITY, INFINITY); - space = cpSpaceNew(); + cpBody *staticBody = &space->staticBody; cpBody *body1 = add_bar(cpv(-240, 160), cpv(-160, 80), 1); cpBody *body2 = add_bar(cpv(-160, 80), cpv( -80, 160), 1); @@ -161,7 +159,6 @@ init(void) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); } diff --git a/tests/tests/ChipmunkTest/Tank.cpp b/tests/tests/ChipmunkTest/Tank.cpp new file mode 100644 index 0000000000..314ca302d2 --- /dev/null +++ b/tests/tests/ChipmunkTest/Tank.cpp @@ -0,0 +1,153 @@ +/* Copyright (c) 2007 Scott Lembcke + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include + +#include "chipmunk.h" +#include "drawSpace.h" +#include "ChipmunkDemo.h" + +extern cpVect mousePoint; + +static cpSpace *space; + +static cpBody *tankBody, *tankControlBody; + +static void +update(int ticks) +{ + int steps = 1; + cpFloat dt = 1.0f/60.0f/(cpFloat)steps; + + for(int i=0; ip); + cpFloat turn = cpvtoangle(cpvunrotate(tankBody->rot, mouseDelta)); + cpBodySetAngle(tankControlBody, tankBody->a - turn); + + // drive the tank towards the mouse + if(cpvnear(mousePoint, tankBody->p, 30.0f)){ + tankControlBody->v = cpvzero; // stop + } else { + cpFloat direction = (cpvdot(mouseDelta, tankBody->rot) > 0.0f ? 1.0f : -1.0f); + tankControlBody->v = cpvrotate(tankBody->rot, cpv(30.0f*direction, 0.0f)); + } + + cpSpaceStep(space, dt); + } +} + +static cpBody * +add_box(cpFloat size, cpFloat mass) +{ + cpVect verts[] = { + cpv(-size,-size), + cpv(-size, size), + cpv( size, size), + cpv( size,-size), + }; + + cpFloat radius = cpvlength(cpv(size, size)); + + cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 4, verts, cpvzero))); + body->p = cpv(frand()*(640 - 2*radius) - (320 - radius), frand()*(480 - 2*radius) - (240 - radius)); + + cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(body, 4, verts, cpvzero)); + shape->e = 0.0f; shape->u = 0.7f; + + return body; +} + +static cpSpace * +init(void) +{ + cpResetShapeIdCounter(); + + space = cpSpaceNew(); + cpSpaceResizeActiveHash(space, 30.0f, 1000); + space->iterations = 10; + space->sleepTimeThreshold = 0.5f; + + cpBody *staticBody = &space->staticBody; + cpShape *shape; + + // Create segments around the edge of the screen. + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); + shape->e = 1.0f; shape->u = 1.0f; + shape->layers = NOT_GRABABLE_MASK; + + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); + shape->e = 1.0f; shape->u = 1.0f; + shape->layers = NOT_GRABABLE_MASK; + + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); + shape->e = 1.0f; shape->u = 1.0f; + shape->layers = NOT_GRABABLE_MASK; + + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); + shape->e = 1.0f; shape->u = 1.0f; + shape->layers = NOT_GRABABLE_MASK; + + for(int i=0; i<50; i++){ + cpBody *body = add_box(10.0f, 1.0f); + + cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, body, cpvzero, cpvzero)); + pivot->biasCoef = 0.0f; // disable joint correction + pivot->maxForce = 1000.0f; // emulate linear friction + + cpConstraint *gear = cpSpaceAddConstraint(space, cpGearJointNew(staticBody, body, 0.0f, 1.0f)); + gear->biasCoef = 0.0f; // disable joint correction + gear->maxForce = 5000.0f; // emulate angular friction + } + + // We joint the tank to the control body and control the tank indirectly by modifying the control body. + tankControlBody = cpBodyNew(INFINITY, INFINITY); + tankBody = add_box(15.0f, 10.0f); + + cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(tankControlBody, tankBody, cpvzero, cpvzero)); + pivot->biasCoef = 0.0f; // disable joint correction + pivot->maxForce = 10000.0f; // emulate linear friction + + cpConstraint *gear = cpSpaceAddConstraint(space, cpGearJointNew(tankControlBody, tankBody, 0.0f, 1.0f)); + gear->biasCoef = 1.0f; // limit angular correction rate + gear->maxBias = 1.0f; // limit angular correction rate + gear->maxForce = 500000.0f; // emulate angular friction + + return space; +} + +static void +destroy(void) +{ + cpBodyFree(tankControlBody); + cpSpaceFreeChildren(space); + cpSpaceFree(space); +} + +chipmunkDemo Tank = { + "Tank", + NULL, + init, + update, + destroy, +}; diff --git a/tests/tests/ChipmunkTest/TheoJansen.cpp b/tests/tests/ChipmunkTest/TheoJansen.cpp index 6813e1715b..971bfc7817 100644 --- a/tests/tests/ChipmunkTest/TheoJansen.cpp +++ b/tests/tests/ChipmunkTest/TheoJansen.cpp @@ -34,7 +34,6 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; static cpConstraint *motor; @@ -89,7 +88,7 @@ make_leg(cpFloat side, cpFloat offset, cpBody *chassis, cpBody *crank, cpVect an cpSpaceAddConstraint(space, cpGearJointNew(upper_leg, lower_leg, 0.0f, 1.0f)); cpConstraint *constraint; - cpFloat diag = sqrtf(side*side + offset*offset); + cpFloat diag = cpfsqrt(side*side + offset*offset); constraint = cpPinJointNew(crank, upper_leg, anchor, cpv(0.0f, side)); cpPinJointSetDist(constraint, diag); @@ -103,7 +102,6 @@ static cpSpace * init(void) { space = cpSpaceNew(); - staticBody = cpBodyNew(INFINITY, INFINITY); cpResetShapeIdCounter(); @@ -111,6 +109,7 @@ init(void) space->iterations = 20; space->gravity = cpv(0,-500); + cpBody *staticBody = &space->staticBody; cpShape *shape; cpVect a, b; @@ -118,17 +117,17 @@ init(void) shape = cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - cpSpaceAddStaticShape(space, shape); + cpSpaceAddShape(space, shape); shape = cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - cpSpaceAddStaticShape(space, shape); + cpSpaceAddShape(space, shape); shape = cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - cpSpaceAddStaticShape(space, shape); + cpSpaceAddShape(space, shape); cpFloat offset = 30.0f; @@ -155,8 +154,8 @@ init(void) int num_legs = 2; for(int i=0; ie = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, b, c, 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, b, c, 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, c, d, 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, c, d, 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; - shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, d, a, 0.0f)); + shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, d, a, 0.0f)); shape->e = 1.0f; shape->u = 1.0f; shape->layers = NOT_GRABABLE_MASK; diff --git a/tests/tests/ChipmunkTest/UnsafeOps.cpp b/tests/tests/ChipmunkTest/UnsafeOps.cpp index 9503807c42..c56eddd4aa 100644 --- a/tests/tests/ChipmunkTest/UnsafeOps.cpp +++ b/tests/tests/ChipmunkTest/UnsafeOps.cpp @@ -30,11 +30,10 @@ #include "ChipmunkDemo.h" static cpSpace *space; -static cpBody *staticBody; #define NUM_CIRCLES 30 -cpShape *circles[NUM_CIRCLES]; -cpFloat circleRadius = 30.0f; +static cpShape *circles[NUM_CIRCLES]; +static cpFloat circleRadius = 30.0f; static void update(int ticks) @@ -59,8 +58,6 @@ update(int ticks) static cpSpace * init(void) { - staticBody = cpBodyNew(INFINITY, INFINITY); - cpResetShapeIdCounter(); space = cpSpaceNew(); @@ -70,7 +67,7 @@ init(void) cpSpaceResizeStaticHash(space, 40.0f, 999); cpSpaceResizeActiveHash(space, 30.0f, 2999); - cpBody *body; + cpBody *body, *staticBody = &space->staticBody; cpShape *shape; shape = cpSpaceAddStaticShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); @@ -102,7 +99,6 @@ init(void) static void destroy(void) { - cpBodyFree(staticBody); cpSpaceFreeChildren(space); cpSpaceFree(space); } diff --git a/tests/tests/ChipmunkTest/cocos2dChipmunkDemo.cpp b/tests/tests/ChipmunkTest/cocos2dChipmunkDemo.cpp index 5538ff1469..8e591941b3 100644 --- a/tests/tests/ChipmunkTest/cocos2dChipmunkDemo.cpp +++ b/tests/tests/ChipmunkTest/cocos2dChipmunkDemo.cpp @@ -32,6 +32,8 @@ */ #include "cocos2d.h" +#include "chipmunk.h" +#include "drawSpace.h" #include "cocos2dChipmunkDemo.h" #include @@ -61,6 +63,7 @@ extern chipmunkDemo OneWay; extern chipmunkDemo Player; extern chipmunkDemo Sensors; extern chipmunkDemo Joints; +extern chipmunkDemo Tank; //extern chipmunkDemo Test; @@ -78,11 +81,12 @@ static chipmunkDemo *demos[] = { &TheoJansen, &MagnetsElectric, &UnsafeOps, -// &Query, + &Query, &OneWay, &Player, &Sensors, &Joints, + &Tank, }; static int maxDemos = sizeof(demos) / sizeof(demos[0]); @@ -120,21 +124,33 @@ drawSpaceOptions options = { static void drawString(int x, int y, const char *str) { - // implement me +// glColor3f(0.0f, 0.0f, 0.0f); +// glRasterPos2i(x, y); +// +// for(int i=0, len=strlen(str); i maxPoints ? points : maxPoints; maxConstraints = constraints > maxConstraints ? constraints : maxConstraints; - char buffer[1000]; + char buffer[1024]; const char *format = - "Arbiters: %d (%d) - " - "Contact Points: %d (%d)\n" - "Other Constraints: %d, Iterations: %d\n" - "Constraints x Iterations: %d (%d)"; + "Arbiters: %d (%d) - " + "Contact Points: %d (%d)\n" + "Other Constraints: %d, Iterations: %d\n" + "Constraints x Iterations: %d (%d)\n" + "KE:% 5.2e"; + + cpArray *bodies = space->bodies; + cpFloat ke = 0.0f; + for(int i=0; inum; i++){ + cpBody *body = (cpBody *)bodies->arr[i]; + if(body->m == INFINITY || body->i == INFINITY) continue; + + ke += body->m*cpvdot(body->v, body->v) + body->i*body->w*body->w; + } sprintf(buffer, format, - arbiters, maxArbiters, - points, maxPoints, - space->constraints->num, space->iterations + space->elasticIterations, - constraints, maxConstraints - ); + arbiters, maxArbiters, + points, maxPoints, + space->constraints->num, space->iterations + space->elasticIterations, + constraints, maxConstraints, (ke < 1e-10f ? 0.0f : ke) + ); drawString(0, 220, buffer); } +static void +reshape(int width, int height) +{ + glViewport(0, 0, width, height); + + float rx = width / 2.0f; + float ry = height / 2.0f; + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrthof(-rx, rx, -ry, ry, -1.0f, 1.0f); + glTranslatef(0.5f, 0.5f, 0.0f); +} + static void display(void) { + cpVect newPoint = cpvlerp(mousePoint_last, mousePoint, 0.25f); + mouseBody->p = newPoint; + mouseBody->v = cpvmult(cpvsub(newPoint, mousePoint_last), 60.0f); + mousePoint_last = newPoint; + currDemo->updateFunc(ticks); + // glClear(GL_COLOR_BUFFER_BIT); // // drawSpace(space, currDemo->drawOptions ? currDemo->drawOptions : &options); // drawInstructions(); // drawInfo(); // drawString(-300, -210, messageString); -// +// // glutSwapBuffers(); ticks++; - - cpVect newPoint = cpvlerp(mousePoint_last, mousePoint, 0.25f); - mouseBody->p = newPoint; - mouseBody->v = cpvmult(cpvsub(newPoint, mousePoint_last), 60.0f); - mousePoint_last = newPoint; - currDemo->updateFunc(ticks); } static char * @@ -207,7 +247,7 @@ runDemo(chipmunkDemo *demo) { if(currDemo) currDemo->destroyFunc(); - + currDemo = demo; ticks = 0; mouseJoint = NULL; @@ -216,7 +256,7 @@ runDemo(chipmunkDemo *demo) maxPoints = 0; maxConstraints = 0; space = currDemo->initFunc(); - + // glutSetWindowTitle(demoTitle(currDemo)); } @@ -257,8 +297,9 @@ mouseToSpace(int x, int y) // // GLdouble mx, my, mz; // gluUnProject(x, glutGet(GLUT_WINDOW_HEIGHT) - y, 0.0f, model, proj, view, &mx, &my, &mz); - +// // return cpv(mx, my); + return cpv(x,y); } @@ -274,8 +315,8 @@ click(int button, int state, int x, int y) // if(button == GLUT_LEFT_BUTTON){ // if(state == GLUT_DOWN){ // cpVect point = mouseToSpace(x, y); -// -// cpShape *shape = cpSpacePointQueryFirst(space, point, GRABABLE_MASK_BIT, 0); +// +// cpShape *shape = cpSpacePointQueryFirst(space, point, GRABABLE_MASK_BIT, CP_NO_GROUP); // if(shape){ // cpBody *body = shape->body; // mouseJoint = cpPivotJointNew2(mouseBody, body, cpvzero, cpBodyWorld2Local(body, point)); @@ -295,7 +336,7 @@ static void timercall(int value) { // glutTimerFunc(SLEEP_TICKS, timercall, 0); -// +// // glutPostRedisplay(); } @@ -319,7 +360,7 @@ arrowKeyDownFunc(int key, int x, int y) // else if(key == GLUT_KEY_DOWN) key_down = 1; // else if(key == GLUT_KEY_LEFT) key_left = 1; // else if(key == GLUT_KEY_RIGHT) key_right = 1; - + set_arrowDirection(); } @@ -330,27 +371,22 @@ arrowKeyUpFunc(int key, int x, int y) // else if(key == GLUT_KEY_DOWN) key_down = 0; // else if(key == GLUT_KEY_LEFT) key_left = 0; // else if(key == GLUT_KEY_RIGHT) key_right = 0; - + set_arrowDirection(); } -static void -idle(void) -{ +//static void +//idle(void) +//{ // glutPostRedisplay(); -} +//} static void initGL(void) { -// glClearColor(1.0f, 1.0f, 1.0f, 0.0f); -// -// glMatrixMode(GL_PROJECTION); -// glLoadIdentity(); -// glOrtho(-320.0f, 320.0f, -240.0f, 240.0f, -1.0f, 1.0f); -// glTranslatef(0.5f, 0.5f, 0.0f); -// -// glEnableClientState(GL_VERTEX_ARRAY); + glClearColor(1.0f, 1.0f, 1.0f, 0.0f); + + glEnableClientState(GL_VERTEX_ARRAY); } static void @@ -365,16 +401,17 @@ glutStuff(int argc, const char *argv[]) // // initGL(); // +// glutReshapeFunc(reshape); // glutDisplayFunc(display); -//// glutIdleFunc(idle); +// // glutIdleFunc(idle); // glutTimerFunc(SLEEP_TICKS, timercall, 0); -// +// // glutIgnoreKeyRepeat(1); // glutKeyboardFunc(keyboard); // // glutSpecialFunc(arrowKeyDownFunc); // glutSpecialUpFunc(arrowKeyUpFunc); -// +// // glutMotionFunc(mouse); // glutPassiveMotionFunc(mouse); // glutMouseFunc(click); @@ -421,6 +458,7 @@ void ChipmunkTestLayer::init() demoIndex = 0; cpInitChipmunk(); + cp_collision_slop = 0.2f; mouseBody = cpBodyNew(INFINITY, INFINITY); runDemo(demos[firstDemoIndex]); diff --git a/tests/tests/ChipmunkTest/drawSpace.cpp b/tests/tests/ChipmunkTest/drawSpace.cpp index d847cd1e93..765956b80e 100644 --- a/tests/tests/ChipmunkTest/drawSpace.cpp +++ b/tests/tests/ChipmunkTest/drawSpace.cpp @@ -29,6 +29,8 @@ #include #include +using namespace cocos2d; + /* IMPORTANT - READ ME! @@ -43,9 +45,7 @@ #define LINE_COLOR 0.0f, 0.0f, 0.0f, 1.0f #define COLLISION_COLOR 1.0f, 0.0f, 0.0f, 1.0f -#define BODY_COLOR 0.0f, 0.0f, 1.0f - -using namespace cocos2d; +#define BODY_COLOR 0.0f, 0.0f, 1.0f, 1.0f static void glColor_from_pointer(void *ptr) @@ -77,9 +77,28 @@ glColor_from_pointer(void *ptr) g = (g*mult)/max + add; b = (b*mult)/max + add; - // glColor4ub isn't implement on some android devices + // glColor4ub isn't implemented on some android devices // glColor4ub(r, g, b, 255); - glColor4f(((GLfloat)r) / 255, ((GLfloat)g) / 255, ((GLfloat)b) / 255, 1.0f); + glColor4f( ((GLfloat)r)/255, ((GLfloat)g) / 255, ((GLfloat)b)/255, 1.0f ); +} + +static void +glColor_for_shape(cpShape *shape, cpSpace *space) +{ + cpBody *body = shape->body; + if(body){ + if(body->node.next){ + GLfloat v = 0.25f; + glColor4f(v,v,v,1); + return; + } else if(body->node.idleTime > space->sleepTimeThreshold) { + GLfloat v = 0.9f; + glColor4f(v,v,v,1); + return; + } + } + + glColor_from_pointer(shape); } static const GLfloat circleVAR[] = { @@ -113,10 +132,10 @@ static const GLfloat circleVAR[] = { static const int circleVAR_count = sizeof(circleVAR)/sizeof(GLfloat)/2; static void -drawCircleShape(cpBody *body, cpCircleShape *circle) +drawCircleShape(cpBody *body, cpCircleShape *circle, cpSpace *space) { glVertexPointer(2, GL_FLOAT, 0, circleVAR); - + // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY // Needed states: GL_VERTEX_ARRAY, // Unneeded states: GL_TEXTURE_2D, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY @@ -124,14 +143,15 @@ drawCircleShape(cpBody *body, cpCircleShape *circle) glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glPushMatrix(); { - cpVect center = cpvadd(body->p, cpvrotate(circle->c, body->rot)); + cpVect center = circle->tc; glTranslatef(center.x, center.y, 0.0f); - glRotatef(body->a*180.0f/(cpFloat)M_PI, 0.0f, 0.0f, 1.0f); + glRotatef(body->a*180.0f/(float)M_PI, 0.0f, 0.0f, 1.0f); glScalef(circle->r, circle->r, 1.0f); if(!circle->shape.sensor){ - glColor_from_pointer(circle); + glColor_for_shape((cpShape *)circle, space); glDrawArrays(GL_TRIANGLE_FAN, 0, circleVAR_count - 1); } @@ -142,57 +162,49 @@ drawCircleShape(cpBody *body, cpCircleShape *circle) // restore default GL state glEnable(GL_TEXTURE_2D); glEnableClientState(GL_COLOR_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); } static const GLfloat pillVAR[] = { - 0.0000f, 1.0000f, - 0.2588f, 0.9659f, - 0.5000f, 0.8660f, - 0.7071f, 0.7071f, - 0.8660f, 0.5000f, - 0.9659f, 0.2588f, - 1.0000f, 0.0000f, - 0.9659f, -0.2588f, - 0.8660f, -0.5000f, - 0.7071f, -0.7071f, - 0.5000f, -0.8660f, - 0.2588f, -0.9659f, - 0.0000f, -1.0000f, + 0.0000f, 1.0000f, 1.0f, + 0.2588f, 0.9659f, 1.0f, + 0.5000f, 0.8660f, 1.0f, + 0.7071f, 0.7071f, 1.0f, + 0.8660f, 0.5000f, 1.0f, + 0.9659f, 0.2588f, 1.0f, + 1.0000f, 0.0000f, 1.0f, + 0.9659f, -0.2588f, 1.0f, + 0.8660f, -0.5000f, 1.0f, + 0.7071f, -0.7071f, 1.0f, + 0.5000f, -0.8660f, 1.0f, + 0.2588f, -0.9659f, 1.0f, + 0.0000f, -1.0000f, 1.0f, - 0.0000f, -1.0000f, - -0.2588f, -0.9659f, - -0.5000f, -0.8660f, - -0.7071f, -0.7071f, - -0.8660f, -0.5000f, - -0.9659f, -0.2588f, - -1.0000f, -0.0000f, - -0.9659f, 0.2588f, - -0.8660f, 0.5000f, - -0.7071f, 0.7071f, - -0.5000f, 0.8660f, - -0.2588f, 0.9659f, - 0.0000f, 1.0000f, + 0.0000f, -1.0000f, 0.0f, + -0.2588f, -0.9659f, 0.0f, + -0.5000f, -0.8660f, 0.0f, + -0.7071f, -0.7071f, 0.0f, + -0.8660f, -0.5000f, 0.0f, + -0.9659f, -0.2588f, 0.0f, + -1.0000f, -0.0000f, 0.0f, + -0.9659f, 0.2588f, 0.0f, + -0.8660f, 0.5000f, 0.0f, + -0.7071f, 0.7071f, 0.0f, + -0.5000f, 0.8660f, 0.0f, + -0.2588f, 0.9659f, 0.0f, + 0.0000f, 1.0000f, 0.0f, }; -static const int pillVAR_count = sizeof(pillVAR)/sizeof(GLfloat)/2; +static const int pillVAR_count = sizeof(pillVAR)/sizeof(GLfloat)/3; static void -drawSegmentShape(cpBody *body, cpSegmentShape *seg) +drawSegmentShape(cpBody *body, cpSegmentShape *seg, cpSpace *space) { - cpVect a = cpvadd(body->p, cpvrotate(seg->a, body->rot)); - cpVect b = cpvadd(body->p, cpvrotate(seg->b, body->rot)); + cpVect a = seg->ta; + cpVect b = seg->tb; if(seg->r){ - cpVect delta = cpvsub(b, a); - cpFloat len = cpvlength(delta)/seg->r; + glVertexPointer(3, GL_FLOAT, 0, pillVAR); - GLfloat VAR[pillVAR_count*2]; - memcpy(VAR, pillVAR, sizeof(pillVAR)); - - for(int i=0, half=pillVAR_count; ir/cpvlength(d)); const GLfloat matrix[] = { - cos, sin, 0.0f, 0.0f, - -sin, cos, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 1.0f, - x, y, 0.0f, 1.0f, + r.x, r.y, 0.0f, 0.0f, + -r.y, r.x, 0.0f, 0.0f, + d.x, d.y, 0.0f, 0.0f, + a.x, a.y, 0.0f, 1.0f, }; - glMultMatrixf(matrix); - + if(!seg->shape.sensor){ - glColor_from_pointer(seg); + glColor_for_shape((cpShape *)seg, space); glDrawArrays(GL_TRIANGLE_FAN, 0, pillVAR_count); } glColor4f(LINE_COLOR); glDrawArrays(GL_LINE_LOOP, 0, pillVAR_count); } glPopMatrix(); - - // Restore Default GL states + + // restore default GL state glEnable(GL_TEXTURE_2D); glEnableClientState(GL_COLOR_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + } else { glColor4f(LINE_COLOR); ccDrawLine(ccp(a.x, a.y),ccp(b.x, b.y)); @@ -235,12 +246,15 @@ drawSegmentShape(cpBody *body, cpSegmentShape *seg) } static void -drawPolyShape(cpBody *body, cpPolyShape *poly) +drawPolyShape(cpBody *body, cpPolyShape *poly, cpSpace *space) { - int count = count=poly->numVerts; - //GLfloat VAR[count*2]; - GLfloat *VAR = new GLfloat[count*2]; - + int count = poly->numVerts; +#if CP_USE_DOUBLES + glVertexPointer(2, GL_DOUBLE, 0, poly->tVerts); +#else + glVertexPointer(2, GL_FLOAT, 0, poly->tVerts); +#endif + // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY // Needed states: GL_VERTEX_ARRAY, // Unneeded states: GL_TEXTURE_2D, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY @@ -248,51 +262,38 @@ drawPolyShape(cpBody *body, cpPolyShape *poly) glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glVertexPointer(2, GL_FLOAT, 0, VAR); - - cpVect *verts = poly->verts; - for(int i=0; ip, cpvrotate(verts[i], body->rot)); - VAR[2*i ] = v.x; - VAR[2*i + 1] = v.y; - } - if(!poly->shape.sensor){ - glColor_from_pointer(poly); + glColor_for_shape((cpShape *)poly, space); glDrawArrays(GL_TRIANGLE_FAN, 0, count); } glColor4f(LINE_COLOR); glDrawArrays(GL_LINE_LOOP, 0, count); - // Restore Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY + // restore default GL state glEnable(GL_TEXTURE_2D); glEnableClientState(GL_COLOR_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - - delete VAR; + glEnableClientState(GL_TEXTURE_COORD_ARRAY); } static void -drawObject(void *ptr, void *unused) +drawObject(cpShape *shape, cpSpace *space) { - cpShape *shape = (cpShape *)ptr; cpBody *body = shape->body; switch(shape->klass->type){ case CP_CIRCLE_SHAPE: - drawCircleShape(body, (cpCircleShape *)shape); + drawCircleShape(body, (cpCircleShape *)shape, space); break; case CP_SEGMENT_SHAPE: - drawSegmentShape(body, (cpSegmentShape *)shape); + drawSegmentShape(body, (cpSegmentShape *)shape, space); break; case CP_POLY_SHAPE: - drawPolyShape(body, (cpPolyShape *)shape); + drawPolyShape(body, (cpPolyShape *)shape, space); break; default: - // printf("Bad enumeration in drawObject().\n"); - break; + printf("Bad enumeration in drawObject().\n"); } } @@ -322,7 +323,8 @@ drawSpring(cpDampedSpring *spring, cpBody *body_a, cpBody *body_b) cpVect b = cpvadd(body_b->p, cpvrotate(spring->anchr2, body_b->rot)); glPointSize(5.0f); - ccDrawLine( ccp(a.x, a.y), ccp(b.x, b.y) ); + ccDrawPoint( ccp(a.x, a.y) ); + ccDrawPoint( ccp(b.x, b.y) ); cpVect delta = cpvsub(b, a); @@ -342,20 +344,21 @@ drawSpring(cpDampedSpring *spring, cpBody *body_a, cpBody *body_b) GLfloat s = 1.0f/cpvlength(delta); const GLfloat matrix[] = { - cos, sin, 0.0f, 0.0f, + cos, sin, 0.0f, 0.0f, -sin*s, cos*s, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 1.0f, - x, y, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + x, y, 0.0f, 1.0f, }; glMultMatrixf(matrix); glDrawArrays(GL_LINE_STRIP, 0, springVAR_count); } glPopMatrix(); - // Restore Default GL states + // restore default GL state glEnable(GL_TEXTURE_2D); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); + } static void @@ -374,8 +377,8 @@ drawConstraint(cpConstraint *constraint) glPointSize(5.0f); ccDrawPoint( ccp(a.x, a.y) ); ccDrawPoint( ccp(b.x, b.y) ); - ccDrawLine( ccp(a.x, a.y), ccp(b.x, b.y) ); + } else if(klass == cpSlideJointGetClass()){ cpSlideJoint *joint = (cpSlideJoint *)constraint; @@ -385,8 +388,8 @@ drawConstraint(cpConstraint *constraint) glPointSize(5.0f); ccDrawPoint( ccp(a.x, a.y) ); ccDrawPoint( ccp(b.x, b.y) ); - ccDrawLine( ccp(a.x, a.y), ccp(b.x, b.y) ); + } else if(klass == cpPivotJointGetClass()){ cpPivotJoint *joint = (cpPivotJoint *)constraint; @@ -414,32 +417,16 @@ drawConstraint(cpConstraint *constraint) } static void -drawBB(void *ptr, void *unused) +drawBB(cpShape *shape, void *unused) { - cpShape *shape = (cpShape *)ptr; - CCPoint vertices[] = { ccp(shape->bb.l, shape->bb.b), ccp(shape->bb.l, shape->bb.t), ccp(shape->bb.r, shape->bb.t), ccp(shape->bb.r, shape->bb.b), }; - ccDrawPoly(vertices, 4, false); -} - -static void -drawCollisions(void *ptr, void *data) -{ - cpArbiter *arb = (cpArbiter *)ptr; - CCPoint *aPoints = new CCPoint[arb->numContacts]; - - for(int i=0; inumContacts; i++){ - aPoints[i] = CCPoint(arb->contacts[i].p.x, arb->contacts[i].p.y); - } - - ccDrawPoints( aPoints, arb->numContacts ); - delete aPoints; + } // copied from cpSpaceHash.c @@ -471,7 +458,7 @@ drawSpatialHash(cpSpaceHash *hash) cell_count++; GLfloat v = 1.0f - (GLfloat)cell_count/10.0f; - glColor4f(v,v,v,1.0f); + glColor4f(v,v,v,1); // glRectf(i*dim, j*dim, (i + 1)*dim, (j + 1)*dim); } } @@ -480,50 +467,79 @@ drawSpatialHash(cpSpaceHash *hash) void drawSpace(cpSpace *space, drawSpaceOptions *options) { - if(options->drawHash) + if(options->drawHash){ + glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_TRUE); drawSpatialHash(space->activeShapes); + glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE); + drawSpatialHash(space->staticShapes); + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + } + + glLineWidth(options->lineThickness); + if(options->drawShapes){ + cpSpaceHashEach(space->activeShapes, (cpSpaceHashIterator)drawObject, space); + cpSpaceHashEach(space->staticShapes, (cpSpaceHashIterator)drawObject, space); + } glLineWidth(1.0f); if(options->drawBBs){ - glColor4f(0.3f, 0.5f, 0.3f, 1.0f); - cpSpaceHashEach(space->activeShapes, &drawBB, NULL); - cpSpaceHashEach(space->staticShapes, &drawBB, NULL); + glColor4f(0.3f, 0.5f, 0.3f,1); + cpSpaceHashEach(space->activeShapes, (cpSpaceHashIterator)drawBB, NULL); + cpSpaceHashEach(space->staticShapes, (cpSpaceHashIterator)drawBB, NULL); } - glLineWidth(options->lineThickness); - if(options->drawShapes){ - cpSpaceHashEach(space->activeShapes, &drawObject, NULL); - cpSpaceHashEach(space->staticShapes, &drawObject, NULL); - } - cpArray *constraints = space->constraints; - glColor4f(0.5f, 1.0f, 0.5f, 1.0f); + glColor4f(0.5f, 1.0f, 0.5f, 1); for(int i=0, count = constraints->num; iarr[i]) ); + drawConstraint((cpConstraint *)constraints->arr[i]); } if(options->bodyPointSize){ - cpArray *bodies = space->bodies; - glPointSize(options->bodyPointSize); - glColor4f(LINE_COLOR); - + + cpArray *bodies = space->bodies; + // cocos2d-x: use ccDrawPoints to optimize the speed CCPoint *aPoints = new CCPoint[bodies->num]; - + + glColor4f(LINE_COLOR); for(int i=0, count = bodies->num; iarr[i]; - aPoints[i] = CCPoint(body->p.x, body->p.y); + // ccDrawPoint( ccp(body->p.x, body->p.y) ); } - ccDrawPoints( aPoints, bodies->num ); - delete aPoints; + delete []aPoints; + +// glColor3f(0.5f, 0.5f, 0.5f); +// cpArray *components = space->components; +// for(int i=0; inum; i++){ +// cpBody *root = components->arr[i]; +// cpBody *body = root, *next; +// do { +// next = body->node.next; +// glVertex2f(body->p.x, body->p.y); +// } while((body = next) != root); +// } } if(options->collisionPointSize){ glPointSize(options->collisionPointSize); - glColor4f(COLLISION_COLOR); - cpArrayEach(space->arbiters, &drawCollisions, NULL); + cpArray *arbiters = space->arbiters; + for(int i=0; inum; i++){ + cpArbiter *arb = (cpArbiter*)arbiters->arr[i]; + // cocos2d-x: use ccDrawPoints to optimze the speed + CCPoint *aPoints = new CCPoint[arb->numContacts]; + + glColor4f(COLLISION_COLOR); + for(int i=0; inumContacts; i++){ + cpVect v = arb->contacts[i].p; + // ccDrawPoint( ccp(v.x, v.y) ); + aPoints[i] = CCPoint(v.x, v.y); + } + + ccDrawPoints( aPoints, arb->numContacts ); + delete []aPoints; + } } }