Player jumping

This commit is contained in:
MrLetsplay 2022-11-12 22:04:13 +01:00
parent ab0accb671
commit 1d61087e62
7 changed files with 17 additions and 12 deletions

View File

@ -5,8 +5,6 @@
#include "internal/ui.h" #include "internal/ui.h"
#include "internal/physics.h" #include "internal/physics.h"
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
namespace kek::Defaults { namespace kek::Defaults {
static KeyBinding static KeyBinding
@ -47,7 +45,7 @@ static void defaultInput(GLFWwindow *window, void *data) {
bool jump = false; bool jump = false;
if(Input::getKeyState(keyUp) == GLFW_PRESS) { if(Input::getKeyState(keyUp) == GLFW_PRESS) {
direction += glm::vec3(0,1,0); direction += glm::vec3(0,1,0);
jump = true; jump = kekData.player->onGround;
} }
if(Input::getKeyState(keyDown) == GLFW_PRESS) { if(Input::getKeyState(keyDown) == GLFW_PRESS) {
@ -63,7 +61,7 @@ static void defaultInput(GLFWwindow *window, void *data) {
btVector3 vel = kekData.player->physics->body->getLinearVelocity(); btVector3 vel = kekData.player->physics->body->getLinearVelocity();
glm::vec3 newVel = glm::normalize(direction) * 6.0f; glm::vec3 newVel = glm::normalize(direction) * 6.0f;
newVel.y = vel.y(); newVel.y = vel.y();
if(jump) newVel.y = 6.0f; if(jump) newVel.y = 2.7f;
kekData.player->physics->body->setLinearVelocity(Physics::fromGLM(newVel)); kekData.player->physics->body->setLinearVelocity(Physics::fromGLM(newVel));
} }
} }
@ -180,13 +178,14 @@ void init() {
btRigidBody *body = new btRigidBody(KEK_PLAYER_MASS, nullptr, shape); btRigidBody *body = new btRigidBody(KEK_PLAYER_MASS, nullptr, shape);
body->setFriction(3); body->setFriction(3);
body->setActivationState(DISABLE_DEACTIVATION); body->setActivationState(DISABLE_DEACTIVATION);
kekData.physics->world->addRigidBody(body, KEK_PLAYER_COLLISION_GROUP, KEK_OBJECT_COLLISION_GROUP); kekData.physics->world->addRigidBody(body);
btCollisionShape *jumpShape = new btBoxShape(btVector3(KEK_PLAYER_RADIUS, 0.1, KEK_PLAYER_RADIUS)); btCollisionShape *jumpShape = new btSphereShape(KEK_PLAYER_RADIUS / 2);
btCollisionObject *jumpCollider = new btGhostObject(); btGhostObject *jumpCollider = new btGhostObject();
jumpCollider->setCollisionShape(jumpShape); jumpCollider->setCollisionShape(jumpShape);
jumpCollider->setCollisionFlags(jumpCollider->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
jumpCollider->setActivationState(DISABLE_DEACTIVATION); jumpCollider->setActivationState(DISABLE_DEACTIVATION);
kekData.physics->world->addCollisionObject(jumpCollider, KEK_PLAYER_COLLISION_GROUP, KEK_OBJECT_COLLISION_GROUP); kekData.physics->world->addCollisionObject(jumpCollider);
kekData.player->physics = new PlayerPhysicsObjectData(); kekData.player->physics = new PlayerPhysicsObjectData();
kekData.player->physics->body = body; kekData.player->physics->body = body;

View File

@ -38,7 +38,7 @@ void GameObject::addPhysics(btCollisionShape *shape, float mass, int collisionFl
shape->calculateLocalInertia(mass, inertia); shape->calculateLocalInertia(mass, inertia);
btRigidBody *body = new btRigidBody(mass, new btDefaultMotionState(), shape, inertia); btRigidBody *body = new btRigidBody(mass, new btDefaultMotionState(), shape, inertia);
body->setFriction(1); body->setFriction(1);
kekData.physics->world->addRigidBody(body, KEK_OBJECT_COLLISION_GROUP, KEK_OBJECT_COLLISION_GROUP | KEK_PLAYER_COLLISION_GROUP); kekData.physics->world->addRigidBody(body);
body->setCollisionFlags(body->getCollisionFlags() | collisionFlags); body->setCollisionFlags(body->getCollisionFlags() | collisionFlags);
this->physics->body = body; this->physics->body = body;
} }

View File

@ -6,6 +6,7 @@ namespace kek {
Player::Player(): RotateableObject() { Player::Player(): RotateableObject() {
this->noclip = false; this->noclip = false;
this->onGround = false;
} }
void Player::rotateTo(glm::quat rotation) { void Player::rotateTo(glm::quat rotation) {

View File

@ -15,6 +15,7 @@ void init() {
btBroadphaseInterface *overlappingPairCache = new btDbvtBroadphase(); btBroadphaseInterface *overlappingPairCache = new btDbvtBroadphase();
btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver(); btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver();
kekData.physics->world = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConf); kekData.physics->world = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConf);
kekData.physics->world->getBroadphase()->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
kekData.physics->world->setGravity(btVector3(0, -10, 0)); kekData.physics->world->setGravity(btVector3(0, -10, 0));
} }
@ -37,8 +38,12 @@ void step(float deltaT) {
} }
} }
kekData.player->physics->jumpCollider->getWorldTransform().setOrigin(Physics::fromGLM(kekData.player->getFootPosition() + glm::vec3(0, KEK_PLAYER_RADIUS / 2, 0)));
kekData.physics->world->stepSimulation(deltaT, 100); kekData.physics->world->stepSimulation(deltaT, 100);
kekData.player->onGround = kekData.player->physics->jumpCollider->getNumOverlappingObjects() > 1; // TODO: improve
if(!kekData.player->noclip) { if(!kekData.player->noclip) {
kekData.activeCamera->moveTo(kekData.player->getEyePosition()); kekData.activeCamera->moveTo(kekData.player->getEyePosition());
} }

View File

@ -47,5 +47,3 @@
#define KEK_PLAYER_RADIUS 0.5f #define KEK_PLAYER_RADIUS 0.5f
#define KEK_PLAYER_EYE_OFFSET (KEK_PLAYER_HEIGHT / 2 - KEK_PLAYER_RADIUS) #define KEK_PLAYER_EYE_OFFSET (KEK_PLAYER_HEIGHT / 2 - KEK_PLAYER_RADIUS)
#define KEK_PLAYER_MASS 50 #define KEK_PLAYER_MASS 50
#define KEK_OBJECT_COLLISION_GROUP 1
#define KEK_PLAYER_COLLISION_GROUP 2

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <btBulletDynamicsCommon.h> #include <btBulletDynamicsCommon.h>
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
namespace kek { namespace kek {
@ -13,7 +14,7 @@ struct PhysicsObjectData {
}; };
struct PlayerPhysicsObjectData: public PhysicsObjectData { struct PlayerPhysicsObjectData: public PhysicsObjectData {
btCollisionObject *jumpCollider; btGhostObject *jumpCollider;
}; };
namespace Physics { namespace Physics {

View File

@ -9,6 +9,7 @@ class Player: public RotateableObject {
public: public:
PlayerPhysicsObjectData *physics; PlayerPhysicsObjectData *physics;
bool noclip; bool noclip;
bool onGround;
Player(); Player();