CollisionShape^ CollisionShape::GetManaged(btCollisionShape* collisionShape) { if (collisionShape == 0) { return nullptr; } void* userObj = collisionShape->getUserPointer(); if (userObj) return static_cast<CollisionShape^>(VoidPtrToGCHandle(userObj).Target); // If we reach here, then collisionShape was created from within unmanaged code, // create a wrapper object based on the shape type. BroadphaseNativeType type = (BroadphaseNativeType)collisionShape->getShapeType(); CollisionShape^ shape; switch(type) { case BroadphaseNativeType::TetrahedralShape: // Required for CompoundFromGImpact.Create shape = gcnew BuSimplex1To4((btBU_Simplex1to4*) collisionShape); break; case BroadphaseNativeType::TriangleShape: // Required for ContactCallback from convex-concave algorithm shape = gcnew TriangleShape((btTriangleShape*) collisionShape); break; case BroadphaseNativeType::GImpactShape: // Required for GImpactMeshShape.GetMeshPart shape = gcnew GImpactMeshShapePart((btGImpactMeshShapePart*) collisionShape); break; default: //throw gcnew NotImplementedException(); shape = gcnew CollisionShape(collisionShape); } shape->_preventDelete = true; return shape; }
btCollisionShape * PhysicsObject::readCollisionData(char * path) { SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, false}; CreateDirectory("Collision Shapes", &sa); FILE * file; char * configName = path; //file = fopen(configName, "r"); fopen_s(&file, configName, "r"); if (file == NULL) { fclose(file); return NULL; } //no file found, Failed to Generate Collision Shape fclose(file); list<CollisionShape> * shapes = new list<CollisionShape>(); ifstream fileStream(configName); string s; string var, val; while (fileStream.peek() != -1) { s = var = val = ""; getline(fileStream, s); bool eq = false; int i = 1; if (s == "}" || s == "{") continue; else if (s == "box") { shapes->push_back(CollisionShape()); shapes->back().type = "box"; continue; } else if (s == "sphere") { shapes->push_back(CollisionShape()); shapes->back().type = "sphere"; continue; } else if (s == "capsule") { shapes->push_back(CollisionShape()); shapes->back().type = "capsule"; continue; } else if (s == "cylinderY" || s == "cylinder") { shapes->push_back(CollisionShape()); shapes->back().type = "cylinder"; continue; } else if (s == "cylinderX") { shapes->push_back(CollisionShape()); shapes->back().type = "cylinderX"; continue; } else if (s == "cylinderZ") { shapes->push_back(CollisionShape()); shapes->back().type = "cylinderZ"; continue; } else if (s == "hull") { shapes->push_back(CollisionShape()); shapes->back().type = "hull"; shapes->back().vertices = list<btVector3>(); continue; } else { while (i < (int)s.size()) { if (s[i] == '=') eq = true; else if (eq) val += s[i]; else var += s[i]; i++; } if (var == "pos") shapes->back().pos = val; else if (var == "size") shapes->back().size = val; else if (var == "axis") shapes->back().axis = val; else if (var == "rot") shapes->back().rot = val; else if (var == "vert") shapes->back().vertices.push_back(stringToBTVector3(val)); } } fileStream.close(); if (shapes->size() == 0) return NULL; else if (shapes->size() == 1) { btCollisionShape * colShape = createCollisionShape(&(shapes->front())); shapes->pop_front(); delete shapes; return colShape; } else { btCompoundShape * compoundShape = new btCompoundShape(); int isd = COMPOUND_SHAPE_PROXYTYPE; while (shapes->size() > 0) { btCollisionShape * colShape = createCollisionShape(&shapes->front()); btTransform trans; trans.setIdentity(); trans.setOrigin(stringToBTVector3(shapes->front().pos)); trans.setRotation(btQuaternion(stringToBTVector3(shapes->front().axis), (float)atof(shapes->front().rot.c_str()))); collisionSubShapes->push_back(colShape); compoundShape->addChildShape(trans, colShape); shapes->pop_front(); } delete shapes; return compoundShape; } }