void btRigidBody::setupRigidBody(const btRigidBody::btRigidBodyConstructionInfo& constructionInfo) { m_internalType=CO_RIGID_BODY; m_linearVelocity.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0)); m_angularVelocity.setValue(btScalar(0.),btScalar(0.),btScalar(0.)); m_angularFactor.setValue(1,1,1); m_linearFactor.setValue(1,1,1); m_gravity.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0)); m_gravity_acceleration.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0)); m_totalForce.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0)); m_totalTorque.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0)), setDamping(constructionInfo.m_linearDamping, constructionInfo.m_angularDamping); m_linearSleepingThreshold = constructionInfo.m_linearSleepingThreshold; m_angularSleepingThreshold = constructionInfo.m_angularSleepingThreshold; m_optionalMotionState = constructionInfo.m_motionState; m_contactSolverType = 0; m_frictionSolverType = 0; m_additionalDamping = constructionInfo.m_additionalDamping; m_additionalDampingFactor = constructionInfo.m_additionalDampingFactor; m_additionalLinearDampingThresholdSqr = constructionInfo.m_additionalLinearDampingThresholdSqr; m_additionalAngularDampingThresholdSqr = constructionInfo.m_additionalAngularDampingThresholdSqr; m_additionalAngularDampingFactor = constructionInfo.m_additionalAngularDampingFactor; if (m_optionalMotionState) { m_optionalMotionState->getWorldTransform(m_worldTransform); } else { m_worldTransform = constructionInfo.m_startWorldTransform; } m_interpolationWorldTransform = m_worldTransform; m_interpolationLinearVelocity.setValue(0,0,0); m_interpolationAngularVelocity.setValue(0,0,0); //moved to btCollisionObject m_friction = constructionInfo.m_friction; m_restitution = constructionInfo.m_restitution; setCollisionShape( constructionInfo.m_collisionShape ); m_debugBodyId = uniqueId++; setMassProps(constructionInfo.m_mass, constructionInfo.m_localInertia); updateInertiaTensor(); m_rigidbodyFlags = 0; m_deltaLinearVelocity.setZero(); m_deltaAngularVelocity.setZero(); m_invMass = m_inverseMass*m_linearFactor; m_pushVelocity.setZero(); m_turnVelocity.setZero(); }
RigidBody::RigidBody( const MassProps& massProps,SimdScalar linearDamping,SimdScalar angularDamping,SimdScalar friction,SimdScalar restitution) : m_gravity(0.0f, 0.0f, 0.0f), m_totalForce(0.0f, 0.0f, 0.0f), m_totalTorque(0.0f, 0.0f, 0.0f), m_linearVelocity(0.0f, 0.0f, 0.0f), m_angularVelocity(0.f,0.f,0.f), m_linearDamping(0.f), m_angularDamping(0.5f), m_kinematicTimeStep(0.f), m_contactSolverType(0), m_frictionSolverType(0) { //moved to CollisionObject m_friction = friction; m_restitution = restitution; m_debugBodyId = uniqueId++; setMassProps(massProps.m_mass, massProps.m_inertiaLocal); setDamping(linearDamping, angularDamping); m_worldTransform.setIdentity(); updateInertiaTensor(); }
bool FrustumPhysicsObject::frustumInit(unsigned int attributeIndex,short collisionFilterGroup) { if(attributeIndex < 0) { return false; } attributeIndex_ = attributeIndex; collisionFilterGroup_ = collisionFilterGroup; AttributePtr<Attribute_Camera> ptr_camera = itrCamera_3.at(attributeIndex); btVector3 localInertia; localInertia.setZero(); btCollisionShape* collisionShape = CollisionShapes::Instance()->getFrustumShape(attributeIndex_); btScalar mass = static_cast<btScalar>(1); setMassProps(mass, localInertia); setCollisionShape(collisionShape); btTransform world; AttributePtr<Attribute_Spatial> ptr_spatial = itrCamera_3.at(attributeIndex_)->ptr_spatial; AttributePtr<Attribute_Position> ptr_position = ptr_spatial->ptr_position; world.setOrigin(convert(ptr_position->position)); world.setRotation(convert(ptr_spatial->rotation)); setWorldTransform(world); setCollisionFlags(getCollisionFlags() | CF_NO_CONTACT_RESPONSE); forceActivationState(DISABLE_DEACTIVATION); return true; }
FractureBody::FractureBody(const FractureBodyInfo & info) : btRigidBody(btRigidBodyConstructionInfo(1, 0, info.m_shape, btVector3(1, 1, 1))), m_connections(info.m_connections), m_motionState(*this) { m_internalType = CO_FRACTURE_TYPE | CO_RIGID_BODY; // calculate center of mass and inertia m_centerOfMassOffset = -info.m_massCenter / info.m_mass; btVector3 inertia = info.m_inertia - getPrincipalInertia(m_centerOfMassOffset, info.m_mass); setMassProps(info.m_mass, inertia); // shift children shapes due to new center of mass for (int i = 0; i < info.m_shape->getNumChildShapes(); ++i) { info.m_shape->getChildTransform(i).getOrigin() += m_centerOfMassOffset; } info.m_shape->recalculateLocalAabb(); // set motion states if (info.m_states.size() == m_connections.size() + 1) { info.m_states[0]->massCenterOffset = m_centerOfMassOffset; new (&m_motionState) FrMotionState(*this, info.m_states[0]); setMotionState(&m_motionState); for (int i = 0; i < m_connections.size(); ++i) { m_connections[i].m_body->setMotionState(info.m_states[i + 1]); } } }
void RigidSphereBody::setRadius(qreal radius){ if(m_radius!=radius){ m_radius=radius; delete m_shape; m_shape = new btSphereShape(m_radius); m_rigidBody->setCollisionShape(m_shape); setMassProps(); } }
bool PhysicsObject::init(unsigned int attributeIndex, short collisionFilterGroup) { if(attributeIndex < 0) { return false; } attributeIndex_ = attributeIndex; collisionFilterGroup_ = collisionFilterGroup; //Get the init data from a physics attribute AttributePtr<Attribute_Physics> ptr_physics = itrPhysics_.at(attributeIndex); btScalar mass = static_cast<btScalar>(ptr_physics->mass); //Resolve mass, local inertia of the collision shape, and also the collision shape itself. btCollisionShape* collisionShape = subClassSpecificCollisionShape(); if(collisionShape != nullptr) { setCollisionShape(collisionShape); } else { ERROR_MESSAGEBOX("Error in PhysicsObject::init. Expected collision shape pointer unexpectedly set to nullptr. Using default shape instead."); setCollisionShape(CollisionShapes::Instance()->getDefaultShape()); } btVector3 localInertia = subClassCalculateLocalInertiaHook(mass); setMassProps(mass, localInertia); //Set inverse mass and inverse local inertia updateInertiaTensor(); if((getCollisionFlags() & btCollisionObject::CF_STATIC_OBJECT)) { btTransform world; AttributePtr<Attribute_Spatial> ptr_spatial = itrPhysics_.at(attributeIndex_)->ptr_spatial; AttributePtr<Attribute_Position> ptr_position = ptr_spatial->ptr_position; world.setOrigin(convert(ptr_position->position)); world.setRotation(convert(ptr_spatial->rotation)); setWorldTransform(world); //Static physics objects: transform once } else { //Non-static physics objects: let a derived btMotionState handle transforms. MotionState* customMotionState = new MotionState(attributeIndex); setMotionState(customMotionState); setAngularVelocity(btVector3(ptr_physics->angularVelocity.x, ptr_physics->angularVelocity.y, ptr_physics->angularVelocity.z)); setLinearVelocity(btVector3(ptr_physics->linearVelocity.x, ptr_physics->linearVelocity.y, ptr_physics->linearVelocity.z)); //Gravity is set after "addRigidBody" for non-static physics objects } if(ptr_physics->collisionResponse) { setCollisionFlags(getCollisionFlags() & ~CF_NO_CONTACT_RESPONSE); //Activate collision response } else { setCollisionFlags(getCollisionFlags() | CF_NO_CONTACT_RESPONSE); //Deactivate collision response } return subClassSpecificInitHook(); }