diff --git a/src/kekengine/cpp/common/defaults.cpp b/src/kekengine/cpp/common/defaults.cpp index 2e2bef8..a9e2e49 100644 --- a/src/kekengine/cpp/common/defaults.cpp +++ b/src/kekengine/cpp/common/defaults.cpp @@ -5,6 +5,8 @@ #include "internal/ui.h" #include "internal/physics.h" +#include + 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)); } diff --git a/src/kekengine/cpp/object/gameobject.cpp b/src/kekengine/cpp/object/gameobject.cpp index 5641147..52e0a34 100644 --- a/src/kekengine/cpp/object/gameobject.cpp +++ b/src/kekengine/cpp/object/gameobject.cpp @@ -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; } diff --git a/src/kekengine/include/constants.h b/src/kekengine/include/constants.h index e2d36fb..33d8100 100644 --- a/src/kekengine/include/constants.h +++ b/src/kekengine/include/constants.h @@ -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 diff --git a/src/kekengine/include/internal/physics.h b/src/kekengine/include/internal/physics.h index eb7a4c2..6a211bf 100644 --- a/src/kekengine/include/internal/physics.h +++ b/src/kekengine/include/internal/physics.h @@ -12,6 +12,10 @@ struct PhysicsObjectData { btRigidBody *body; }; +struct PlayerPhysicsObjectData: public PhysicsObjectData { + btCollisionObject *jumpCollider; +}; + namespace Physics { glm::vec3 toGLM(btVector3 vec); diff --git a/src/kekengine/include/physics.h b/src/kekengine/include/physics.h index 0479b33..323649d 100644 --- a/src/kekengine/include/physics.h +++ b/src/kekengine/include/physics.h @@ -3,6 +3,7 @@ namespace kek { struct PhysicsObjectData; +struct PlayerPhysicsObjectData; namespace Physics { diff --git a/src/kekengine/include/player.h b/src/kekengine/include/player.h index aaed436..5e997d3 100644 --- a/src/kekengine/include/player.h +++ b/src/kekengine/include/player.h @@ -7,7 +7,7 @@ namespace kek { class Player: public RotateableObject { public: - PhysicsObjectData *physics; + PlayerPhysicsObjectData *physics; bool noclip; Player(); diff --git a/src/kekgame/cpp/kekgame.cpp b/src/kekgame/cpp/kekgame.cpp index 9e6a258..2952c4c 100644 --- a/src/kekgame/cpp/kekgame.cpp +++ b/src/kekgame/cpp/kekgame.cpp @@ -6,6 +6,11 @@ #include #include +#include +#include + +#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));