Jumpy jump

This commit is contained in:
Robin Engman 2014-02-17 06:44:29 +01:00
parent f8e68b95b2
commit a90f33d20e
4 changed files with 75 additions and 51 deletions

View File

@ -65,7 +65,7 @@ Player::Player(Oyster::Physics::ICustomBody *rigidBody, Oyster::Physics::ICustom
this->previousPosition = Oyster::Math::Float3(0,0,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 = 20;
this->previousMoveSpeed = Oyster::Math::Float3(0,0,0); this->previousMoveSpeed = Oyster::Math::Float3(0,0,0);
} }
@ -84,66 +84,86 @@ void Player::BeginFrame()
//weapon->Update(0.002f); //weapon->Update(0.002f);
Object::BeginFrame(); Object::BeginFrame();
//Oyster::Math::Float3 previousFall = this->previousMoveSpeed*-this->rigidBody->GetState().centerPos.GetNormalized(); Oyster::Math::Float maxSpeed = 30;
//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::Float4x4 xform;
{ xform = this->rigidBody->GetState().GetOrientation();
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
{
if(this->rigidBody->GetLamda() == 1.0f) /* Handle turning */
{ /*if (left)
this->playerState = PLAYER_STATE_WALKING; m_turnAngle -= dt * m_turnVelocity;
} if (right)
m_turnAngle += dt * m_turnVelocity;*/
if(this->moveDir != Oyster::Math::Float3::null) //xform.setRotation (btQuaternion (btVector3(0.0, 1.0, 0.0), m_turnAngle));
{
Oyster::Math::Float3 velocity = this->rigidBody->GetLinearVelocity();
this->rigidBody->SetLinearVelocity(velocity - this->moveDir*this->moveSpeed );
}
}
this->moveDir = Oyster::Math::Float3::null; 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) if (key_forward > 0.001)
{ {
key_forward -= gameInstance->GetFrameTime(); // fixed timer key_forward -= gameInstance->GetFrameTime();
this->moveDir += this->rigidBody->GetState().GetOrientation().v[2].GetNormalized().xyz; walkDirection += forwardDir;
walkDirection.Normalize();
} }
if (key_backward > 0.001) if (key_backward > 0.001)
{ {
key_backward -= gameInstance->GetFrameTime(); key_backward -= gameInstance->GetFrameTime();
this->moveDir -= this->rigidBody->GetState().GetOrientation().v[2].GetNormalized().xyz; walkDirection -= forwardDir;
walkDirection.Normalize();
} }
if (key_strafeRight > 0.001) if (key_strafeRight > 0.001)
{ {
key_strafeRight -= gameInstance->GetFrameTime(); key_strafeRight -= gameInstance->GetFrameTime();
Oyster::Math::Float3 forward = this->rigidBody->GetState().GetOrientation().v[2]; walkDirection -= rightDir;
Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos; walkDirection.Normalize();
this->moveDir -= (up).Cross(forward).GetNormalized();
} }
if (key_strafeLeft > 0.001) if (key_strafeLeft > 0.001)
{ {
key_strafeLeft -= gameInstance->GetFrameTime(); key_strafeLeft -= gameInstance->GetFrameTime();
Oyster::Math::Float3 forward = this->rigidBody->GetState().GetOrientation().v[2]; walkDirection += rightDir;
Oyster::Math::Float3 up = this->rigidBody->GetState().centerPos; walkDirection.Normalize();
this->moveDir += (up).Cross(forward).GetNormalized(); 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(); /* Dampen when on the ground and not being moved by the player */
this->rigidBody->SetLinearVelocity(this->moveDir*this->moveSpeed + this->rigidBody->GetLinearVelocity()); 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(); if (key_jump > 0.001)
this->previousPosition = this->rigidBody->GetState().centerPos; {
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); this->weapon->Update(0.01f);
} }
@ -226,12 +246,7 @@ void Player::Rotate(const Oyster::Math3D::Float3 lookDir, const Oyster::Math3D::
void Player::Jump() void Player::Jump()
{ {
if(this->rigidBody->GetLamda() < 1.0f) this->key_jump = KEY_TIMER;
{
Oyster::Math::Float3 up = this->rigidBody->GetState().GetOrientation().v[1].GetNormalized();
this->rigidBody->ApplyImpulse(up *1500);
this->playerState = PLAYER_STATE::PLAYER_STATE_JUMPING;
}
} }
bool Player::IsWalking() bool Player::IsWalking()

View File

@ -69,7 +69,7 @@ void SimpleRigidBody::SetState( const SimpleRigidBody::State &state )
void SimpleRigidBody::ApplyImpulse(Float3 impulse) 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) void SimpleRigidBody::SetCollisionShape(btCollisionShape* shape)
@ -350,7 +350,7 @@ void SimpleRigidBody::SetCustomTag( void *ref )
void SimpleRigidBody::PreStep (const btCollisionWorld* collisionWorld) void SimpleRigidBody::PreStep (const btCollisionWorld* collisionWorld)
{ {
btTransform xform; btTransform xform;
this->rigidBody->getMotionState()->getWorldTransform (xform); xform = this->rigidBody->getWorldTransform ();
btVector3 down = -xform.getBasis()[1]; btVector3 down = -xform.getBasis()[1];
btVector3 forward = xform.getBasis()[2]; btVector3 forward = xform.getBasis()[2];
down.normalize (); down.normalize ();
@ -359,7 +359,14 @@ void SimpleRigidBody::PreStep (const btCollisionWorld* collisionWorld)
this->raySource[0] = xform.getOrigin(); this->raySource[0] = xform.getOrigin();
this->raySource[1] = 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); this->rayTarget[1] = this->raySource[1] + forward * this->state.reach.y * btScalar(1.1);
class ClosestNotMe : public btCollisionWorld::ClosestRayResultCallback class ClosestNotMe : public btCollisionWorld::ClosestRayResultCallback
@ -392,13 +399,15 @@ void SimpleRigidBody::PreStep (const btCollisionWorld* collisionWorld)
if (rayCallback.hasHit()) if (rayCallback.hasHit())
{ {
this->rayLambda[i] = rayCallback.m_closestHitFraction; this->rayLambda[i] = rayCallback.m_closestHitFraction;
} else { }
else
{
this->rayLambda[i] = 1.0; this->rayLambda[i] = 1.0;
} }
} }
} }
float SimpleRigidBody::GetLamda() const float SimpleRigidBody::GetLambda() const
{ {
return this->rayLambda[0]; return this->rayLambda[0];
} }

View File

@ -67,7 +67,7 @@ namespace Oyster
void PreStep(const btCollisionWorld* collisionWorld); void PreStep(const btCollisionWorld* collisionWorld);
float GetLamda() const; float GetLambda() const;
private: private:

View File

@ -180,7 +180,7 @@ namespace Oyster
********************************************************/ ********************************************************/
virtual void SetCustomTag( void *ref ) = 0; virtual void SetCustomTag( void *ref ) = 0;
virtual float GetLamda() const = 0; virtual float GetLambda() const = 0;
}; };
} }
} }