Improve physics

This commit is contained in:
MrLetsplay 2022-11-11 17:30:27 +01:00
parent 4d78ca9d94
commit 6e45e0bc34
6 changed files with 25 additions and 9 deletions

View File

@ -42,8 +42,10 @@ static void defaultInput(GLFWwindow *window, void *data) {
direction += glm::normalize(glm::cross(kekData.activeCamera->direction, glm::vec3(0.0f, 1.0f, 0.0f))); direction += glm::normalize(glm::cross(kekData.activeCamera->direction, glm::vec3(0.0f, 1.0f, 0.0f)));
} }
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;
} }
if(Input::getKeyState(keyDown) == GLFW_PRESS) { if(Input::getKeyState(keyDown) == GLFW_PRESS) {
@ -55,7 +57,12 @@ static void defaultInput(GLFWwindow *window, void *data) {
if(kekData.player->noclip) { if(kekData.player->noclip) {
kekData.activeCamera->translate(direction * KEK_NOCLIP_SPEED * kekData.lastFrameTime); kekData.activeCamera->translate(direction * KEK_NOCLIP_SPEED * kekData.lastFrameTime);
}else { }else {
kekData.player->physics->body->applyCentralImpulse(Physics::fromGLM(glm::normalize(direction) * (kekData.lastFrameTime * 20))); //kekData.player->physics->body->applyCentralImpulse(Physics::fromGLM(glm::normalize(direction) * (kekData.lastFrameTime * 20)));
btVector3 vel = kekData.player->physics->body->getLinearVelocity();
glm::vec3 newVel = glm::normalize(direction) * 6.0f;
newVel.y = vel.y();
if(jump) newVel.y = 6.0f;
kekData.player->physics->body->setLinearVelocity(Physics::fromGLM(newVel));
} }
} }
} }
@ -163,11 +170,17 @@ void init() {
//UI::addElement(options); //UI::addElement(options);
kekData.player = new Player(); kekData.player = new Player();
btCollisionShape *shape = new btBoxShape(btVector3(1,1,1)); btVector3 positions[2];
kekData.player->physics = new PhysicsObjectData(); positions[0] = btVector3(0, -KEK_PLAYER_HEIGHT / 2 + KEK_PLAYER_RADIUS, 0);
btRigidBody *body = new btRigidBody(1, nullptr, shape); 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); body->setActivationState(DISABLE_DEACTIVATION);
kekData.physics->world->addRigidBody(body); kekData.physics->world->addRigidBody(body);
kekData.player->physics = new PhysicsObjectData();
kekData.player->physics->body = body; kekData.player->physics->body = body;
kekData.player->moveTo(glm::vec3(0,10,0)); kekData.player->moveTo(glm::vec3(0,10,0));
} }

View File

@ -34,7 +34,10 @@ void GameObject::draw(Shader *shader) {
void GameObject::addPhysics(btCollisionShape *shape, float mass, int collisionFlags) { void GameObject::addPhysics(btCollisionShape *shape, float mass, int collisionFlags) {
this->physics = new PhysicsObjectData(); this->physics = new PhysicsObjectData();
btRigidBody *body = new btRigidBody(mass, new btDefaultMotionState(), shape); btVector3 inertia = btVector3(0,0,0);
shape->calculateLocalInertia(mass, inertia);
btRigidBody *body = new btRigidBody(mass, new btDefaultMotionState(), shape, inertia);
body->setFriction(1);
kekData.physics->world->addRigidBody(body); kekData.physics->world->addRigidBody(body);
body->setCollisionFlags(collisionFlags); body->setCollisionFlags(collisionFlags);
this->physics->body = body; this->physics->body = body;

View File

@ -34,7 +34,6 @@ void step(float deltaT) {
for(GameObject *obj : kekData.activeScene->objects) { for(GameObject *obj : kekData.activeScene->objects) {
if(obj->physics) { if(obj->physics) {
obj->physics->body->getWorldTransform().setOrigin(fromGLM(obj->getPosition())); obj->physics->body->getWorldTransform().setOrigin(fromGLM(obj->getPosition()));
} }
} }

View File

@ -46,3 +46,4 @@
#define KEK_PLAYER_HEIGHT 2 #define KEK_PLAYER_HEIGHT 2
#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

View File

@ -25,7 +25,7 @@ public:
void draw(Shader *shader); void draw(Shader *shader);
void addPhysics(btCollisionShape *shape, float mass = 1, int collisionFlags = 0); void addPhysics(btCollisionShape *shape, float mass, int collisionFlags = 0);
virtual void rotate(float angle, glm::vec3 axis) { rotateTo(glm::rotate(getRotation(), angle, axis)); }; virtual void rotate(float angle, glm::vec3 axis) { rotateTo(glm::rotate(getRotation(), angle, axis)); };

View File

@ -60,7 +60,7 @@ int main(int argc, char **argv) {
Mesh *mesh = ObjParser::loadMesh("object/sphere/Sphere.obj"); Mesh *mesh = ObjParser::loadMesh("object/sphere/Sphere.obj");
GameObject *test = new GameObject(); GameObject *test = new GameObject();
btCollisionShape *shape = new btSphereShape(1); btCollisionShape *shape = new btSphereShape(1);
test->addPhysics(shape); test->addPhysics(shape, 10);
test->addMesh(mesh); test->addMesh(mesh);
test->moveTo(glm::vec3(0,5,0)); test->moveTo(glm::vec3(0,5,0));
scene->addObject(test); scene->addObject(test);
@ -77,7 +77,7 @@ int main(int argc, char **argv) {
for(int i = 0; i < 10; i++) { for(int i = 0; i < 10; i++) {
GameObject *test2 = new GameObject(); GameObject *test2 = new GameObject();
btCollisionShape *shape = new btBoxShape(btVector3(1,1,1)); btCollisionShape *shape = new btBoxShape(btVector3(1,1,1));
test2->addPhysics(shape); test2->addPhysics(shape, 10);
test2->addMesh(ObjParser::loadMesh("object/cube_colored/Cube.obj")); test2->addMesh(ObjParser::loadMesh("object/cube_colored/Cube.obj"));
test2->moveTo(glm::vec3(1.0f, 5.0f, 3 * i)); test2->moveTo(glm::vec3(1.0f, 5.0f, 3 * i));
scene->addObject(test2); scene->addObject(test2);