Fixed state problem

This commit is contained in:
Robin Engman 2014-02-10 15:46:55 +01:00
parent 55a2c6593c
commit 26e151f9b4
6 changed files with 42 additions and 11 deletions

View File

@ -15,7 +15,7 @@ Game::PlayerData::PlayerData()
//sbDesc.quaternion = Oyster::Math::Float3(0, Oyster::Math::pi, 0);
//create rigid body
Oyster::Physics::ICustomBody* rigidBody = Oyster::Physics::API::Instance().AddCollisionBox(size, Oyster::Math::Float4(0, 0, 0, 1), centerPosition, mass);
Oyster::Physics::ICustomBody* rigidBody = Oyster::Physics::API::Instance().AddCollisionBox(size, Oyster::Math::Float4(0, 0, 0, 1), centerPosition, mass, 1, 1, 1);
//create player with this rigid body
this->player = new Player(rigidBody,Player::DefaultCollisionAfter, Player::PlayerCollision, OBJECT_TYPE::OBJECT_TYPE_PLAYER);

View File

@ -55,7 +55,7 @@ void Level::InitiateLevel(float radius)
int idCount = 100;
// add level sphere
ICustomBody* rigidBody = API::Instance().AddCollisionSphere(599.2f, Oyster::Math::Float4(0, 0, 0, 1), Oyster::Math::Float3(0, 0, 0), 0);
ICustomBody* rigidBody = API::Instance().AddCollisionSphere(599.2f, Oyster::Math::Float4(0, 0, 0, 1), Oyster::Math::Float3(0, 0, 0), 0, 1, 1, 1);
ICustomBody::State state;
rigidBody->GetState(state);
@ -72,7 +72,7 @@ void Level::InitiateLevel(float radius)
int offset = 0;
for(int i =0; i< nrOfBoxex; i ++)
{
rigidBody_TestBox = API::Instance().AddCollisionBox(Oyster::Math::Float3(0.5f, 0.5f, 0.5f), Oyster::Math::Float4(0, 0, 0, 1), Oyster::Math::Float3(0, 605 + i*5, 10), 5);
rigidBody_TestBox = API::Instance().AddCollisionBox(Oyster::Math::Float3(0.5f, 0.5f, 0.5f), Oyster::Math::Float4(0, 0, 0, 1), Oyster::Math::Float3(0, 605 + i*5, 10), 5, 1, 1, 1);
this->dynamicObjects.Push(new DynamicObject(rigidBody_TestBox,Object::DefaultCollisionBefore, Object::DefaultCollisionAfter, OBJECT_TYPE::OBJECT_TYPE_BOX));
this->dynamicObjects[i]->objectID = idCount++;

View File

@ -16,7 +16,7 @@ const Game *Object::gameInstance = (Game*)(&Game::Instance());
Object::Object()
{
this->rigidBody = API::Instance().AddCollisionBox(Oyster::Math::Float3(0.0f, 0.0f, 0.0f), Oyster::Math::Float4(0, 0, 0, 1), Oyster::Math::Float3(0, 0, 0), 0);
this->rigidBody = API::Instance().AddCollisionBox(Oyster::Math::Float3(0.0f, 0.0f, 0.0f), Oyster::Math::Float4(0, 0, 0, 1), Oyster::Math::Float3(0, 0, 0), 0, 1, 1, 1);
this->type = OBJECT_TYPE::OBJECT_TYPE_UNKNOWN;
this->objectID = GID();
@ -26,7 +26,7 @@ Object::Object()
Object::Object(OBJECT_TYPE type)
{
this->rigidBody = API::Instance().AddCollisionBox(Oyster::Math::Float3(0.0f, 0.0f, 0.0f), Oyster::Math::Float4(0, 0, 0, 1), Oyster::Math::Float3(0, 0, 0), 0);
this->rigidBody = API::Instance().AddCollisionBox(Oyster::Math::Float3(0.0f, 0.0f, 0.0f), Oyster::Math::Float4(0, 0, 0, 1), Oyster::Math::Float3(0, 0, 0), 0, 1, 1, 1);
this->type = type;
this->objectID = GID();
this->currPhysicsState = this->rigidBody->GetState();
@ -44,7 +44,7 @@ Object::Object(Oyster::Physics::ICustomBody *rigidBody, OBJECT_TYPE type)
Object::Object(void* collisionFuncBefore, void* collisionFuncAfter, OBJECT_TYPE type)
{
this->rigidBody = API::Instance().AddCollisionBox(Oyster::Math::Float3(0.0f, 0.0f, 0.0f), Oyster::Math::Float4(0, 0, 0, 1), Oyster::Math::Float3(0, 0, 0), 0);
this->rigidBody = API::Instance().AddCollisionBox(Oyster::Math::Float3(0.0f, 0.0f, 0.0f), Oyster::Math::Float4(0, 0, 0, 1), Oyster::Math::Float3(0, 0, 0), 0, 1, 1, 1);
this->type = type;
this->objectID = GID();
@ -101,7 +101,6 @@ Oyster::Physics::ICustomBody* Object::GetRigidBody()
void Object::BeginFrame()
{
this->rigidBody->SetState(this->newPhysicsState);
}
// update physic

View File

@ -50,6 +50,7 @@ API_Impl::~API_Impl()
ICustomBody* API_Impl::AddCollisionSphere(float radius, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction)
{
SimpleRigidBody* body = new SimpleRigidBody;
SimpleRigidBody::State state;
// Add collision shape
btCollisionShape* collisionShape = new btSphereShape(radius);
@ -73,12 +74,22 @@ ICustomBody* API_Impl::AddCollisionSphere(float radius, ::Oyster::Math::Float4 r
this->dynamicsWorld->addRigidBody(rigidBody);
this->customBodies.push_back(body);
state.centerPos = position;
state.reach = Float3(radius, radius, radius);
state.dynamicFrictionCoeff = 0.5f;
state.staticFrictionCoeff = 0.5f;
state.quaternion = Quaternion(Float3(rotation.xyz), rotation.w);
state.mass = mass;
body->SetState(state);
return body;
}
ICustomBody* API_Impl::AddCollisionBox(Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction)
{
SimpleRigidBody* body = new SimpleRigidBody;
SimpleRigidBody::State state;
// Add collision shape
btCollisionShape* collisionShape = new btBoxShape(btVector3(halfSize.x, halfSize.y, halfSize.z));
@ -102,12 +113,22 @@ ICustomBody* API_Impl::AddCollisionBox(Float3 halfSize, ::Oyster::Math::Float4 r
this->dynamicsWorld->addRigidBody(rigidBody);
this->customBodies.push_back(body);
state.centerPos = position;
state.reach = halfSize;
state.dynamicFrictionCoeff = 0.5f;
state.staticFrictionCoeff = 0.5f;
state.quaternion = Quaternion(Float3(rotation.xyz), rotation.w);
state.mass = mass;
body->SetState(state);
return body;
}
ICustomBody* API_Impl::AddCollisionCylinder(::Oyster::Math::Float3 halfSize, ::Oyster::Math::Float4 rotation, ::Oyster::Math::Float3 position, float mass, float restitution, float staticFriction, float dynamicFriction)
{
SimpleRigidBody* body = new SimpleRigidBody;
SimpleRigidBody::State state;
// Add collision shape
btCollisionShape* collisionShape = new btCylinderShape(btVector3(halfSize.x, halfSize.y, halfSize.z));
@ -131,6 +152,15 @@ ICustomBody* API_Impl::AddCollisionCylinder(::Oyster::Math::Float3 halfSize, ::O
this->dynamicsWorld->addRigidBody(rigidBody);
this->customBodies.push_back(body);
state.centerPos = position;
state.reach = halfSize;
state.dynamicFrictionCoeff = 0.5f;
state.staticFrictionCoeff = 0.5f;
state.quaternion = Quaternion(Float3(rotation.xyz), rotation.w);
state.mass = mass;
body->SetState(state);
return body;
}

View File

@ -52,9 +52,11 @@ SimpleRigidBody::State& SimpleRigidBody::GetState( SimpleRigidBody::State &targe
void SimpleRigidBody::SetState( const SimpleRigidBody::State &state )
{
btTransform trans;
btVector3 position(state.centerPos.x, state.centerPos.y, state.centerPos.z);
btQuaternion quaternion(state.quaternion.imaginary.x, state.quaternion.imaginary.y, state.quaternion.imaginary.z, state.quaternion.real);
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));
trans.setRotation(quaternion);
trans.setOrigin(position);
this->motionState->setWorldTransform(trans);
this->rigidBody->setFriction(state.staticFrictionCoeff);
this->rigidBody->setRestitution(state.restitutionCoeff);
@ -154,7 +156,7 @@ Float4x4 SimpleRigidBody::GetView( const ::Oyster::Math::Float3 &offset ) const
void SimpleRigidBody::CallSubscription_AfterCollisionResponse(ICustomBody* bodyA, ICustomBody* bodyB, Oyster::Math::Float kineticEnergyLoss)
{
if(this->onMovement)
if(this->afterCollision)
this->afterCollision(bodyA, bodyB, kineticEnergyLoss);
}

View File

@ -16,7 +16,7 @@ namespace Oyster
public:
// Default constructor
CustomBodyState( ::Oyster::Math::Float mass = 1.0f,
::Oyster::Math::Float restitutionCoeff = 1.0f,
::Oyster::Math::Float restitutionCoeff = 0.5f,
::Oyster::Math::Float staticFrictionCoeff = 1.0f,
::Oyster::Math::Float dynamicFrictionCoeff = 1.0f,
const ::Oyster::Math::Float3 &centerPos = ::Oyster::Math::Float3::null,