static unsigned int _shape_add(Entity ent, PhysicsShape type, cpShape *shape) { PhysicsInfo *info; ShapeInfo *shapeInfo; info = entitypool_get(pool, ent); error_assert(info); /* init ShapeInfo */ shapeInfo = array_add(info->shapes); shapeInfo->type = type; shapeInfo->shape = shape; /* init cpShape */ cpShapeSetBody(shape, info->body); cpSpaceAddShape(space, shape); cpShapeSetFriction(shapeInfo->shape, 1); cpShapeSetUserData(shapeInfo->shape, ent); /* update moment */ if (!cpBodyIsStatic(info->body)) { if (array_length(info->shapes) > 1) cpBodySetMoment(info->body, _moment(info->body, shapeInfo) + cpBodyGetMoment(info->body)); else cpBodySetMoment(info->body, _moment(info->body, shapeInfo)); } return array_length(info->shapes) - 1; }
Line::Line(ShapeLine *src, float mass){ cpFloat moment = cpMomentForSegment(mass, toChipmunk(src->getA()), toChipmunk(src->getB()), src->getRadius()); DynamicBody::setup(cpShapeGetSpace(src->shape), mass, moment); ShapeLine::setup(src); cpSpaceRemoveShape(cpShapeGetSpace(shape), shape); cpSpaceAddShape(cpBodyGetSpace(body), shape); cpShapeSetBody(shape, body); }
WorldShape_t *worldEnt_addShape(WorldEntity_t *aEntity, WorldShape_t *aShape) { cpShapeSetBody(aShape->cpShape, aEntity->cpBody); if(aEntity == aEntity->world->staticEntity) { cpSpaceAddShape(aEntity->world->cpSpace, aShape->cpShape); } llist_pushValue(aEntity->shapes, aShape); return aShape; }
static int l_physics_setShapeBody(lua_State* state) { l_physics_Shape* shape = (l_physics_Shape*)lua_touserdata(state, 1); l_physics_Body* body = (l_physics_Body*)lua_touserdata(state, 2); if (shape->physics && shape->shape && body->body) cpShapeSetBody(shape->shape, body->body); return 0; }
Polygon::Polygon(ShapePolygon *src, float mass){ std::vector<glm::vec2> ofVerts = src->getPoints(); std::vector<cpVect> verts = toChipmunk(ofVerts); cpFloat moment = cpMomentForPoly(mass, verts.size(), verts.data(), cpvzero, cpPolyShapeGetRadius(src->shape)); DynamicBody::setup(cpShapeGetSpace(src->shape), mass, moment); cpSpaceRemoveShape(cpShapeGetSpace(src->shape), src->shape); ShapePolygon::setup(src); cpShapeSetBody(shape, body); cpSpaceAddShape(cpBodyGetSpace(body), shape); }
void PhysicsShapeInfo::setBody(cpBody* body) { if (this->_body != body) { this->_body = body; for (cpShape* shape : _shapes) { cpShapeSetBody(shape, body == nullptr ? _sharedBody : body); } } }
void PhysicsShape::setBody(PhysicsBody *body) { // already added if (body && _body == body) { return; } if (_body) { _body->removeShape(this); } for (auto shape : _cpShapes) { cpShapeSetBody(shape, body == nullptr ? s_sharedBody : body->_cpBody); } _body = body; }
void shape::body( cpBody *body ) { // Can't change the body if it's in the space if( this->m_space ) return; // Maybe a throw is appropriate here? cpShapeSetBody( this->m_shape, body ); }