fixed ground detection for jumping

This commit is contained in:
Anselme 2017-07-30 13:54:59 +02:00
parent 71fd9e8b83
commit 4a8cbaf57f

View File

@ -59,7 +59,7 @@ const float PLAYER_RADIUS = 0.30f;
const float PLAYER_HEIGHT = 1.75f; const float PLAYER_HEIGHT = 1.75f;
const float EYES_OFFSET = 0.775f; const float EYES_OFFSET = 0.775f;
const float JUMP_VELOCITY = 5.f; const float JUMP_VELOCITY = 5.f;
const float EPSILON = 1.f; const float EPSILON = 1.5f;
PlayerCharacterNode::PlayerCharacterNode(bool noClip) : PlayerCharacterNode::PlayerCharacterNode(bool noClip) :
m_noclipMode(noClip), m_noclipMode(noClip),
@ -172,25 +172,25 @@ void PlayerCharacterNode::update()
} }
else else
{ {
float controlRatio = 0.2f; // 1 = total control, 0 = no control, can be seen as a slipperiness factor bool onGround = false;
btVector3 newVelocity = velocity*(1.f-controlRatio) + targetVelocity*controlRatio; btVector3 start(pos);
if(jump) start.setY(start.y() - PLAYER_HEIGHT/2.f);
{/* btVector3 end(pos);
btVector3 start(pos); end.setY(end.y() - EPSILON);
start.setY(start.y() - PLAYER_HEIGHT/2.f); btCollisionWorld::ClosestRayResultCallback RayCallback(start, end);
btVector3 end(pos); getEngine().getPhysics()->rayTest(start, end, RayCallback);
end.setY(end.y() - EPSILON); float controlRatio = 0.f; // 1 = total control, 0 = no control, can be seen as a slipperiness factor
btCollisionWorld::ClosestRayResultCallback RayCallback(start, end); if(RayCallback.hasHit()) // if ground is nearby
getEngine().getPhysics()->rayTest(start, end, RayCallback); {
if(RayCallback.hasHit()) // if ground is nearby onGround = true;
{ btVector3 normal = RayCallback.m_hitNormalWorld;
//btVector3 normal = RayCallback.m_hitNormalWorld; float slope = normal.dot(btVector3(0, 1, 0));
controlRatio = slope > 0.4f ? 0.2f * slope : 0.f;
}*/
// raycasting not working yet
newVelocity.setY(JUMP_VELOCITY);
} }
btVector3 newVelocity = velocity*(1.f-controlRatio) + targetVelocity*controlRatio;
if(jump && onGround)
newVelocity.setY(JUMP_VELOCITY);
m_rigidBody->setLinearVelocity(newVelocity); m_rigidBody->setLinearVelocity(newVelocity);
} }
m_playerLightNode->update(); m_playerLightNode->update();