From 4a8cbaf57f290b7f6e7a380cb11b694d65284102 Mon Sep 17 00:00:00 2001 From: Anselme Date: Sun, 30 Jul 2017 13:54:59 +0200 Subject: [PATCH] fixed ground detection for jumping --- src/scene/playercharacternode.cpp | 38 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/scene/playercharacternode.cpp b/src/scene/playercharacternode.cpp index 14d8c9c..0567efe 100644 --- a/src/scene/playercharacternode.cpp +++ b/src/scene/playercharacternode.cpp @@ -59,7 +59,7 @@ const float PLAYER_RADIUS = 0.30f; const float PLAYER_HEIGHT = 1.75f; const float EYES_OFFSET = 0.775f; const float JUMP_VELOCITY = 5.f; -const float EPSILON = 1.f; +const float EPSILON = 1.5f; PlayerCharacterNode::PlayerCharacterNode(bool noClip) : m_noclipMode(noClip), @@ -172,25 +172,25 @@ void PlayerCharacterNode::update() } else { - float controlRatio = 0.2f; // 1 = total control, 0 = no control, can be seen as a slipperiness factor - btVector3 newVelocity = velocity*(1.f-controlRatio) + targetVelocity*controlRatio; - if(jump) - {/* - btVector3 start(pos); - start.setY(start.y() - PLAYER_HEIGHT/2.f); - btVector3 end(pos); - end.setY(end.y() - EPSILON); - btCollisionWorld::ClosestRayResultCallback RayCallback(start, end); - getEngine().getPhysics()->rayTest(start, end, RayCallback); - if(RayCallback.hasHit()) // if ground is nearby - { - //btVector3 normal = RayCallback.m_hitNormalWorld; - - }*/ - - // raycasting not working yet - newVelocity.setY(JUMP_VELOCITY); + bool onGround = false; + btVector3 start(pos); + start.setY(start.y() - PLAYER_HEIGHT/2.f); + btVector3 end(pos); + end.setY(end.y() - EPSILON); + btCollisionWorld::ClosestRayResultCallback RayCallback(start, end); + getEngine().getPhysics()->rayTest(start, end, RayCallback); + float controlRatio = 0.f; // 1 = total control, 0 = no control, can be seen as a slipperiness factor + if(RayCallback.hasHit()) // if ground is nearby + { + onGround = true; + btVector3 normal = RayCallback.m_hitNormalWorld; + float slope = normal.dot(btVector3(0, 1, 0)); + controlRatio = slope > 0.4f ? 0.2f * slope : 0.f; } + + btVector3 newVelocity = velocity*(1.f-controlRatio) + targetVelocity*controlRatio; + if(jump && onGround) + newVelocity.setY(JUMP_VELOCITY); m_rigidBody->setLinearVelocity(newVelocity); } m_playerLightNode->update();