From a90f33d20e00fa243821dfcce609e33633501977 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Mon, 17 Feb 2014 06:44:29 +0100 Subject: [PATCH] Jumpy jump --- Code/Game/GameLogic/Player.cpp | 103 ++++++++++-------- .../Implementation/SimpleRigidBody.cpp | 19 +++- .../Implementation/SimpleRigidBody.h | 2 +- Code/GamePhysics/PhysicsAPI.h | 2 +- 4 files changed, 75 insertions(+), 51 deletions(-) diff --git a/Code/Game/GameLogic/Player.cpp b/Code/Game/GameLogic/Player.cpp index 508857b9..4f26c0b5 100644 --- a/Code/Game/GameLogic/Player.cpp +++ b/Code/Game/GameLogic/Player.cpp @@ -65,7 +65,7 @@ Player::Player(Oyster::Physics::ICustomBody *rigidBody, Oyster::Physics::ICustom this->previousPosition = Oyster::Math::Float3(0,0,0); this->moveDir = Oyster::Math::Float3(0,0,0); - this->moveSpeed = 100; + this->moveSpeed = 20; this->previousMoveSpeed = Oyster::Math::Float3(0,0,0); } @@ -83,67 +83,87 @@ void Player::BeginFrame() { //weapon->Update(0.002f); Object::BeginFrame(); - - //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 lostVelocity = (this->previousMoveSpeed - velocity).GetMagnitude()*this->moveDir; - this->rigidBody->SetLinearVelocity(velocity + lostVelocity - this->moveDir*this->moveSpeed ); - } - else - { + Oyster::Math::Float maxSpeed = 30; - if(this->rigidBody->GetLamda() == 1.0f) - { - this->playerState = PLAYER_STATE_WALKING; - } + Oyster::Math::Float4x4 xform; + xform = this->rigidBody->GetState().GetOrientation(); - if(this->moveDir != Oyster::Math::Float3::null) - { - Oyster::Math::Float3 velocity = this->rigidBody->GetLinearVelocity(); - this->rigidBody->SetLinearVelocity(velocity - this->moveDir*this->moveSpeed ); - } - } + /* Handle turning */ + /*if (left) + m_turnAngle -= dt * m_turnVelocity; + if (right) + m_turnAngle += dt * m_turnVelocity;*/ - this->moveDir = Oyster::Math::Float3::null; + //xform.setRotation (btQuaternion (btVector3(0.0, 1.0, 0.0), m_turnAngle)); + + Oyster::Math::Float3 linearVelocity = this->rigidBody->GetLinearVelocity(); + Oyster::Math::Float speed = this->rigidBody->GetLinearVelocity().GetLength(); + + Oyster::Math::Float3 forwardDir = xform.v[2]; + Oyster::Math::Float3 rightDir = xform.v[0]; + forwardDir.Normalize(); + rightDir.Normalize(); + Oyster::Math::Float3 walkDirection = Oyster::Math::Float3(0.0, 0.0, 0.0); + Oyster::Math::Float walkSpeed = this->moveSpeed*0.2; if (key_forward > 0.001) { - key_forward -= gameInstance->GetFrameTime(); // fixed timer - this->moveDir += this->rigidBody->GetState().GetOrientation().v[2].GetNormalized().xyz; + key_forward -= gameInstance->GetFrameTime(); + walkDirection += forwardDir; + walkDirection.Normalize(); } if (key_backward > 0.001) { key_backward -= gameInstance->GetFrameTime(); - this->moveDir -= this->rigidBody->GetState().GetOrientation().v[2].GetNormalized().xyz; + walkDirection -= forwardDir; + walkDirection.Normalize(); } if (key_strafeRight > 0.001) { key_strafeRight -= gameInstance->GetFrameTime(); - Oyster::Math::Float3 forward = this->rigidBody->GetState().GetOrientation().v[2]; - Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos; - this->moveDir -= (up).Cross(forward).GetNormalized(); + walkDirection -= rightDir; + walkDirection.Normalize(); } if (key_strafeLeft > 0.001) { key_strafeLeft -= gameInstance->GetFrameTime(); - Oyster::Math::Float3 forward = this->rigidBody->GetState().GetOrientation().v[2]; - Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos; - this->moveDir += (up).Cross(forward).GetNormalized(); + walkDirection += rightDir; + walkDirection.Normalize(); + maxSpeed = 40; } + - if(this->moveDir != Oyster::Math::Float3::null) + if (key_forward <= 0.001 && key_backward <= 0.001 && key_strafeRight <= 0.001 && key_strafeLeft <= 0.001 && key_jump <= 0.001 && this->rigidBody->GetLambda() < 0.7f) { - this->moveDir.Normalize(); - this->rigidBody->SetLinearVelocity(this->moveDir*this->moveSpeed + this->rigidBody->GetLinearVelocity()); + /* Dampen when on the ground and not being moved by the player */ + linearVelocity *= 0.2; + this->rigidBody->SetLinearVelocity (linearVelocity); + } + else + { + if (speed < maxSpeed && this->rigidBody->GetLambda() < 1.0f) + { + Oyster::Math::Float3 velocity = linearVelocity + walkDirection * walkSpeed; + this->rigidBody->SetLinearVelocity(velocity); + } + else if(speed < maxSpeed) + { + Oyster::Math::Float3 velocity = linearVelocity + (walkDirection * walkSpeed)*0.2f; + this->rigidBody->SetLinearVelocity(velocity); + } } - this->previousMoveSpeed = this->rigidBody->GetLinearVelocity(); - this->previousPosition = this->rigidBody->GetState().centerPos; - + if (key_jump > 0.001) + { + this->key_jump -= this->gameInstance->GetFrameTime(); + if(this->rigidBody->GetLambda() < 1.0f) + { + Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1].GetNormalized(); + this->rigidBody->ApplyImpulse(up*this->rigidBody->GetState().mass*20); + this->playerState = PLAYER_STATE::PLAYER_STATE_JUMPING; + } + } this->weapon->Update(0.01f); } @@ -226,12 +246,7 @@ void Player::Rotate(const Oyster::Math3D::Float3 lookDir, const Oyster::Math3D:: void Player::Jump() { - if(this->rigidBody->GetLamda() < 1.0f) - { - Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1].GetNormalized(); - this->rigidBody->ApplyImpulse(up *1500); - this->playerState = PLAYER_STATE::PLAYER_STATE_JUMPING; - } + this->key_jump = KEY_TIMER; } bool Player::IsWalking() diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index cd0c1962..f7cf6cce 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -69,7 +69,7 @@ void SimpleRigidBody::SetState( const SimpleRigidBody::State &state ) void SimpleRigidBody::ApplyImpulse(Float3 impulse) { - this->rigidBody->applyImpulse(btVector3(impulse.x, impulse.y, impulse.z), btVector3(0.0f, 0.0f, 0.0f)); + this->rigidBody->applyCentralImpulse(btVector3(impulse.x, impulse.y, impulse.z)); } void SimpleRigidBody::SetCollisionShape(btCollisionShape* shape) @@ -350,7 +350,7 @@ void SimpleRigidBody::SetCustomTag( void *ref ) void SimpleRigidBody::PreStep (const btCollisionWorld* collisionWorld) { btTransform xform; - this->rigidBody->getMotionState()->getWorldTransform (xform); + xform = this->rigidBody->getWorldTransform (); btVector3 down = -xform.getBasis()[1]; btVector3 forward = xform.getBasis()[2]; down.normalize (); @@ -359,7 +359,14 @@ void SimpleRigidBody::PreStep (const btCollisionWorld* collisionWorld) this->raySource[0] = xform.getOrigin(); this->raySource[1] = xform.getOrigin(); - this->rayTarget[0] = this->raySource[0] + down * this->state.reach.y * btScalar(1.1); + Float angle = acos(Float3(0, 1, 0).Dot(this->state.centerPos.GetNormalized())); + down.setZ(-down.z()); + down.setX(-down.x()); + btVector3 targetPlus = down * this->state.reach.y * btScalar(1.1); + + + + this->rayTarget[0] = this->raySource[0] + targetPlus; this->rayTarget[1] = this->raySource[1] + forward * this->state.reach.y * btScalar(1.1); class ClosestNotMe : public btCollisionWorld::ClosestRayResultCallback @@ -392,13 +399,15 @@ void SimpleRigidBody::PreStep (const btCollisionWorld* collisionWorld) if (rayCallback.hasHit()) { this->rayLambda[i] = rayCallback.m_closestHitFraction; - } else { + } + else + { this->rayLambda[i] = 1.0; } } } -float SimpleRigidBody::GetLamda() const +float SimpleRigidBody::GetLambda() const { return this->rayLambda[0]; } \ No newline at end of file diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.h b/Code/GamePhysics/Implementation/SimpleRigidBody.h index d67393d1..effd123d 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.h +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.h @@ -67,7 +67,7 @@ namespace Oyster void PreStep(const btCollisionWorld* collisionWorld); - float GetLamda() const; + float GetLambda() const; private: diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index e83f1d99..9bbde6e2 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -180,7 +180,7 @@ namespace Oyster ********************************************************/ virtual void SetCustomTag( void *ref ) = 0; - virtual float GetLamda() const = 0; + virtual float GetLambda() const = 0; }; } }