Shell(cpVect pos, cpVect vel, float angle) { cpVect vl[4] = {cpv(0, 0), cpv(0.1, 0), cpv(0.07, 0.3), cpv(0.03, 0.3)}; int vn = sizeof(vl)/sizeof(cpVect); float mass = cpAreaForPoly(vn, vl, 0) * shell_density; float moi = cpMomentForPoly(mass, vn, vl, cpv(0, 0), 0); body = cpBodyNew(mass, moi); cpshape = cpPolyShapeNew(body, vn, vl, cpTransformIdentity, 0); cpShapeSetFriction(cpshape, 0.9); cpVect centroid = cpCentroidForPoly(vn, vl); shape.setPointCount(vn); for (int i = 0; i < vn; i++) { shape.setPoint(i, sf::Vector2f(vl[i].x, vl[i].y)); } cpBodySetCenterOfGravity(body, centroid); cpBodySetPosition(body, pos-centroid); cpBodySetVelocity(body, vel); cpBodySetAngle(body, angle); cpShapeSetCollisionType(cpshape, 2); cpShapeSetUserData(cpshape, this); }
bool PhysicsShapePolygon::init(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, const Point& offset/* = Point(0, 0)*/) { do { CC_BREAK_IF(!PhysicsShape::init(Type::POLYGEN)); cpVect* vecs = new cpVect[count]; PhysicsHelper::points2cpvs(points, vecs, count); cpShape* shape = cpPolyShapeNew(_info->getSharedBody(), count, vecs, PhysicsHelper::point2cpv(offset)); CC_SAFE_DELETE_ARRAY(vecs); CC_BREAK_IF(shape == nullptr); _info->add(shape); _area = calculateArea(); _mass = material.density == PHYSICS_INFINITY ? PHYSICS_INFINITY : material.density * _area; _moment = calculateDefaultMoment(); _center = PhysicsHelper::cpv2point(cpCentroidForPoly(((cpPolyShape*)shape)->numVerts, ((cpPolyShape*)shape)->verts)); setMaterial(material); return true; } while (false); return false; }
void cpRecenterPoly(const int numVerts, cpVect *verts){ cpVect centroid = cpCentroidForPoly(numVerts, verts); for(int i=0; i<numVerts; i++){ verts[i] = cpvsub(verts[i], centroid); } }
void cpRecenterPoly(const int count, cpVect *verts){ cpVect centroid = cpCentroidForPoly(count, verts); for(int i=0; i<count; i++){ verts[i] = cpvsub(verts[i], centroid); } }
Point PhysicsShape::getPolyonCenter(Point* points, int count) { cpVect* cpvs = new cpVect[count]; cpVect center = cpCentroidForPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count)); delete[] cpvs; return PhysicsHelper::cpv2point(center); }
Vec2 PhysicsShape::getPolyonCenter(const Vec2* points, int count) { cpVect* cpvs = new (std::nothrow) cpVect[count]; cpVect center = cpCentroidForPoly(count, PhysicsHelper::points2cpvs(points, cpvs, count)); delete[] cpvs; return PhysicsHelper::cpv2point(center); }
static VALUE rb_cpCentroidForPoly(VALUE self, VALUE arr) { Check_Type(arr, T_ARRAY); long numVerts = RARRAY_LEN(arr); VALUE *ary_ptr = RARRAY_PTR(arr); cpVect verts[numVerts]; for(long i = 0; i < numVerts; i++) verts[i] = *VGET(ary_ptr[i]); return VNEW(cpCentroidForPoly(numVerts, verts)); }
Vec2 PhysicsShapePolygon::getCenter() { auto shape = _cpShapes.front(); int count = cpPolyShapeGetCount(shape); cpVect* vecs = new cpVect[count]; for(int i=0;i<count;++i) vecs[i] = cpPolyShapeGetVert(shape, i); Vec2 center = PhysicsHelper::cpv2point(cpCentroidForPoly(count, vecs)); CC_SAFE_DELETE_ARRAY(vecs); return center; }
static struct cpShapeMassInfo cpPolyShapeMassInfo(cpFloat mass, int count, const cpVect *verts, cpFloat radius) { // TODO moment is approximate due to radius. cpVect centroid = cpCentroidForPoly(count, verts); struct cpShapeMassInfo info = { mass, cpMomentForPoly(1.0f, count, verts, cpvneg(centroid), radius), centroid, cpAreaForPoly(count, verts, radius), }; return info; }
Vec2 PhysicsShapeEdgePolygon::getCenter() { int count = (int)_cpShapes.size(); cpVect* points = new (std::nothrow) cpVect[count]; int i = 0; for(auto shape : _cpShapes) { points[i++] = cpSegmentShapeGetA(shape); } Vec2 center = PhysicsHelper::cpv2point(cpCentroidForPoly(count, points)); delete[] points; return center; }
Vec2 PhysicsShapeEdgeChain::getCenter() { int count = (int)_cpShapes.size() + 1; cpVect* points = new cpVect[count]; int i = 0; for(auto shape : _cpShapes) { points[i++] = ((cpSegmentShape*)shape)->a; } points[i++] = ((cpSegmentShape*)_cpShapes.back())->b; Vec2 center = PhysicsHelper::cpv2point(cpCentroidForPoly(count, points)); delete[] points; return center; }
Ship(int vn, cpVect* vl, cpVect pos, Genome* ng = 0) { float mass = cpAreaForPoly(vn, vl, 0) * ship_density; float moi = cpMomentForPoly(mass, vn, vl, cpv(0, 0), 0); body = cpBodyNew(mass, moi); cpshape = cpPolyShapeNew(body, vn, vl, cpTransformIdentity, 0); cpShapeSetFriction(cpshape, 0.9); cpVect centroid = cpCentroidForPoly(vn, vl); shape.setPointCount(vn); for (int i = 0; i < vn; i++) { shape.setPoint(i, sf::Vector2f(vl[i].x, vl[i].y)); } cpBodySetCenterOfGravity(body, centroid); cpBodySetPosition(body, pos-centroid); cpBodySetVelocity(body, cpv(0, 0)); cpShapeSetCollisionType(cpshape, 1); cpShapeSetUserData(cpshape, this); last_fired = 0; nose_angle = PI/2; player = false; target = 0; score = 0; if (ng == 0) { Genome* braingenome = mutate(readgenome("shipmind.mind")); brain = braingenome->makenetwork(); delete braingenome; } else { brain = ng->makenetwork(); } score = 0; }
void PhysicsShape::recenterPoints(Vec2* points, int count, const Vec2& center) { cpVect* cpvs = new cpVect[count]; cpVect centroid = cpCentroidForPoly(count, cpvs); for(int i=0; i<count; i++){ cpvs[i] = cpvsub(cpvs[i], centroid); } PhysicsHelper::cpvs2points(cpvs, points, count); delete[] cpvs; if (center != Vec2::ZERO) { for (int i = 0; i < count; ++i) { points[i] += center; } } }
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); } }
void Slice::ClipPoly(cpSpace *space, cpShape *shape, cpVect n, cpFloat dist) { cpBody *body = cpShapeGetBody(shape); int count = cpPolyShapeGetCount(shape); int clippedCount = 0; cpVect *clipped = (cpVect *)alloca((count + 1)*sizeof(cpVect)); for(int i=0, j=count-1; i<count; j=i, i++){ cpVect a = cpBodyLocalToWorld(body, cpPolyShapeGetVert(shape, j)); cpFloat a_dist = cpvdot(a, n) - dist; if(a_dist < 0.0){ clipped[clippedCount] = a; clippedCount++; } cpVect b = cpBodyLocalToWorld(body, cpPolyShapeGetVert(shape, i)); cpFloat b_dist = cpvdot(b, n) - dist; if(a_dist*b_dist < 0.0f){ cpFloat t = cpfabs(a_dist)/(cpfabs(a_dist) + cpfabs(b_dist)); clipped[clippedCount] = cpvlerp(a, b, t); clippedCount++; } } cpVect centroid = cpCentroidForPoly(clippedCount, clipped); cpFloat mass = cpAreaForPoly(clippedCount, clipped, 0.0f)*DENSITY; cpFloat moment = cpMomentForPoly(mass, clippedCount, clipped, cpvneg(centroid), 0.0f); cpBody *new_body = cpSpaceAddBody(space, cpBodyNew(mass, moment)); cpBodySetPosition(new_body, centroid); cpBodySetVelocity(new_body, cpBodyGetVelocityAtWorldPoint(body, centroid)); cpBodySetAngularVelocity(new_body, cpBodyGetAngularVelocity(body)); cpTransform transform = cpTransformTranslate(cpvneg(centroid)); cpShape *new_shape = cpSpaceAddShape(space, cpPolyShapeNew(new_body, clippedCount, clipped, transform, 0.0)); // Copy whatever properties you have set on the original shape that are important cpShapeSetFriction(new_shape, cpShapeGetFriction(shape)); }
bool PhysicsShapeEdgePolygon::init(const Point* points, int count, const PhysicsMaterial& material/* = MaterialDefault*/, float border/* = 1*/) { cpVect* vec = nullptr; do { CC_BREAK_IF(!PhysicsShape::init(Type::EDGEPOLYGEN)); vec = new cpVect[count]; PhysicsHelper::points2cpvs(points, vec, count); _center = PhysicsHelper::cpv2point(cpCentroidForPoly(count, vec)); int i = 0; for (; i < count; ++i) { cpShape* shape = cpSegmentShapeNew(_info->getSharedBody(), vec[i], vec[(i+1)%count], PhysicsHelper::float2cpfloat(border)); CC_BREAK_IF(shape == nullptr); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); _info->add(shape); } CC_SAFE_DELETE_ARRAY(vec); CC_BREAK_IF(i < count); _mass = PHYSICS_INFINITY; _moment = PHYSICS_INFINITY; setMaterial(material); return true; } while (false); CC_SAFE_DELETE_ARRAY(vec); return false; }
cpBool Buoyancy::WaterPreSolve(cpArbiter *arb, cpSpace *space, void *ptr) { CP_ARBITER_GET_SHAPES(arb, water, poly); cpBody *body = cpShapeGetBody(poly); // Get the top of the water sensor bounding box to use as the water level. cpFloat level = cpShapeGetBB(water).t; // Clip the polygon against the water level int count = cpPolyShapeGetCount(poly); int clippedCount = 0; #ifdef _MSC_VER // MSVC is pretty much the only compiler in existence that doesn't support variable sized arrays. cpVect clipped[10]; #else cpVect clipped[count + 1]; #endif for(int i=0, j=count-1; i<count; j=i, i++){ cpVect a = cpBodyLocalToWorld(body, cpPolyShapeGetVert(poly, j)); cpVect b = cpBodyLocalToWorld(body, cpPolyShapeGetVert(poly, i)); if(a.y < level){ clipped[clippedCount] = a; clippedCount++; } cpFloat a_level = a.y - level; cpFloat b_level = b.y - level; if(a_level*b_level < 0.0f){ cpFloat t = cpfabs(a_level)/(cpfabs(a_level) + cpfabs(b_level)); clipped[clippedCount] = cpvlerp(a, b, t); clippedCount++; } } // Calculate buoyancy from the clipped polygon area cpFloat clippedArea = cpAreaForPoly(clippedCount, clipped, 0.0f); cpFloat displacedMass = clippedArea*FLUID_DENSITY; cpVect centroid = cpCentroidForPoly(clippedCount, clipped); cpDataPointer data = ptr; DrawPolygon(clippedCount, clipped, 0.0f, RGBAColor(0, 0, 1, 1), RGBAColor(0, 0, 1, 0.1f), data); DrawDot(5, centroid, RGBAColor(0, 0, 1, 1), data); cpFloat dt = cpSpaceGetCurrentTimeStep(space); cpVect g = cpSpaceGetGravity(space); // Apply the buoyancy force as an impulse. cpBodyApplyImpulseAtWorldPoint(body, cpvmult(g, -displacedMass*dt), centroid); // Apply linear damping for the fluid drag. cpVect v_centroid = cpBodyGetVelocityAtWorldPoint(body, centroid); cpFloat k = k_scalar_body(body, centroid, cpvnormalize(v_centroid)); cpFloat damping = clippedArea*FLUID_DRAG*FLUID_DENSITY; cpFloat v_coef = cpfexp(-damping*dt*k); // linear drag // cpFloat v_coef = 1.0/(1.0 + damping*dt*cpvlength(v_centroid)*k); // quadratic drag cpBodyApplyImpulseAtWorldPoint(body, cpvmult(cpvsub(cpvmult(v_centroid, v_coef), v_centroid), 1.0/k), centroid); // Apply angular damping for the fluid drag. cpVect cog = cpBodyLocalToWorld(body, cpBodyGetCenterOfGravity(body)); cpFloat w_damping = cpMomentForPoly(FLUID_DRAG*FLUID_DENSITY*clippedArea, clippedCount, clipped, cpvneg(cog), 0.0f); cpBodySetAngularVelocity(body, cpBodyGetAngularVelocity(body)*cpfexp(-w_damping*dt/cpBodyGetMoment(body))); return cpTrue; }
Vec2 PhysicsShapePolygon::getCenter() { return PhysicsHelper::cpv2point(cpCentroidForPoly(((cpPolyShape*)_cpShapes.front())->numVerts, ((cpPolyShape*)_cpShapes.front())->verts)); }