From 3f6aba44b20595dae3e601282c906931a801ec92 Mon Sep 17 00:00:00 2001 From: Jukitsu Date: Thu, 4 Jul 2024 02:27:08 +0200 Subject: [PATCH] tweaks to movement physics --- Jukcraft/src/core/Camera.cpp | 11 +++-------- Jukcraft/src/core/Game.cpp | 3 ++- Jukcraft/src/entity/BipedEntity.cpp | 8 ++++---- Jukcraft/src/entity/BipedEntity.h | 5 +++-- Jukcraft/src/entity/Entity.h | 9 +++------ Jukcraft/src/entity/LivingEntity.cpp | 26 +++++++++++++++----------- Jukcraft/src/entity/LivingEntity.h | 6 +++++- Jukcraft/src/entity/player/Player.cpp | 4 ++-- Jukcraft/src/entity/player/Player.h | 5 +++-- 9 files changed, 40 insertions(+), 37 deletions(-) diff --git a/Jukcraft/src/core/Camera.cpp b/Jukcraft/src/core/Camera.cpp index 892ce03..3acad65 100644 --- a/Jukcraft/src/core/Camera.cpp +++ b/Jukcraft/src/core/Camera.cpp @@ -4,8 +4,6 @@ #include #include "core/App.h" -#include "physics/constants.h" - static constexpr float sensitivity = 0.005f; namespace Jukcraft { @@ -44,14 +42,11 @@ namespace Jukcraft { if (App::GetWindow().isKeyPressed(GLFW_KEY_SPACE)) input.y += 1; - speed = WALK_SPEED; - - if (input.y > 0) { player->jump(); - } if (input.x || input.z) { - player->setRelativeAccel(glm::vec3((float)input.x, 0.0f, (float)input.z) * speed); - } + } + + player->setInput(glm::ivec3(input.x, 0, input.z)); glm::mat4 proj = glm::perspective( diff --git a/Jukcraft/src/core/Game.cpp b/Jukcraft/src/core/Game.cpp index f18e85f..d613d26 100644 --- a/Jukcraft/src/core/Game.cpp +++ b/Jukcraft/src/core/Game.cpp @@ -26,7 +26,8 @@ namespace Jukcraft { blocks.emplace_back("Leaves", 6, models.cube, std::vector{6, 6, 6, 6, 6, 6}, true, 14); world = std::make_unique(blocks, shader); - player = std::make_unique(*world, glm::vec3(5.0f, 70.0f, 10.0f), glm::vec3(0.0f), glm::vec3(0.0f), glm::pi() / 2.0f, 0.0f); + player = std::make_unique(*world, glm::vec3(5.0f, 70.0f, 10.0f), glm::vec3(0.0f), + glm::pi() / 2.0f, 0.0f); } void Game::onMousePress(int button) { diff --git a/Jukcraft/src/entity/BipedEntity.cpp b/Jukcraft/src/entity/BipedEntity.cpp index e995715..4f590f2 100644 --- a/Jukcraft/src/entity/BipedEntity.cpp +++ b/Jukcraft/src/entity/BipedEntity.cpp @@ -3,10 +3,10 @@ #include "world/World.h" namespace Jukcraft { - BipedEntity::BipedEntity(World& world, const glm::vec3& initialPos, const glm::vec3& initialVelocity, - const glm::vec3& initialAcceleration, float initialYaw, float initialPitch) - :LivingEntity(world, initialPos, initialVelocity, - initialAcceleration, initialYaw, initialPitch, 0.6f, 1.8f) { + BipedEntity::BipedEntity(World& world, const glm::vec3& initialPos, const glm::vec3& initialVelocity + , float initialYaw, float initialPitch) + :LivingEntity(world, initialPos, initialVelocity, + initialYaw, initialPitch, 0.6f, 1.8f) { } BipedEntity::~BipedEntity() { diff --git a/Jukcraft/src/entity/BipedEntity.h b/Jukcraft/src/entity/BipedEntity.h index 17efcc4..162c392 100644 --- a/Jukcraft/src/entity/BipedEntity.h +++ b/Jukcraft/src/entity/BipedEntity.h @@ -5,8 +5,9 @@ namespace Jukcraft { class BipedEntity : public LivingEntity { public: - BipedEntity(World& world, const glm::vec3& initialPos = glm::vec3(0.0f), const glm::vec3& initialVelocity = glm::vec3(0.0f), - const glm::vec3& initialAcceleration = glm::vec3(0.0f), float initialYaw = 0.0f, float initialPitch = 0.0f); + BipedEntity(World& world, const glm::vec3& initialPos = glm::vec3(0.0f), + const glm::vec3& initialVelocity = glm::vec3(0.0f), + float initialYaw = 0.0f, float initialPitch = 0.0f); virtual ~BipedEntity(); }; } \ No newline at end of file diff --git a/Jukcraft/src/entity/Entity.h b/Jukcraft/src/entity/Entity.h index aa57d39..fb8473e 100644 --- a/Jukcraft/src/entity/Entity.h +++ b/Jukcraft/src/entity/Entity.h @@ -6,9 +6,9 @@ namespace Jukcraft { Entity() :mass(1.0f) { } - Entity(const glm::vec3& initialPos, const glm::vec3& initialVelocity, - const glm::vec3& initialAcceleration, float initialYaw, float initialPitch, float mass) - :position(initialPos), velocity(initialVelocity), relativeAccel(initialAcceleration), + Entity(const glm::vec3& initialPos, const glm::vec3& initialVelocity, + float initialYaw, float initialPitch, float mass) + :position(initialPos), velocity(initialVelocity), yaw(initialYaw), pitch(initialPitch), mass(mass) { } @@ -23,20 +23,17 @@ namespace Jukcraft { constexpr const glm::vec3& getPos() const { return position; } constexpr const glm::vec3& getVelocity() const { return velocity; } - constexpr const glm::vec3& getRelativeAccel() const { return relativeAccel; } constexpr float getYaw() const { return yaw; } constexpr float getPitch() const { return pitch; } constexpr float getKineticEnergy() const { return 0.5f * mass * velocity.length() * velocity.length(); } void setPos(const glm::vec3& pos) { position = pos; } void setVelocity(const glm::vec3& v) { velocity = v; } - void setRelativeAccel(const glm::vec3& a) { relativeAccel = a; } void setYaw(float theta) { yaw = theta; } void setPitch(float phi) { pitch = phi; } protected: glm::vec3 position; glm::vec3 velocity; - glm::vec3 relativeAccel; float yaw, pitch; const float mass; }; diff --git a/Jukcraft/src/entity/LivingEntity.cpp b/Jukcraft/src/entity/LivingEntity.cpp index 3090952..17e5e40 100644 --- a/Jukcraft/src/entity/LivingEntity.cpp +++ b/Jukcraft/src/entity/LivingEntity.cpp @@ -2,13 +2,15 @@ #include "entity/LivingEntity.h" #include "world/World.h" +#include "physics/constants.h" + namespace Jukcraft { LivingEntity::LivingEntity(World& world, const glm::vec3& initialPos, const glm::vec3& initialVelocity, - const glm::vec3& initialAcceleration, float initialYaw, float initialPitch, float width, float height) - :Entity(initialPos, initialVelocity, initialAcceleration, initialYaw, initialPitch, width * width * height), + float initialYaw, float initialPitch, float width, float height) + :Entity(initialPos, initialVelocity, initialYaw, initialPitch, width * width * height), oldPosition(position), interpolatedPos(position), interpolationStep(1.0f), - width(0.6f), height(1.8f), collider(), world(world), onGround(false) { + width(0.6f), height(1.8f), collider(), world(world), onGround(false), speed(WALK_SPEED) { collider.vx1 = position - glm::vec3(width / 2.0f, 0, width / 2.0f); collider.vx2 = position + glm::vec3(width / 2.0f, height, width / 2.0f); } @@ -108,16 +110,18 @@ namespace Jukcraft { } void LivingEntity::applyPhysics() { - float accelMagnitude = glm::length(glm::vec2(relativeAccel.x, relativeAccel.z)); - float headingAngle = yaw - glm::atan(relativeAccel.z, relativeAccel.x) + glm::pi() / 2; + if (input.x || input.z) { + float headingAngle = yaw - glm::atan((float)input.z, (float)input.x) + glm::pi() / 2; - velocity += glm::vec3( - glm::cos(headingAngle) * accelMagnitude, - relativeAccel.y, - glm::sin(headingAngle) * accelMagnitude - ) * getFriction(); + velocity += glm::vec3( + glm::cos(headingAngle) * speed, + input.y, + glm::sin(headingAngle) * speed + ) * getFriction(); - setRelativeAccel(glm::vec3(0.0f)); + setInput(glm::ivec3(0)); + } + updateCollider(); diff --git a/Jukcraft/src/entity/LivingEntity.h b/Jukcraft/src/entity/LivingEntity.h index 62b8fd8..f58cc19 100644 --- a/Jukcraft/src/entity/LivingEntity.h +++ b/Jukcraft/src/entity/LivingEntity.h @@ -12,7 +12,6 @@ namespace Jukcraft { public: LivingEntity(World& world, const glm::vec3& initialPos = glm::vec3(0.0f), const glm::vec3& initialVelocity = glm::vec3(0.0f), - const glm::vec3& initialAcceleration = glm::vec3(0.0f), float initialYaw = 0.0f, float initialPitch = 0.0f, float width = 1.0f, float height = 1.0f); virtual ~LivingEntity(); @@ -25,10 +24,13 @@ namespace Jukcraft { void move(const glm::vec3& motion) override; void push(const glm::vec3& motion) override; + void setInput(const glm::ivec3& inputAccel) { input = inputAccel; } constexpr float getEyeLevel() const { return height - 0.2f; } constexpr const Collider& getCollider() const { return collider; } + constexpr const glm::ivec3& getInput() const { return input; } protected: + glm::ivec3 input; constexpr const glm::vec3& getFriction() const { if (onGround) @@ -41,6 +43,8 @@ namespace Jukcraft { float width, height; + const float speed; + bool onGround; glm::vec3 oldPosition; diff --git a/Jukcraft/src/entity/player/Player.cpp b/Jukcraft/src/entity/player/Player.cpp index e5ac141..f600bca 100644 --- a/Jukcraft/src/entity/player/Player.cpp +++ b/Jukcraft/src/entity/player/Player.cpp @@ -4,8 +4,8 @@ namespace Jukcraft { Player::Player(World& world, const glm::vec3& initialPos, const glm::vec3& initialVelocity, - const glm::vec3& initialAcceleration, float initialYaw, float initialPitch) - :BipedEntity(world, initialPos, initialVelocity, initialAcceleration, initialYaw, initialPitch) { + float initialYaw, float initialPitch) + :BipedEntity(world, initialPos, initialVelocity, initialYaw, initialPitch) { } diff --git a/Jukcraft/src/entity/player/Player.h b/Jukcraft/src/entity/player/Player.h index 695fad1..7eec881 100644 --- a/Jukcraft/src/entity/player/Player.h +++ b/Jukcraft/src/entity/player/Player.h @@ -4,8 +4,9 @@ namespace Jukcraft { class Player : public BipedEntity { public: - Player(World& world, const glm::vec3& initialPos = glm::vec3(0.0f), const glm::vec3& initialVelocity = glm::vec3(0.0f), - const glm::vec3& initialAcceleration = glm::vec3(0.0f), float initialYaw = 0.0f, float initialPitch = 0.0f); + Player(World& world, const glm::vec3& initialPos = glm::vec3(0.0f), + const glm::vec3& initialVelocity = glm::vec3(0.0f), + float initialYaw = 0.0f, float initialPitch = 0.0f); virtual ~Player(); private: friend class Camera;