From 26e151f9b4fe01fd3d6f11d75f876fc194af2421 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Mon, 10 Feb 2014 15:46:55 +0100 Subject: [PATCH] Fixed state problem --- Code/Game/GameLogic/Game_PlayerData.cpp | 2 +- Code/Game/GameLogic/Level.cpp | 4 +-- Code/Game/GameLogic/Object.cpp | 7 ++--- .../Implementation/PhysicsAPI_Impl.cpp | 30 +++++++++++++++++++ .../Implementation/SimpleRigidBody.cpp | 8 +++-- Code/GamePhysics/PhysicsStructs.h | 2 +- 6 files changed, 42 insertions(+), 11 deletions(-) diff --git a/Code/Game/GameLogic/Game_PlayerData.cpp b/Code/Game/GameLogic/Game_PlayerData.cpp index 0cb4e4bd..5b99b1ee 100644 --- a/Code/Game/GameLogic/Game_PlayerData.cpp +++ b/Code/Game/GameLogic/Game_PlayerData.cpp @@ -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); diff --git a/Code/Game/GameLogic/Level.cpp b/Code/Game/GameLogic/Level.cpp index 364972df..d39800ac 100644 --- a/Code/Game/GameLogic/Level.cpp +++ b/Code/Game/GameLogic/Level.cpp @@ -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++; diff --git a/Code/Game/GameLogic/Object.cpp b/Code/Game/GameLogic/Object.cpp index f099be18..ded2148f 100644 --- a/Code/Game/GameLogic/Object.cpp +++ b/Code/Game/GameLogic/Object.cpp @@ -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 diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 7f3a53a7..9026a60f 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -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; } diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index ab8925a8..879ff8bc 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -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); } diff --git a/Code/GamePhysics/PhysicsStructs.h b/Code/GamePhysics/PhysicsStructs.h index 4c4ae797..fa6c023c 100644 --- a/Code/GamePhysics/PhysicsStructs.h +++ b/Code/GamePhysics/PhysicsStructs.h @@ -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 ¢erPos = ::Oyster::Math::Float3::null,