Merge remote-tracking branch 'origin/GamePhysics' into GameLogic

Conflicts:
	Code/Game/GameLogic/Player.cpp
This commit is contained in:
Erik Persson 2014-02-14 12:46:27 +01:00
commit 8b92c015ea
9 changed files with 113 additions and 13 deletions

View File

@ -17,7 +17,7 @@ Game::PlayerData::PlayerData()
//sbDesc.quaternion = Oyster::Math::Float3(0, Oyster::Math::pi, 0); //sbDesc.quaternion = Oyster::Math::Float3(0, Oyster::Math::pi, 0);
//create rigid body //create rigid body
Oyster::Physics::ICustomBody* rigidBody = Oyster::Physics::API::Instance().AddCollisionBox(size, Oyster::Math::Float4(0, 0, 0, 1), centerPosition, mass, 0.5f, 0.8f, 0.6f ); Oyster::Physics::ICustomBody* rigidBody = Oyster::Physics::API::Instance().AddCharacter(2.0f, 0.5f, Oyster::Math::Float4(0, 0, 0, 1), centerPosition, mass, 0.5f, 0.8f, 0.6f );
rigidBody->SetAngularFactor(0.0f); rigidBody->SetAngularFactor(0.0f);
//create player with this rigid body //create player with this rigid body
this->player = new Player(rigidBody, Player::PlayerCollision, ObjectSpecialType_Player,0,0); this->player = new Player(rigidBody, Player::PlayerCollision, ObjectSpecialType_Player,0,0);

View File

@ -42,6 +42,8 @@ Player::Player(Oyster::Physics::ICustomBody *rigidBody, void (*EventOnCollision)
key_strafeRight = 0; key_strafeRight = 0;
key_strafeLeft = 0; key_strafeLeft = 0;
this->previousPosition = Oyster::Math::Float3(0,0,0);
this->moveDir = Oyster::Math::Float3(0,0,0); this->moveDir = Oyster::Math::Float3(0,0,0);
this->moveSpeed = 100; this->moveSpeed = 100;
this->previousMoveSpeed = Oyster::Math::Float3(0,0,0); this->previousMoveSpeed = Oyster::Math::Float3(0,0,0);
@ -75,6 +77,7 @@ Player::Player(Oyster::Physics::ICustomBody *rigidBody, Oyster::Physics::ICustom
key_strafeRight = 0; key_strafeRight = 0;
key_strafeLeft = 0; key_strafeLeft = 0;
this->previousPosition = Oyster::Math::Float3(0,0,0);
this->moveDir = Oyster::Math::Float3(0,0,0); this->moveDir = Oyster::Math::Float3(0,0,0);
this->moveSpeed = 100; this->moveSpeed = 100;
this->previousMoveSpeed = Oyster::Math::Float3(0,0,0); this->previousMoveSpeed = Oyster::Math::Float3(0,0,0);
@ -95,12 +98,29 @@ void Player::BeginFrame()
//weapon->Update(0.002f); //weapon->Update(0.002f);
Object::BeginFrame(); Object::BeginFrame();
if(this->moveDir != Oyster::Math::Float3::null) //Oyster::Math::Float3 previousFall = this->previousMoveSpeed*-this->rigidBody->GetState().centerPos.GetNormalized();
//Oyster::Math::Float3 currentFall = this->rigidBody->GetLinearVelocity()*-this->rigidBody->GetState().centerPos.GetNormalized();
if(this->moveDir != Oyster::Math::Float3::null && this->playerState != PLAYER_STATE_JUMPING)
{ {
Oyster::Math::Float3 velocity = this->rigidBody->GetLinearVelocity(); Oyster::Math::Float3 velocity = this->rigidBody->GetLinearVelocity();
Oyster::Math::Float3 lostVelocity = (this->previousMoveSpeed - velocity).GetMagnitude()*this->moveDir; Oyster::Math::Float3 lostVelocity = (this->previousMoveSpeed - velocity).GetMagnitude()*this->moveDir;
this->rigidBody->SetLinearVelocity(velocity + lostVelocity - this->moveDir*this->moveSpeed ); this->rigidBody->SetLinearVelocity(velocity + lostVelocity - this->moveDir*this->moveSpeed );
} }
else
{
if(this->rigidBody->GetLamda() == 1.0f)
{
this->playerState = PLAYER_STATE_WALKING;
}
if(this->moveDir != Oyster::Math::Float3::null)
{
Oyster::Math::Float3 velocity = this->rigidBody->GetLinearVelocity();
this->rigidBody->SetLinearVelocity(velocity - this->moveDir*this->moveSpeed );
}
}
this->moveDir = Oyster::Math::Float3::null; this->moveDir = Oyster::Math::Float3::null;
@ -133,9 +153,11 @@ void Player::BeginFrame()
{ {
this->moveDir.Normalize(); this->moveDir.Normalize();
this->rigidBody->SetLinearVelocity(this->moveDir*this->moveSpeed + this->rigidBody->GetLinearVelocity()); this->rigidBody->SetLinearVelocity(this->moveDir*this->moveSpeed + this->rigidBody->GetLinearVelocity());
this->previousMoveSpeed = this->rigidBody->GetLinearVelocity();
} }
this->previousMoveSpeed = this->rigidBody->GetLinearVelocity();
this->previousPosition = this->rigidBody->GetState().centerPos;
this->weapon->Update(0.01f); this->weapon->Update(0.01f);
} }
@ -212,16 +234,19 @@ void Player::Rotate(const Oyster::Math3D::Float3 lookDir, const Oyster::Math3D::
// this is the camera right vector // this is the camera right vector
this->lookDir = lookDir; this->lookDir = lookDir;
Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1]; //Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1];
this->rigidBody->SetUpAndRight(up, right); //this->rigidBody->SetUpAndRight(up, right);
} }
void Player::Jump() void Player::Jump()
{
if(this->rigidBody->GetLamda() < 1.0f)
{ {
Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1].GetNormalized(); Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1].GetNormalized();
this->rigidBody->ApplyImpulse(up *1500); this->rigidBody->ApplyImpulse(up *1500);
this->playerState = PLAYER_STATE::PLAYER_STATE_JUMPING; this->playerState = PLAYER_STATE::PLAYER_STATE_JUMPING;
} }
}
bool Player::IsWalking() bool Player::IsWalking()
{ {

View File

@ -89,6 +89,7 @@ namespace GameLogic
float key_jump; float key_jump;
Oyster::Math::Float3 previousPosition;
Oyster::Math::Float3 moveDir; Oyster::Math::Float3 moveDir;
Oyster::Math::Float moveSpeed; Oyster::Math::Float moveSpeed;
Oyster::Math::Float3 previousMoveSpeed; Oyster::Math::Float3 previousMoveSpeed;

View File

@ -210,7 +210,7 @@ ICustomBody* API_Impl::AddCharacter(::Oyster::Math::Float height, ::Oyster::Math
this->customBodies.push_back(body); this->customBodies.push_back(body);
state.centerPos = position; state.centerPos = position;
state.reach = Float3(radius, height*0.5f, radius); state.reach = Float3(radius, height, radius);
state.dynamicFrictionCoeff = 0.5f; state.dynamicFrictionCoeff = 0.5f;
state.staticFrictionCoeff = 0.5f; state.staticFrictionCoeff = 0.5f;
state.quaternion = Quaternion(Float3(rotation.xyz), rotation.w); state.quaternion = Quaternion(Float3(rotation.xyz), rotation.w);
@ -230,7 +230,9 @@ void API_Impl::UpdateWorld()
{ {
for(unsigned int i = 0; i < this->customBodies.size(); i++ ) for(unsigned int i = 0; i < this->customBodies.size(); i++ )
{ {
SimpleRigidBody* simpleBody = dynamic_cast<SimpleRigidBody*>(this->customBodies[i]);
this->customBodies[i]->SetGravity(-(this->customBodies[i]->GetState().centerPos - this->gravityPoint).GetNormalized()*this->gravity); this->customBodies[i]->SetGravity(-(this->customBodies[i]->GetState().centerPos - this->gravityPoint).GetNormalized()*this->gravity);
simpleBody->PreStep(this->dynamicsWorld);
} }
this->dynamicsWorld->stepSimulation(this->timeStep, 1, this->timeStep); this->dynamicsWorld->stepSimulation(this->timeStep, 1, this->timeStep);

View File

@ -345,3 +345,60 @@ void SimpleRigidBody::SetCustomTag( void *ref )
{ {
this->customTag = ref; this->customTag = ref;
} }
void SimpleRigidBody::PreStep (const btCollisionWorld* collisionWorld)
{
btTransform xform;
this->rigidBody->getMotionState()->getWorldTransform (xform);
btVector3 down = -xform.getBasis()[1];
btVector3 forward = xform.getBasis()[2];
down.normalize ();
forward.normalize();
this->raySource[0] = xform.getOrigin();
this->raySource[1] = xform.getOrigin();
this->rayTarget[0] = this->raySource[0] + down * this->state.reach.y * btScalar(1.1);
this->rayTarget[1] = this->raySource[1] + forward * this->state.reach.y * btScalar(1.1);
class ClosestNotMe : public btCollisionWorld::ClosestRayResultCallback
{
public:
ClosestNotMe (btRigidBody* me) : btCollisionWorld::ClosestRayResultCallback(btVector3(0.0, 0.0, 0.0), btVector3(0.0, 0.0, 0.0))
{
m_me = me;
}
virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult& rayResult,bool normalInWorldSpace)
{
if (rayResult.m_collisionObject == m_me)
return 1.0;
return ClosestRayResultCallback::addSingleResult (rayResult, normalInWorldSpace);
}
protected:
btRigidBody* m_me;
};
ClosestNotMe rayCallback(this->rigidBody);
int i = 0;
for (i = 0; i < 2; i++)
{
rayCallback.m_closestHitFraction = 1.0;
if((this->raySource[i] - this->rayTarget[i]).length() != 0)
collisionWorld->rayTest (this->raySource[i], this->rayTarget[i], rayCallback);
if (rayCallback.hasHit())
{
this->rayLambda[i] = rayCallback.m_closestHitFraction;
} else {
this->rayLambda[i] = 1.0;
}
}
}
float SimpleRigidBody::GetLamda() const
{
return this->rayLambda[0];
}

View File

@ -58,11 +58,17 @@ namespace Oyster
void SetCustomTag( void *ref ); void SetCustomTag( void *ref );
void* GetCustomTag() const; void* GetCustomTag() const;
// Class specific // Class specific
void SetCollisionShape(btCollisionShape* shape); void SetCollisionShape(btCollisionShape* shape);
void SetMotionState(btDefaultMotionState* motionState); void SetMotionState(btDefaultMotionState* motionState);
void SetRigidBody(btRigidBody* rigidBody); void SetRigidBody(btRigidBody* rigidBody);
void PreStep(const btCollisionWorld* collisionWorld);
float GetLamda() const;
private: private:
btCollisionShape* collisionShape; btCollisionShape* collisionShape;
@ -76,7 +82,11 @@ namespace Oyster
void *customTag; void *customTag;
::Oyster::Math::Float3 gravity; Math::Float3 gravity;
btVector3 raySource[2];
btVector3 rayTarget[2];
btScalar rayLambda[2];
}; };
} }
} }

View File

@ -179,6 +179,8 @@ namespace Oyster
* @param ref: Anything castable to a void pointer, the engine won't care. * @param ref: Anything castable to a void pointer, the engine won't care.
********************************************************/ ********************************************************/
virtual void SetCustomTag( void *ref ) = 0; virtual void SetCustomTag( void *ref ) = 0;
virtual float GetLamda() const = 0;
}; };
} }
} }

View File

@ -10,9 +10,10 @@ namespace Oyster
{ {
namespace Struct namespace Struct
{ {
inline CustomBodyState::CustomBodyState( ::Oyster::Math::Float mass, ::Oyster::Math::Float restitutionCoeff, ::Oyster::Math::Float staticFrictionCoeff, ::Oyster::Math::Float dynamicFrictionCoeff, const ::Oyster::Math::Float3 &centerPos, const ::Oyster::Math::Quaternion& quaternion) inline CustomBodyState::CustomBodyState( ::Oyster::Math::Float mass, ::Oyster::Math::Float3 reach, ::Oyster::Math::Float restitutionCoeff, ::Oyster::Math::Float staticFrictionCoeff, ::Oyster::Math::Float dynamicFrictionCoeff, const ::Oyster::Math::Float3 &centerPos, const ::Oyster::Math::Quaternion& quaternion)
{ {
this->mass = mass; this->mass = mass;
this->reach = reach;
this->restitutionCoeff = restitutionCoeff; this->restitutionCoeff = restitutionCoeff;
this->staticFrictionCoeff = staticFrictionCoeff; this->staticFrictionCoeff = staticFrictionCoeff;
this->dynamicFrictionCoeff = dynamicFrictionCoeff; this->dynamicFrictionCoeff = dynamicFrictionCoeff;
@ -24,6 +25,7 @@ namespace Oyster
{ {
this->mass = state.mass; this->mass = state.mass;
this->restitutionCoeff = state.restitutionCoeff; this->restitutionCoeff = state.restitutionCoeff;
this->reach = state.reach;
this->staticFrictionCoeff = state.staticFrictionCoeff; this->staticFrictionCoeff = state.staticFrictionCoeff;
this->dynamicFrictionCoeff = state.dynamicFrictionCoeff; this->dynamicFrictionCoeff = state.dynamicFrictionCoeff;
this->centerPos = state.centerPos; this->centerPos = state.centerPos;

View File

@ -16,6 +16,7 @@ namespace Oyster
public: public:
// Default constructor // Default constructor
CustomBodyState( ::Oyster::Math::Float mass = 1.0f, CustomBodyState( ::Oyster::Math::Float mass = 1.0f,
::Oyster::Math::Float3 reach = ::Oyster::Math::Float3(0,0,0),
::Oyster::Math::Float restitutionCoeff = 0.5f, ::Oyster::Math::Float restitutionCoeff = 0.5f,
::Oyster::Math::Float staticFrictionCoeff = 1.0f, ::Oyster::Math::Float staticFrictionCoeff = 1.0f,
::Oyster::Math::Float dynamicFrictionCoeff = 1.0f, ::Oyster::Math::Float dynamicFrictionCoeff = 1.0f,