static int cpVect_near(lua_State *L) { cpVect v1 = check_cpVect(L, 1); cpVect v2 = check_cpVect(L, 3); cpFloat d = (cpFloat)luaL_checknumber(L, 5); lua_pushboolean(L, cpvnear(v1, v2, d)); return 1; }
// Recursive function used by cpPolylineSimplifyCurves(). static cpPolyline * DouglasPeucker( cpVect *verts, cpPolyline *reduced, int length, int start, int end, cpFloat min, cpFloat tol ){ // Early exit if the points are adjacent if((end - start + length)%length < 2) return reduced; cpVect a = verts[start]; cpVect b = verts[end]; // Check if the length is below the threshold if(cpvnear(a, b, min) && cpPolylineIsShort(verts, length, start, end, min)) return reduced; // Find the maximal vertex to split and recurse on cpFloat max = 0.0; int maxi = start; cpVect n = cpvnormalize(cpvperp(cpvsub(b, a))); cpFloat d = cpvdot(n, a); for(int i=Next(start, length); i!=end; i=Next(i, length)){ cpFloat dist = fabs(cpvdot(n, verts[i]) - d); if(dist > max){ max = dist; maxi = i; } } if(max > tol){ reduced = DouglasPeucker(verts, reduced, length, start, maxi, min, tol); reduced = cpPolylinePush(reduced, verts[maxi]); reduced = DouglasPeucker(verts, reduced, length, maxi, end, min, tol); } return reduced; }
static void update(int ticks) { int steps = 1; cpFloat dt = 1.0f/60.0f/(cpFloat)steps; for(int i=0; i<steps; i++){ // turn the control body based on the angle relative to the actual body cpVect mouseDelta = cpvsub(mousePoint, tankBody->p); cpFloat turn = cpvtoangle(cpvunrotate(tankBody->rot, mouseDelta)); cpBodySetAngle(tankControlBody, tankBody->a - turn); // drive the tank towards the mouse if(cpvnear(mousePoint, tankBody->p, 30.0)){ tankControlBody->v = cpvzero; // stop } else { cpFloat direction = (cpvdot(mouseDelta, tankBody->rot) > 0.0 ? 1.0 : -1.0); tankControlBody->v = cpvrotate(tankBody->rot, cpv(30.0f*direction, 0.0f)); } cpSpaceStep(space, dt); } }
static cpBool cpCircleShapePointQuery(cpCircleShape *circle, cpVect p){ return cpvnear(circle->tc, p, circle->r); }
static cpBool cpCircleShapePointQuery(cpShape *shape, cpVect p){ cpCircleShape *circle = (cpCircleShape *)shape; return cpvnear(circle->tc, p, circle->r); }