2013-11-21 17:22:13 +01:00
|
|
|
#include "SimpleRigidBody.h"
|
2013-11-25 16:35:56 +01:00
|
|
|
#include "PhysicsAPI_Impl.h"
|
2013-11-21 17:22:13 +01:00
|
|
|
|
|
|
|
using namespace ::Oyster::Physics;
|
2013-11-28 11:58:46 +01:00
|
|
|
using namespace ::Oyster::Physics3D;
|
2013-11-25 16:35:56 +01:00
|
|
|
using namespace ::Oyster::Math3D;
|
2013-11-21 17:22:13 +01:00
|
|
|
using namespace ::Oyster::Collision3D;
|
|
|
|
using namespace ::Utility::DynamicMemory;
|
2013-11-25 16:35:56 +01:00
|
|
|
using namespace ::Utility::Value;
|
2013-11-21 17:22:13 +01:00
|
|
|
|
2013-11-26 13:27:34 +01:00
|
|
|
SimpleRigidBody::SimpleRigidBody()
|
2013-11-28 11:58:46 +01:00
|
|
|
{
|
2014-02-09 21:24:09 +01:00
|
|
|
this->collisionShape = NULL;
|
|
|
|
this->motionState = NULL;
|
|
|
|
this->rigidBody = NULL;
|
|
|
|
|
|
|
|
this->state.centerPos = Float3(0.0f, 0.0f, 0.0f);
|
|
|
|
this->state.quaternion = Quaternion(Float3(0.0f, 0.0f, 0.0f), 1.0f);
|
|
|
|
this->state.dynamicFrictionCoeff = 0.0f;
|
|
|
|
this->state.staticFrictionCoeff = 0.0f;
|
|
|
|
this->state.mass = 0.0f;
|
|
|
|
this->state.restitutionCoeff = 0.0f;
|
|
|
|
this->state.reach = Float3(0.0f, 0.0f, 0.0f);
|
2014-02-03 15:48:42 +01:00
|
|
|
|
2014-02-10 13:55:01 +01:00
|
|
|
this->afterCollision = NULL;
|
|
|
|
this->onMovement = NULL;
|
2013-12-19 21:39:20 +01:00
|
|
|
|
2014-01-20 13:44:12 +01:00
|
|
|
this->customTag = nullptr;
|
2013-11-28 11:58:46 +01:00
|
|
|
}
|
2013-11-21 17:22:13 +01:00
|
|
|
|
2014-02-09 21:24:09 +01:00
|
|
|
SimpleRigidBody::~SimpleRigidBody()
|
2013-11-21 17:22:13 +01:00
|
|
|
{
|
2014-02-09 21:24:09 +01:00
|
|
|
delete this->motionState;
|
|
|
|
this->motionState = NULL;
|
|
|
|
delete this->collisionShape;
|
|
|
|
this->collisionShape = NULL;
|
|
|
|
delete this->rigidBody;
|
|
|
|
this->rigidBody = NULL;
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:50:40 +01:00
|
|
|
SimpleRigidBody::State SimpleRigidBody::GetState() const
|
|
|
|
{
|
|
|
|
return this->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleRigidBody::State& SimpleRigidBody::GetState( SimpleRigidBody::State &targetMem ) const
|
|
|
|
{
|
|
|
|
targetMem = this->state;
|
|
|
|
return targetMem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetState( const SimpleRigidBody::State &state )
|
|
|
|
{
|
|
|
|
btTransform trans;
|
|
|
|
this->motionState->getWorldTransform(trans);
|
|
|
|
trans.setRotation(btQuaternion(state.quaternion.imaginary.x, state.quaternion.imaginary.y, state.quaternion.imaginary.z, state.quaternion.real));
|
|
|
|
trans.setOrigin(btVector3(state.centerPos.x, state.centerPos.y, state.centerPos.z));
|
|
|
|
this->motionState->setWorldTransform(trans);
|
|
|
|
this->rigidBody->setFriction(state.staticFrictionCoeff);
|
|
|
|
this->rigidBody->setRestitution(state.restitutionCoeff);
|
|
|
|
btVector3 fallInertia(0, 0, 0);
|
|
|
|
collisionShape->calculateLocalInertia(state.mass, fallInertia);
|
|
|
|
this->rigidBody->setMassProps(state.mass, fallInertia);
|
|
|
|
|
|
|
|
this->state = state;
|
|
|
|
}
|
|
|
|
|
2014-02-09 21:24:09 +01:00
|
|
|
void SimpleRigidBody::SetCollisionShape(btCollisionShape* shape)
|
2013-12-06 09:46:30 +01:00
|
|
|
{
|
2014-02-09 21:24:09 +01:00
|
|
|
this->collisionShape = shape;
|
2013-12-06 09:46:30 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 21:24:09 +01:00
|
|
|
void SimpleRigidBody::SetMotionState(btDefaultMotionState* motionState)
|
2013-12-06 09:46:30 +01:00
|
|
|
{
|
2014-02-09 21:24:09 +01:00
|
|
|
this->motionState = motionState;
|
2013-12-06 09:46:30 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 21:24:09 +01:00
|
|
|
void SimpleRigidBody::SetRigidBody(btRigidBody* rigidBody)
|
2013-12-19 10:53:55 +01:00
|
|
|
{
|
2014-02-09 21:24:09 +01:00
|
|
|
this->rigidBody = rigidBody;
|
|
|
|
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 21:24:09 +01:00
|
|
|
void SimpleRigidBody::SetSubscription(EventAction_AfterCollisionResponse function)
|
2013-11-21 17:22:13 +01:00
|
|
|
{
|
2014-02-09 21:24:09 +01:00
|
|
|
this->afterCollision = function;
|
2013-12-18 08:57:27 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 13:55:01 +01:00
|
|
|
void SimpleRigidBody::SetSubscription(EventAction_Move function)
|
|
|
|
{
|
|
|
|
this->onMovement = function;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:18:45 +01:00
|
|
|
void SimpleRigidBody::SetLinearVelocity(Float3 velocity)
|
|
|
|
{
|
|
|
|
this->rigidBody->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:39:45 +01:00
|
|
|
void SimpleRigidBody::SetPosition(::Oyster::Math::Float3 position)
|
|
|
|
{
|
|
|
|
btTransform trans;
|
|
|
|
this->motionState->getWorldTransform(trans);
|
|
|
|
trans.setOrigin(btVector3(position.x, position.y, position.z));
|
|
|
|
this->motionState->setWorldTransform(trans);
|
|
|
|
this->state.centerPos = position;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:18:45 +01:00
|
|
|
void SimpleRigidBody::SetRotation(Float4 quaternion)
|
|
|
|
{
|
|
|
|
btTransform trans;
|
|
|
|
this->motionState->getWorldTransform(trans);
|
|
|
|
trans.setRotation(btQuaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w));
|
|
|
|
this->motionState->setWorldTransform(trans);
|
2014-02-10 14:39:45 +01:00
|
|
|
this->state.quaternion = Quaternion(quaternion.xyz, quaternion.w);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetRotation(::Oyster::Math::Quaternion quaternion)
|
|
|
|
{
|
|
|
|
btTransform trans;
|
|
|
|
this->motionState->getWorldTransform(trans);
|
|
|
|
trans.setRotation(btQuaternion(quaternion.imaginary.x, quaternion.imaginary.y, quaternion.imaginary.z, quaternion.real));
|
|
|
|
this->motionState->setWorldTransform(trans);
|
|
|
|
this->state.quaternion = quaternion;
|
2014-02-10 14:18:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::SetRotation(Float3 eulerAngles)
|
|
|
|
{
|
|
|
|
btTransform trans;
|
|
|
|
this->motionState->getWorldTransform(trans);
|
|
|
|
trans.setRotation(btQuaternion(eulerAngles.x, eulerAngles.y, eulerAngles.z));
|
|
|
|
this->motionState->setWorldTransform(trans);
|
2014-02-10 14:39:45 +01:00
|
|
|
this->state.quaternion = Quaternion(Float3(trans.getRotation().x(), trans.getRotation().y(), trans.getRotation().z()), trans.getRotation().w());
|
|
|
|
}
|
|
|
|
|
|
|
|
Float4x4 SimpleRigidBody::GetRotation() const
|
|
|
|
{
|
|
|
|
return this->state.GetRotation();
|
|
|
|
}
|
|
|
|
|
|
|
|
Float4x4 SimpleRigidBody::GetOrientation() const
|
|
|
|
{
|
|
|
|
return this->state.GetOrientation();
|
|
|
|
}
|
|
|
|
|
|
|
|
Float4x4 SimpleRigidBody::GetView() const
|
|
|
|
{
|
|
|
|
return this->state.GetView();
|
|
|
|
}
|
|
|
|
|
|
|
|
Float4x4 SimpleRigidBody::GetView( const ::Oyster::Math::Float3 &offset ) const
|
|
|
|
{
|
|
|
|
return this->state.GetView(offset);
|
2014-02-10 14:18:45 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 13:55:01 +01:00
|
|
|
void SimpleRigidBody::CallSubscription_AfterCollisionResponse(ICustomBody* bodyA, ICustomBody* bodyB, Oyster::Math::Float kineticEnergyLoss)
|
2013-12-18 08:57:27 +01:00
|
|
|
{
|
2014-02-10 13:55:01 +01:00
|
|
|
if(this->onMovement)
|
|
|
|
this->afterCollision(bodyA, bodyB, kineticEnergyLoss);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleRigidBody::CallSubscription_Move()
|
|
|
|
{
|
|
|
|
if(this->onMovement)
|
|
|
|
this->onMovement(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
btCollisionShape* SimpleRigidBody::GetCollisionShape() const
|
|
|
|
{
|
|
|
|
return this->collisionShape;
|
2013-11-21 17:22:13 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 21:24:09 +01:00
|
|
|
btDefaultMotionState* SimpleRigidBody::GetMotionState() const
|
2014-02-03 15:48:42 +01:00
|
|
|
{
|
2014-02-09 21:24:09 +01:00
|
|
|
return this->motionState;
|
2014-02-03 15:48:42 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 13:55:01 +01:00
|
|
|
btRigidBody* SimpleRigidBody::GetRigidBody() const
|
|
|
|
{
|
|
|
|
return this->rigidBody;
|
|
|
|
}
|
|
|
|
|
2014-01-20 13:44:12 +01:00
|
|
|
void * SimpleRigidBody::GetCustomTag() const
|
|
|
|
{
|
|
|
|
return this->customTag;
|
|
|
|
}
|
|
|
|
|
2013-11-26 13:27:34 +01:00
|
|
|
|
2014-01-20 13:44:12 +01:00
|
|
|
void SimpleRigidBody::SetCustomTag( void *ref )
|
|
|
|
{
|
|
|
|
this->customTag = ref;
|
|
|
|
}
|
|
|
|
|
2014-02-09 21:24:09 +01:00
|
|
|
|