Player jump collider (WIP)

This commit is contained in:
MrLetsplay 2022-11-12 20:35:30 +01:00
parent 6e45e0bc34
commit ab0accb671
7 changed files with 27 additions and 7 deletions

View File

@ -5,6 +5,8 @@
#include "internal/ui.h"
#include "internal/physics.h"
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
namespace kek::Defaults {
static KeyBinding
@ -172,16 +174,23 @@ void init() {
kekData.player = new Player();
btVector3 positions[2];
positions[0] = btVector3(0, -KEK_PLAYER_HEIGHT / 2 + KEK_PLAYER_RADIUS, 0);
positions[1] = btVector3(0, -KEK_PLAYER_HEIGHT / 2 + KEK_PLAYER_RADIUS, 0);
positions[1] = btVector3(0, KEK_PLAYER_HEIGHT / 2 + KEK_PLAYER_RADIUS, 0);
float radii[2] = {KEK_PLAYER_RADIUS, KEK_PLAYER_RADIUS};
btCollisionShape *shape = new btMultiSphereShape(positions, radii, 2);
btRigidBody *body = new btRigidBody(KEK_PLAYER_MASS, nullptr, shape);
body->setFriction(3);
body->setActivationState(DISABLE_DEACTIVATION);
kekData.physics->world->addRigidBody(body);
kekData.physics->world->addRigidBody(body, KEK_PLAYER_COLLISION_GROUP, KEK_OBJECT_COLLISION_GROUP);
kekData.player->physics = new PhysicsObjectData();
btCollisionShape *jumpShape = new btBoxShape(btVector3(KEK_PLAYER_RADIUS, 0.1, KEK_PLAYER_RADIUS));
btCollisionObject *jumpCollider = new btGhostObject();
jumpCollider->setCollisionShape(jumpShape);
jumpCollider->setActivationState(DISABLE_DEACTIVATION);
kekData.physics->world->addCollisionObject(jumpCollider, KEK_PLAYER_COLLISION_GROUP, KEK_OBJECT_COLLISION_GROUP);
kekData.player->physics = new PlayerPhysicsObjectData();
kekData.player->physics->body = body;
kekData.player->physics->jumpCollider = jumpCollider;
kekData.player->moveTo(glm::vec3(0,10,0));
}

View File

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

View File

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

View File

@ -12,6 +12,10 @@ struct PhysicsObjectData {
btRigidBody *body;
};
struct PlayerPhysicsObjectData: public PhysicsObjectData {
btCollisionObject *jumpCollider;
};
namespace Physics {
glm::vec3 toGLM(btVector3 vec);

View File

@ -3,6 +3,7 @@
namespace kek {
struct PhysicsObjectData;
struct PlayerPhysicsObjectData;
namespace Physics {

View File

@ -7,7 +7,7 @@ namespace kek {
class Player: public RotateableObject {
public:
PhysicsObjectData *physics;
PlayerPhysicsObjectData *physics;
bool noclip;
Player();

View File

@ -6,6 +6,11 @@
#include <BulletCollision/CollisionShapes/btBoxShape.h>
#include <BulletCollision/btBulletCollisionCommon.h>
#include <BulletCollision/btBulletCollisionCommon.h>
#include <btBulletDynamicsCommon.h>
#include "internal.h"
#include "internal/physics.h"
using namespace kek;
@ -73,7 +78,6 @@ int main(int argc, char **argv) {
test3->moveTo(glm::vec3(2, 1, 2));
scene->addObject(test3);
for(int i = 0; i < 10; i++) {
GameObject *test2 = new GameObject();
btCollisionShape *shape = new btBoxShape(btVector3(1,1,1));