cpPolyShape * cpPolyShapeInit(cpPolyShape *poly, cpBody *body, int count, const cpVect *verts, cpTransform transform, cpFloat radius) { cpVect *hullVerts = (cpVect *)alloca(count*sizeof(cpVect)); // Transform the verts before building the hull in case of a negative scale. for(int i=0; i<count; i++) hullVerts[i] = cpTransformPoint(transform, verts[i]); unsigned int hullCount = cpConvexHull(count, hullVerts, hullVerts, NULL, 0.0); return cpPolyShapeInitRaw(poly, body, hullCount, hullVerts, radius); }
cpPolyShape * cpBoxShapeInit2(cpPolyShape *poly, cpBody *body, cpBB box, cpFloat radius) { cpVect verts[] = { cpv(box.r, box.b), cpv(box.r, box.t), cpv(box.l, box.t), cpv(box.l, box.b), }; return cpPolyShapeInitRaw(poly, body, 4, verts, radius); }
DynamicObject::DynamicObject(float x, float y, float scale, float mass, float elast, float fric, int type, std::string gpuPath, std::string vPath, std::string fPath) { gpuDataList.push_back(gpuStore.add(gpuPath, 3.1415f)); shaderList.push_back(shaderStore.add(vPath, fPath)); transformOverrides = false; height = scale; modelScale = glm::vec3(scale); width = height*gpuDataList[0]->whRatio; /*** Set physics data ***/ body = cpBodyNew(mass, 0); cpSpaceAddBody(space, body); cpBodySetPosition(body, cpv(x, y)); ObjGPUData* gpuData = gpuDataList[0]; int vertCount = gpuData->vList.size(); cpVect vertices[vertCount]; glm::vec3 pos(x, y, 0); for(int i = 0; i < vertCount; i++) { glm::vec4 currentVert = glm::translate(glm::mat4(1.0f), pos) * glm::scale(glm::mat4(1.0f), modelScale) * gpuData->rotation * gpuData->unitScale * glm::vec4(gpuData->vList[i],0); vertices[i] = cpv(currentVert.x, currentVert.y); } shape = cpSpaceAddShape(space, (cpShape*) cpPolyShapeInitRaw(cpPolyShapeAlloc(), body, vertCount, vertices, 1.0f)); cpBodySetMoment(body, abs(cpMomentForPoly(mass, vertCount, vertices, cpvzero, 1.0f))); cpShapeSetElasticity(shape, elast); cpShapeSetFriction(shape, fric); cpShapeSetUserData(shape, this); cpShapeSetCollisionType(shape, type); draw = true; }
cpShape * cpPolyShapeNewRaw(cpBody *body, int count, const cpVect *verts, cpFloat radius) { return (cpShape *)cpPolyShapeInitRaw(cpPolyShapeAlloc(), body, count, verts, radius); }