cpConstraint * cpPivotJointNew(cpBody *a, cpBody *b, cpVect pivot) { cpVect anchr1 = (a ? cpBodyWorld2Local(a, pivot) : pivot); cpVect anchr2 = (b ? cpBodyWorld2Local(b, pivot) : pivot); return cpPivotJointNew2(a, b, anchr1, anchr2); }
static void grabObject(cpBody *body) { cpVect lp; lp = cpBodyWorld2Local(body, atlMouseClickPos()); atlCreateMouseJoint(g_Space, body, lp); cpBodySetAngle(body, M_PI/180); }
__declspec( dllexport ) void explosion( const void * _in, int in_size, void * _out, int out_sz ) { int i; Variable *var; cpBody *body; cpVect bp; float angle; float dist; float divi; float finalPower; float power = PEEKFLOAT(INPUT_MEMBLOCK,0); cpVect position = PEEKVECT(INPUT_MEMBLOCK,4); for (i = 0;i != mVariableHandler.mSize;i++) { var = mVariableHandler.mPtrArray[i]; if (var == NULL)continue; if (var->mType != VarTypeBody) continue; body = (cpBody*)var->mPtr; if (cpBodyIsStatic(body)) continue; bp = cpvsub(position,cpBodyGetPos(body)); dist = cpvlength(bp)+0.1f; divi = (power/dist); finalPower = -min(power,divi*divi); angle = cpvtoangle(bp); cpBodyApplyImpulse(body,cpv(cosf(angle)*finalPower,sinf(angle)*finalPower),cpBodyWorld2Local(body,position)); } }
void GameTrack::_surfaceQuery(R2DSurfaceQuery* query) { query->grip=grips[0]; dvect pos=cpVectToDvect(query->pos); query->height=0; for(vector<GameRoad*>::iterator i=roads.begin();i!=roads.end();i++) { if((*i)->pointInside(pos)) { query->grip=(*i)->surface_grip; query->roughness=(*i)->surface_roughness; break; } } //TODO: hax! cpShape *s=cpSpacePointQueryFirst(sim->get_space(), query->pos, 0x8000, 0); if(s) { cpVect local_pos=cpBodyWorld2Local(s->body,query->pos); static const dvect p_offset=dvect(0.5,0.5); GameCar* obj=(GameCar*)s->body->data; if(obj) { dvect local_p=cpVectToDvect(local_pos); dvect size=(obj->shapes[0].end-obj->shapes[0].start); local_p=local_p/size+p_offset; query->height=(1-local_p.x); /* printf("world %f %f local %f %f\n", query->pos.x,query->pos.y, local_p.x,local_p.y); */ } else { query->height=0; } } //query->height=fmax(cos(query->pos.x/5),0); //query->height=((float)((int)(query->pos.x*10)%10))/10; /* if(query->pos.x>0 && query->pos.x<10) { query->height=query->pos.x/10; } else if (query->pos.x>=10 && query->pos.x<13) { query->height=(13-query->pos.x)/3; } else { query->height=0; } */ }
static void update(cpSpace *space) { cpFloat tolerance = 2.0; if(ChipmunkDemoRightClick && cpShapeNearestPointQuery(shape, ChipmunkDemoMouse, NULL) > tolerance){ cpBody *body = cpShapeGetBody(shape); int count = cpPolyShapeGetNumVerts(shape); // Allocate the space for the new vertexes on the stack. cpVect *verts = (cpVect *)alloca((count + 1)*sizeof(cpVect)); for(int i=0; i<count; i++){ verts[i] = cpPolyShapeGetVert(shape, i); } verts[count] = cpBodyWorld2Local(body, ChipmunkDemoMouse); // This function builds a convex hull for the vertexes. // Because the result array is NULL, it will reduce the input array instead. int hullCount = cpConvexHull(count + 1, verts, NULL, NULL, tolerance); // Figure out how much to shift the body by. cpVect centroid = cpCentroidForPoly(hullCount, verts); // Recalculate the body properties to match the updated shape. cpFloat mass = cpAreaForPoly(hullCount, verts)*DENSITY; cpBodySetMass(body, mass); cpBodySetMoment(body, cpMomentForPoly(mass, hullCount, verts, cpvneg(centroid))); cpBodySetPos(body, cpBodyLocal2World(body, centroid)); // Use the setter function from chipmunk_unsafe.h. // You could also remove and recreate the shape if you wanted. cpPolyShapeSetVerts(shape, hullCount, verts, cpvneg(centroid)); } int steps = 1; cpFloat dt = 1.0f/60.0f/(cpFloat)steps; for(int i=0; i<steps; i++){ cpSpaceStep(space, dt); } }
cpBody* drawing_update(cpSpace *space, cpBody *drawing, cpFloat x, cpFloat y) { Point_array *pa = cpBodyGetUserData(drawing); int length = pa->x_values->len; // if the difference between the new x and the previous x is greater than the threshold if (( abs((SCALE * x) - (SCALE * g_array_index(pa->x_values, cpFloat, length - 1))) >= (SCALE * THRESHOLD) ) || ( abs((SCALE * y) - (SCALE * g_array_index(pa->y_values, cpFloat, length - 1))) >= (SCALE * THRESHOLD) )) { cpVect point = cpv(x, y); cpBodyWorld2Local(drawing, point); x = point.x; y = point.y; g_array_append_val(pa->x_values, x); g_array_append_val(pa->y_values, y); cpBodySetUserData(drawing, pa); drawing = add_segment_to_body(space, drawing); } cpBodySetVel(drawing, cpvzero); return drawing; }
static void 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, CP_NO_GROUP); if(shape){ cpBody *body = shape->body; mouseJoint = cpPivotJointNew2(mouseBody, body, cpvzero, cpBodyWorld2Local(body, point)); mouseJoint->maxForce = 50000.0f; mouseJoint->errorBias = cpfpow(1.0f - 0.15f, 60.0f); cpSpaceAddConstraint(space, mouseJoint); } } else if(mouseJoint){ cpSpaceRemoveConstraint(space, mouseJoint); cpConstraintFree(mouseJoint); mouseJoint = NULL; } } }
cVect cBody::World2Local( const cVect v ) { return tovect( cpBodyWorld2Local( mBody, tocpv( v ) ) ); }
Vec2 PhysicsBody::world2Local(const Vec2& point) { return PhysicsHelper::cpv2point(cpBodyWorld2Local(_info->getBody(), PhysicsHelper::point2cpv(point))); }
cpVect * bmx_cpbody_world2local(cpBody * body, cpVect * vec) { return bmx_cpvect_new(cpBodyWorld2Local(body, *vec)); }
cpConstraint * cpPivotJointNew(cpBody *a, cpBody *b, cpVect pivot) { return cpPivotJointNew2(a, b, cpBodyWorld2Local(a, pivot), cpBodyWorld2Local(b, pivot)); }
static int cpBody_getWorld2Local (lua_State *L){ cpBody *b = check_cpBody(L, 1); cpVect vi = check_cpVect(L, 2); push_cpVect(L, cpBodyWorld2Local(b, vi)); return 2; }
static void pullControlPoint(cpShape *shape) { int i; float origArea, newArea, newMass; control_point_t *cp; cpPolyShape *ps; cpCircleShape *circle; cpVect lp; cpVect verts[4]; cpConstraint *mouseJoint; mouseJoint = atlMouseJoint(); if (!mouseJoint) { lp = cpBodyWorld2Local(shape->body, atlMouseClickPos()); } else { /* update joint position */ lp = cpBodyWorld2Local(shape->body, atlMousePos()); atlRemoveMouseJoint(g_Space); } atlCreateMouseJoint(g_Space, shape->body, lp); cp = (control_point_t *)shape->data; ps = (cpPolyShape *)cp->dom->shape; control_point_t *circlePoint; for (i = 0; i < 4; ++i) { verts[i] = cpPolyShapeGetVert((cpShape *)ps, i); if (lp.x > 0) { if (verts[i].x > 0) verts[i].x = lp.x; else verts[i].x = -lp.x; } else if (lp.x < 0) { if (verts[i].x < 0) verts[i].x = lp.x; else verts[i].x = -lp.x; } if (lp.y > 0) { if (verts[i].y > 0) verts[i].y = lp.y; else verts[i].y = -lp.y; } else if (lp.y < 0) { if (verts[i].y < 0) verts[i].y = lp.y; else verts[i].y = -lp.y; } circlePoint = (control_point_t *)cp->dom->pControlPoints[i]; circle = (cpCircleShape *)circlePoint->shape; cpCircleShapeSetOffset((cpShape *)circle, verts[i]); } origArea = (fabs(dominoVerts[0].x)+dominoVerts[3].x) * (fabs(dominoVerts[0].y)+dominoVerts[1].y); newArea = (fabs(verts[0].x)+verts[3].x) * (fabs(verts[0].y)+verts[1].y); if (newArea > origArea) newMass = newArea/origArea * 0.05f; else newMass = 1.0f; cpBodySetMass(cp->dom->body, newMass); cpPolyShapeSetVerts((cpShape *)ps, ps->numVerts, verts, cpvzero); }
void wrPivot1JointInit(cpVect *pivot, cpPivotJoint *joint, cpBody *a, cpBody *b) { cpPivotJointInit(joint, a, b, cpBodyWorld2Local(a, *pivot), cpBodyWorld2Local(b, *pivot)); }
void wrBodyWorld2Local(cpBody *b, cpVect *v) { cpVect ret = cpBodyWorld2Local(b, *v); v->x = ret.x; v->y = ret.y; }
static VALUE rb_cpBodyWorld2Local(VALUE self, VALUE v) { return VNEW(cpBodyWorld2Local(BODY(self), *VGET(v))); }
static cpSpace * init(void) { ChipmunkDemoMessageString = "Use the arrow keys to control the machine."; space = cpSpaceNew(); cpSpaceSetGravity(space, cpv(0, -600)); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; // 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)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-192,0), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,0), cpv(-192, -64), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,-64), cpv(-128,144), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,80), cpv(-192,176), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,176), cpv(-128,240), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,144), cpv(192,64), 2.0f)); cpShapeSetElasticity(shape, 0.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); cpVect verts[] = { cpv(-30,-80), cpv(-30, 80), cpv( 30, 64), cpv( 30,-80), }; cpBody *plunger = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY)); cpBodySetPos(plunger, cpv(-160,-80)); shape = cpSpaceAddShape(space, cpPolyShapeNew(plunger, 4, verts, cpvzero)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 0.5f); cpShapeSetLayers(shape, 1); // add balls to hopper for(int i=0; i<numBalls; i++) balls[i] = add_ball(cpv(-224 + i,80 + 64*i)); // add small gear cpBody *smallGear = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 80, 0, cpvzero))); cpBodySetPos(smallGear, cpv(-160,-160)); cpBodySetAngle(smallGear, -M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(smallGear, 80.0f, cpvzero)); cpShapeSetLayers(shape, 0); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, smallGear, cpv(-160,-160), cpvzero)); // add big gear cpBody *bigGear = cpSpaceAddBody(space, cpBodyNew(40.0f, cpMomentForCircle(40.0f, 160, 0, cpvzero))); cpBodySetPos(bigGear, cpv(80,-160)); cpBodySetAngle(bigGear, M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(bigGear, 160.0f, cpvzero)); cpShapeSetLayers(shape, 0); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, bigGear, cpv(80,-160), cpvzero)); // connect the plunger to the small gear. cpSpaceAddConstraint(space, cpPinJointNew(smallGear, plunger, cpv(80,0), cpv(0,0))); // connect the gears. cpSpaceAddConstraint(space, cpGearJointNew(smallGear, bigGear, -M_PI_2, -2.0f)); // feeder mechanism cpFloat bottom = -300.0f; cpFloat top = 32.0f; cpBody *feeder = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForSegment(1.0f, cpv(-224.0f, bottom), cpv(-224.0f, top)))); cpBodySetPos(feeder, cpv(-224, (bottom + top)/2.0f)); cpFloat len = top - bottom; cpSpaceAddShape(space, cpSegmentShapeNew(feeder, cpv(0.0f, len/2.0f), cpv(0.0f, -len/2.0f), 20.0f)); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, feeder, cpv(-224.0f, bottom), cpv(0.0f, -len/2.0f))); cpVect anchr = cpBodyWorld2Local(feeder, cpv(-224.0f, -160.0f)); cpSpaceAddConstraint(space, cpPinJointNew(feeder, smallGear, anchr, cpv(0.0f, 80.0f))); // motorize the second gear motor = cpSpaceAddConstraint(space, cpSimpleMotorNew(staticBody, bigGear, 3.0f)); return space; }
static cpSpace * init(void) { space = cpSpaceNew(); space->gravity = cpv(0, -600); cpBody *staticBody = &space->staticBody; cpShape *shape; // 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 = 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 = 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 = 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 = 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 = 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 = 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; cpVect verts[] = { cpv(-30,-80), cpv(-30, 80), cpv( 30, 64), cpv( 30,-80), }; cpBody *plunger = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY)); plunger->p = cpv(-160,-80); shape = cpSpaceAddShape(space, cpPolyShapeNew(plunger, 4, verts, cpvzero)); shape->e = 1.0f; shape->u = 0.5f; shape->layers = 1; // add balls to hopper for(int i=0; i<numBalls; i++) balls[i] = add_ball(cpv(-224 + i,80 + 64*i)); // add small gear cpBody *smallGear = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 80, 0, cpvzero))); smallGear->p = cpv(-160,-160); cpBodySetAngle(smallGear, (cpFloat)-M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(smallGear, 80.0f, cpvzero)); shape->layers = 0; cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, smallGear, cpv(-160,-160), cpvzero)); // add big gear cpBody *bigGear = cpSpaceAddBody(space, cpBodyNew(40.0f, cpMomentForCircle(40.0f, 160, 0, cpvzero))); bigGear->p = cpv(80,-160); cpBodySetAngle(bigGear, (cpFloat)M_PI_2); shape = cpSpaceAddShape(space, cpCircleShapeNew(bigGear, 160.0f, cpvzero)); shape->layers = 0; cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, bigGear, cpv(80,-160), cpvzero)); // connect the plunger to the small gear. cpSpaceAddConstraint(space, cpPinJointNew(smallGear, plunger, cpv(80,0), cpv(0,0))); // connect the gears. cpSpaceAddConstraint(space, cpGearJointNew(smallGear, bigGear, (cpFloat)-M_PI_2, -2.0f)); // feeder mechanism cpFloat bottom = -300.0f; cpFloat top = 32.0f; cpBody *feeder = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForSegment(1.0f, cpv(-224.0f, bottom), cpv(-224.0f, top)))); feeder->p = cpv(-224, (bottom + top)/2.0f); cpFloat len = top - bottom; cpSpaceAddShape(space, cpSegmentShapeNew(feeder, cpv(0.0f, len/2.0f), cpv(0.0f, -len/2.0f), 20.0f)); cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, feeder, cpv(-224.0f, bottom), cpv(0.0f, -len/2.0f))); cpVect anchr = cpBodyWorld2Local(feeder, cpv(-224.0f, -160.0f)); cpSpaceAddConstraint(space, cpPinJointNew(feeder, smallGear, anchr, cpv(0.0f, 80.0f))); // motorize the second gear motor = cpSpaceAddConstraint(space, cpSimpleMotorNew(staticBody, bigGear, 3.0f)); return space; }
cp::Vect Body::world2Local(const cp::Vect& v) { return cpBodyWorld2Local(body,v); }