Skip to content

Commit

Permalink
tweaks to movement physics
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukitsu committed Jul 4, 2024
1 parent 20abec9 commit 3f6aba4
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 37 deletions.
11 changes: 3 additions & 8 deletions Jukcraft/src/core/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <GLFW/glfw3.h>
#include "core/App.h"

#include "physics/constants.h"

static constexpr float sensitivity = 0.005f;

namespace Jukcraft {
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion Jukcraft/src/core/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace Jukcraft {
blocks.emplace_back("Leaves", 6, models.cube, std::vector<uint8_t>{6, 6, 6, 6, 6, 6}, true, 14);

world = std::make_unique<World>(blocks, shader);
player = std::make_unique<Player>(*world, glm::vec3(5.0f, 70.0f, 10.0f), glm::vec3(0.0f), glm::vec3(0.0f), glm::pi<float>() / 2.0f, 0.0f);
player = std::make_unique<Player>(*world, glm::vec3(5.0f, 70.0f, 10.0f), glm::vec3(0.0f),
glm::pi<float>() / 2.0f, 0.0f);
}

void Game::onMousePress(int button) {
Expand Down
8 changes: 4 additions & 4 deletions Jukcraft/src/entity/BipedEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions Jukcraft/src/entity/BipedEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
}
9 changes: 3 additions & 6 deletions Jukcraft/src/entity/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

}
Expand All @@ -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;
};
Expand Down
26 changes: 15 additions & 11 deletions Jukcraft/src/entity/LivingEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -108,16 +110,18 @@ namespace Jukcraft {
}

void LivingEntity::applyPhysics() {
float accelMagnitude = glm::length(glm::vec2(relativeAccel.x, relativeAccel.z));
float headingAngle = yaw - glm::atan<float>(relativeAccel.z, relativeAccel.x) + glm::pi<float>() / 2;
if (input.x || input.z) {
float headingAngle = yaw - glm::atan<float>((float)input.z, (float)input.x) + glm::pi<float>() / 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();

Expand Down
6 changes: 5 additions & 1 deletion Jukcraft/src/entity/LivingEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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)
Expand All @@ -41,6 +43,8 @@ namespace Jukcraft {

float width, height;

const float speed;

bool onGround;

glm::vec3 oldPosition;
Expand Down
4 changes: 2 additions & 2 deletions Jukcraft/src/entity/player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {


}
Expand Down
5 changes: 3 additions & 2 deletions Jukcraft/src/entity/player/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3f6aba4

Please sign in to comment.