void RaceCircuit::create() { setMargin(2.f); const float scaling = PhysicsState::engine->simulateScale(); btVector3 * pos = createVertexPos(sNumVertices); int i; for(i = 0; i < sNumVertices; i++) { Vector3F q(sMeshVertices[i * 3] - sMeshNormals[i * 3] * margin(), sMeshVertices[i * 3 + 1] - sMeshNormals[i * 3 + 1] * margin(), sMeshVertices[i * 3 + 2] - sMeshNormals[i * 3 + 2] * margin()); q *= scaling; pos[i] = btVector3(q.x, q.y, q.z); } int * idx = createTriangles(sNumTriangleIndices / 3); for(i = 0; i < sNumTriangleIndices; i++) { idx[i] = sMeshTriangleIndices[i]; } btBvhTriangleMeshShape* shp = createCollisionShape(); Matrix44F trans; btRigidBody * bd = PhysicsState::engine->createRigidBody(shp, trans, 0.f); bd->setFriction(.768f); }
void collisionShapeNode::computeCollisionShape(const MPlug& plug, MDataBlock& data) { //std::cout << collisionShapeNode::collisionMarginOffset << std::endl; MObject thisObject(thisMObject()); MPlug plgInShape(thisObject, ia_shape); if (isCompound(plgInShape)) { m_collision_shape = createCompositeShape(plgInShape); } else { MObject thisObject(thisMObject()); MPlug plgType(thisObject, ia_type); int type; plgType.getValue(type); switch(type) { case 4: //box m_collision_shape = solver_t::create_box_shape(); break; case 5: //sphere m_collision_shape = solver_t::create_sphere_shape(); break; case 6: //plane m_collision_shape = solver_t::create_plane_shape(); break; default: { MPlugArray plgaConnectedTo; plgInShape.connectedTo(plgaConnectedTo, true, true); int numSelectedShapes = plgaConnectedTo.length(); if(numSelectedShapes > 0) { MObject node = plgaConnectedTo[0].node(); m_collision_shape = createCollisionShape(node); } } } } //btAssert(m_collision_shape); data.setClean(plug); }
void MeshManager::setCollision(BasicMesh* result, const std::string& line) { std::string number = line.substr(line.find(':')+1); std::istringstream iss(number); int type = CS_NONE; iss >> type; result->setCollisionType(static_cast<CollisionShape>(type)); result->m_collisionShape = createCollisionShape(result); }
BulletRigidBody::BulletRigidBody(RigidBody* pInterface_, BulletDynamicsWorld* dynamicsWorld_, btRigidBody* rigidBody_, const std::string& name_ ) : base_type(pInterface, dynamicsWorld_), rigidBody(rigidBody_) { assert(rigidBody); motionState.reset( new BulletMotionState(this) ); { btTransform transform; if (btMotionState* ms = rigidBody->getMotionState() ) { ms->getWorldTransform(transform); } else { transform = rigidBody->getWorldTransform(); } motionState->setWorldTransform(transform); rigidBody->setMotionState( motionState.get() ); } rigidBody->setUserPointer( pInterface ); // fill up desc pInterface_->desc.transform = getTransform(); if ( rigidBody->getCollisionFlags() & btCollisionObject::CF_KINEMATIC_OBJECT ) { pInterface_->desc.type = RigidBody::DT_KINEMATIC; } else if ( rigidBody->getCollisionFlags() & btCollisionObject::CF_STATIC_OBJECT ) { pInterface_->desc.type = RigidBody::DT_STATIC; } else { pInterface_->desc.type = RigidBody::DT_DYNAMIC; } pInterface_->desc.mass = real(1.0) / rigidBody->getInvMass(); pInterface_->desc.friction = rigidBody->getFriction(); pInterface_->desc.linearVelocity = to_vec( rigidBody->getLinearVelocity() ); pInterface_->desc.angularVelocity = to_vec( rigidBody->getAngularVelocity() ); pInterface_->desc.name = name_; CollisionShape* collisionShape = reinterpret_cast<CollisionShape*>( rigidBody->getCollisionShape()->getUserPointer() ); if (!collisionShape) { collisionShape = createCollisionShape( *rigidBody->getCollisionShape() ); } pInterface_->desc.collisionShape.reset(collisionShape); AUTO_LOGGER_MESSAGE(log::S_FLOOD, "Creating rigid body from btRigidBody:\n" << pInterface_->desc << LOG_FILE_AND_LINE); }
/** Creates the physics body for this triangle mesh. If the body already * exists (because it was created by a previous call to createBody) * it is first removed from the world. This is used by loading the track * where a physics body is used to determine the height of terrain. To have * an optimised rigid body including all static objects, the track is then * removed and all objects together with the track is converted again into * a single rigid body. This avoids using irrlicht (or the graphics engine) * for height of terrain detection). */ void TriangleMesh::createPhysicalBody(btCollisionObject::CollisionFlags flags) { // We need the collision shape, but not the collision object (since // this will be creates when the dynamics body is anyway). createCollisionShape(/*create_collision_object*/false); btTransform startTransform; startTransform.setIdentity(); m_motion_state = new btDefaultMotionState(startTransform); btRigidBody::btRigidBodyConstructionInfo info(0.0f, m_motion_state, m_collision_shape); m_body=new btRigidBody(info); World::getWorld()->getPhysics()->addBody(m_body); m_body->setUserPointer(&m_user_pointer); m_body->setCollisionFlags(m_body->getCollisionFlags() | flags | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK); } // createPhysicalBody
boing::boing(MObject inputShape,MString inname, MString inTypeName, MVector pos, MVector vel, MVector rot, MVector av, float mass) { node = inputShape; name = inname; typeName = inTypeName; m_initial_velocity = vel; m_initial_position = pos; m_initial_rotation = rot; m_initial_angularvelocity = av; m_mass = mass; attrArray = MStringArray(); dataArray = MStringArray(); m_collision_shape = createCollisionShape(node); /*cout<<"m_collision_shape : "<<m_collision_shape<<endl; std::cout<<"creating a new (boing::boing) boing node : "<<inname<<" type "<<inTypeName<<"from node "<<MFnDependencyNode(inputShape).name()<<" in pos "<<m_initial_position<<" vel "<<m_initial_velocity<<" rotation "<<m_initial_rotation<<" and av "<<m_initial_angularvelocity<<endl;*/ count++; std::cout<<"count = "<<count<<std::endl; createRigidBody(); }
/** Creates the physics body for this triangle mesh. If the body already * exists (because it was created by a previous call to createBody) * it is first removed from the world. This is used by loading the track * where a physics body is used to determine the height of terrain. To have * an optimised rigid body including all static objects, the track is then * removed and all objects together with the track is converted again into * a single rigid body. This avoids using irrlicht (or the graphics engine) * for height of terrain detection). * \param friction Friction to be used for this TriangleMesh. * \param flags Additional collision flags (default 0). * \param serializedBhv if non-NULL, the bhv is deserialized instead of * being calculated on the fly */ void TriangleMesh::createPhysicalBody(float friction, btCollisionObject::CollisionFlags flags, const char* serializedBhv) { // We need the collision shape, but not the collision object (since // this will be created when the dynamics body is anyway). createCollisionShape(/*create_collision_object*/false, serializedBhv); btTransform startTransform; startTransform.setIdentity(); m_motion_state = new btDefaultMotionState(startTransform); btRigidBody::btRigidBodyConstructionInfo info(0.0f, m_motion_state, m_collision_shape); info.m_restitution = 0.8f; info.m_friction = friction; m_body=new btRigidBody(info); Physics::getInstance()->addBody(m_body); m_body->setUserPointer(&m_user_pointer); m_body->setCollisionFlags(m_body->getCollisionFlags() | flags | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK); } // createPhysicalBody
collision_shape_t::pointer collisionShapeNode::createCompositeShape(const MPlug& plgInShape) { std::vector<collision_shape_t::pointer> childCollisionShapes; std::vector< vec3f> childPositions; std::vector< quatf> childOrientations; MPlugArray plgaConnectedTo; plgInShape.connectedTo(plgaConnectedTo, true, true); int numSelectedShapes = plgaConnectedTo.length(); if(numSelectedShapes > 0) { MObject node = plgaConnectedTo[0].node(); MDagPath dagPath; MDagPath::getAPathTo(node, dagPath); int numChildren = dagPath.childCount(); if (node.hasFn(MFn::kTransform)) { MFnTransform trNode(dagPath); MVector mtranslation = trNode.getTranslation(MSpace::kTransform); MObject thisObject(thisMObject()); MFnDagNode fnDagNode(thisObject); MStatus status; MFnTransform fnParentTransform(fnDagNode.parent(0, &status)); mtranslation = trNode.getTranslation(MSpace::kPostTransform); mtranslation = fnParentTransform.getTranslation(MSpace::kTransform); const char* name = trNode.name().asChar(); printf("name = %s\n", name); for (int i=0;i<numChildren;i++) { MObject child = dagPath.child(i); if(child.hasFn(MFn::kMesh)) { return false; } if(child.hasFn(MFn::kTransform)) { MDagPath dagPath; MDagPath::getAPathTo(child, dagPath); MFnTransform trNode(dagPath); MVector mtranslation = trNode.getTranslation(MSpace::kTransform); mtranslation = trNode.getTranslation(MSpace::kPostTransform); MQuaternion mrotation; trNode.getRotation(mrotation, MSpace::kTransform); double mscale[3]; trNode.getScale(mscale); vec3f childPos((float)mtranslation.x, (float)mtranslation.y, (float)mtranslation.z); quatf childOrn((float)mrotation.w, (float)mrotation.x, (float)mrotation.y, (float)mrotation.z); const char* name = trNode.name().asChar(); printf("name = %s\n", name); int numGrandChildren = dagPath.childCount(); { for (int i=0;i<numGrandChildren;i++) { MObject grandchild = dagPath.child(i); if(grandchild.hasFn(MFn::kMesh)) { collision_shape_t::pointer childShape = createCollisionShape(grandchild); if (childShape) { childCollisionShapes.push_back(childShape); childPositions.push_back(childPos); childOrientations.push_back(childOrn); } } } } } } } } if (childCollisionShapes.size()>0) { composite_shape_t::pointer composite = solver_t::create_composite_shape( &childCollisionShapes[0], &childPositions[0], &childOrientations[0], childCollisionShapes.size() ); return composite; } return 0; }
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; } }
MStatus boingRbCmd::redoIt() { //MGlobal::getActiveSelectionList(m_undoSelectionList) /* if (argData->isFlagSet("help")) { MGlobal::displayInfo(MString("Tässä olisi helppi-teksti")); return MS::kSuccess; }*/ isSetAttr = argParser->isFlagSet("-setAttr"); isGetAttr = argParser->isFlagSet("-getAttr"); isCreate = argParser->isFlagSet("-create"); isDelete = argParser->isFlagSet("-delete"); isValue = argParser->isFlagSet("-value"); if (isSetAttr && isValue) { MString sAttr; //MArgList argList; argParser->getFlagArgument("setAttr", 0, sAttr); //cout<<sAttr<<endl; MStringArray jobArgsArray = parseArguments(sAttr, "."); /* MString stringBuffer; for (unsigned int charIdx = 0; charIdx < sAttr.numChars(); charIdx++) { MString ch = sAttr.substringW(charIdx, charIdx); //cout<<"ch = "<<ch<<endl; if (ch == ".") { if (stringBuffer.length() > 0) { jobArgsArray.append(stringBuffer); //cout<<"jobArgsArray = "<<jobArgsArray<<endl; stringBuffer.clear(); } } else { stringBuffer += ch; //cout<<"stringBuffer = "<<stringBuffer<<endl; } } jobArgsArray.append(stringBuffer); */ MVector value; argParser->getFlagArgument("-value", 0, value.x); argParser->getFlagArgument("-value", 1, value.y); argParser->getFlagArgument("-value", 2, value.z); //cout<<"jobArgsArray[1] : "<<jobArgsArray[1]<<endl; MString attr = checkAttribute(jobArgsArray[1]); setBulletVectorAttribute(nameToNode(jobArgsArray[0]), attr, value); //cout<<value<<endl; setResult(MS::kSuccess); } else if ( isGetAttr) { MString gAttr; argParser->getFlagArgument("getAttr", 0, gAttr); //cout<<gAttr<<endl; MStringArray jobArgsArray = parseArguments(gAttr, "."); MString attr = checkAttribute(jobArgsArray[1]); //cout<<"attr = "<<attr<<endl; if (attr!="name") { MVector result = getBulletVectorAttribute(nameToNode(jobArgsArray[0]), attr); MDoubleArray dResult; dResult.append(result.x); dResult.append(result.y); dResult.append(result.z); setResult(dResult); } else { MStringArray result; std::set<boingRBNode*> nodes; //bSolverNode::getRigidBodies(result, nodes); //solver_t *solver_t; //shared_ptr<solver_impl_t> m_impl_t = solver_t->get_solver(); //bt_solver_t *solv = bt_solver_t; //shared_ptr<solver_impl_t> solver = solver_t->get_solver(); //cout<<"size(m_rigid_bodies)"<<m_rigid_bodies->length()<<endl; //btDefaultSerializer* serializer = new MySerializer(solver_t->get_solver()); //btHashMap<btHashPtr,const char*> m_nameMap; //for(size_t i = 0; i < m_rigid_bodies.size(); ++i) { //solver_t::remove_rigid_body(m_rigid_bodies[i]); //cout<<"m_rigid_bodies["<<i<<"] : "<<m_rigid_bodies[i]<<endl; //} //cout << "rigid bodies" << endl; //std::set<rigid_body_t::pointer>m_rigid_bodies = solver_t->get_rigid_bodies(); //btDefaultSerializer* serializer = new MySerializer(solv); //btHashMap<btHashPtr,const char*> m_nameMap = bt_solver_t::get_m_nameMap(); //std::set<rigid_body_t::pointer>::iterator it; //unsigned int i=0; //for (it = m_rigid_bodies.begin(); it != m_rigid_bodies.end(); ++it) { //rigid_body_t::pointer body = static_cast<rigid_body_t::pointer>bt_rigid_body_t::body(); //rigid_body_t::pointer body = it->get(); //bt_rigid_body_t::body(); //collision_shape_t::pointer collshape_pointer = (collision_shape_t::pointer)body->collision_shape(); //const char *namePtr = solv->m_nameMap.find(it); // rigid_body_t::pointer m_rigid_body = it->get(); // const rigid_body_impl_t* rb = m_rigid_body->impl(); //const char*const * namePtr = solv->m_nameMap.find(rb); // const char* namePtr = serializer->findNameForPointer(rb); // cout<<++i<<endl; //if (namePtr && *namePtr) { //cout<<namePtr<<endl; // result.append((MString) namePtr); //} else { // continue; //} //cout<<collshape_pointer<<endl; //cout<<"pointer : " << (*)it << endl; //const char* rbname = MySerializer::findNameForPointer(it); //cout<<"rbname = "<<rbname<<endl; //} cout<<"result : "<<result<<endl; setResult(result); } } else if ( isCreate ) { MString aArgument; argParser->getFlagArgument("-create", 0, aArgument); MStringArray createArgs = parseArguments(aArgument,";"); int size = createArgs.length(); MString inputShape; MString rbname = "dummyRb"; MVector vel; MVector pos; MVector rot; for (int i=0; i<size; ++i) { //cout<<"createArgs[i] = "<<createArgs[i]<<endl; MStringArray singleArg = parseArguments(createArgs[i],"="); cout<<"singleArg[0] : "<<singleArg[0]<<endl; cout<<"singleArg[1] : "<<singleArg[1]<<endl; if (singleArg[0] == "name") { //name rbname = singleArg[1]; cout<<"name"<<endl; cout<<"singleArg[0] : "<<singleArg[0]<<endl; cout<<"singleArg[1] : "<<singleArg[1]<<endl; } else if (singleArg[0] == "geo") { //geo inputShape = singleArg[1]; } else if (singleArg[0] == "vel") { //initialvelocity MStringArray velArray = parseArguments(singleArg[1], ","); vel = MVector(velArray[0].asDouble(), velArray[1].asDouble(), velArray[2].asDouble() ); //cout<<"velocity = "<<vel<<endl; } else if (singleArg[0] == "pos") { //initialposition MStringArray posArray = parseArguments(singleArg[1], ","); pos = MVector(posArray[0].asDouble(), posArray[1].asDouble(), posArray[2].asDouble() ); //cout<<"position = "<<pos<<endl; } else if (singleArg[0] == "rot") { //initialrotation MStringArray rotArray = parseArguments(singleArg[1], ","); rot = MVector(rotArray[0].asDouble(), rotArray[1].asDouble(), rotArray[2].asDouble() ); //cout<<"rotation = "<<rot<<endl; } else { cout<<"Unrecognized parameter : "<<singleArg[0]<<endl; } } // create the collisionShape-node MObject node = nameToNode(inputShape); collision_shape_t::pointer collShape = createCollisionShape(node); createRigidBody(collShape, node, rbname, vel, pos, rot); } else if ( isDelete ) { } return MS::kSuccess; }
void registerStart() { collisionShape = createCollisionShape(); collisionShape->setUserPointer(this); }
BasicMesh* MeshManager::loadMesh(const std::string& fileName, const std::string meshName) { shared_ptr<GameFile> file = GameFile::openFile(fileName, "r"); std::string readLine; BasicMesh* result = new BasicMesh(); std::string collisionLine; while(!(file->eof())) { readLine = file->readLine(); if(readLine.size() == 0 || readLine == "\n") { continue; } if(readLine.find(NAME_SECTION) == 0) { loadName(result, readLine); continue; } else if(readLine.find(VERTICES_SECTION) == 0) { loadVertices(result, readLine, file.get()); continue; } else if(readLine.find(UVS_SECTION) == 0) { loadUvs(result, readLine, file.get()); continue; } else if(readLine.find(NORMALS_SECTION) == 0) { loadNormals(result, readLine, file.get()); continue; } else if(readLine.find(INDICES_SECTION) == 0) { loadIndices(result, readLine, file.get()); continue; } else if(readLine.find(COLLISION_SECTION) == 0) { collisionLine = readLine; // it has to be calculated after all the geometry continue; } else { Log("Skipping unknown line in mesh file - %s", readLine.c_str()); } } if(collisionLine.size() != 0) { setCollision(result, collisionLine); } else { // by default create box collision result->setCollisionType(CS_BOX); result->m_collisionShape = createCollisionShape(result); } return result; }