Skip to content

Commit

Permalink
WIP Collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukitsu committed Nov 26, 2023
1 parent bd1f759 commit 569d991
Show file tree
Hide file tree
Showing 24 changed files with 300 additions and 97 deletions.
8 changes: 5 additions & 3 deletions Jukcraft/Jukcraft.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@
<ClInclude Include="src\core\Window.h" />
<ClInclude Include="src\core\util.h" />
<ClInclude Include="src\entity\Entity.h" />
<ClInclude Include="src\entity\Mob.h" />
<ClInclude Include="src\entity\LivingEntity.h" />
<ClInclude Include="src\entity\BipedEntity.h" />
<ClInclude Include="src\entity\player\Player.h" />
<ClInclude Include="src\models\Collider.h" />
<ClInclude Include="src\pch.h" />
Expand All @@ -169,7 +170,7 @@
<ClInclude Include="src\renderer\gfx\objects\Buffer.h" />
<ClInclude Include="src\renderer\gfx\objects\Shader.h" />
<ClInclude Include="src\renderer\gfx\objects\VertexArray.h" />
<ClInclude Include="src\renderer\models\Model.h" />
<ClInclude Include="src\models\Model.h" />
<ClInclude Include="src\renderer\texture\TextureManager.h" />
<ClInclude Include="src\world\LightEngine.h" />
<ClInclude Include="src\world\World.h" />
Expand All @@ -184,7 +185,8 @@
<ClCompile Include="src\core\HitRay.cpp" />
<ClCompile Include="src\core\Log.cpp" />
<ClCompile Include="src\core\Window.cpp" />
<ClCompile Include="src\entity\Mob.cpp" />
<ClCompile Include="src\entity\LivingEntity.cpp" />
<ClCompile Include="src\entity\BipedEntity.cpp" />
<ClCompile Include="src\entity\player\Player.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\pch.cpp">
Expand Down
8 changes: 5 additions & 3 deletions Jukcraft/Jukcraft.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<ClInclude Include="src\renderer\gfx\objects\VertexArray.h">
<Filter>renderer\gfx\objects</Filter>
</ClInclude>
<ClInclude Include="src\renderer\models\Model.h">
<ClInclude Include="src\models\Model.h">
<Filter>renderer\models</Filter>
</ClInclude>
<ClInclude Include="src\renderer\texture\TextureManager.h">
Expand All @@ -103,8 +103,9 @@
<ClInclude Include="src\renderer\gfx\buffers\StagingBuffer.h" />
<ClInclude Include="src\entity\Entity.h" />
<ClInclude Include="src\entity\player\Player.h" />
<ClInclude Include="src\entity\Mob.h" />
<ClInclude Include="src\entity\BipedEntity.h" />
<ClInclude Include="src\models\Collider.h" />
<ClInclude Include="src\entity\LivingEntity.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\core\App.cpp">
Expand Down Expand Up @@ -144,6 +145,7 @@
</ClCompile>
<ClCompile Include="src\renderer\gfx\buffers\DynamicBuffer.cpp" />
<ClCompile Include="src\entity\player\Player.cpp" />
<ClCompile Include="src\entity\Mob.cpp" />
<ClCompile Include="src\entity\BipedEntity.cpp" />
<ClCompile Include="src\entity\LivingEntity.cpp" />
</ItemGroup>
</Project>
5 changes: 4 additions & 1 deletion Jukcraft/src/blocks/Block.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "renderer/models/Model.h"
#include "models/Model.h"

namespace Jukcraft {
class Block {
Expand All @@ -11,6 +11,9 @@ namespace Jukcraft {
bool isCube() const {
return model.getQuads().size() == 6;
}
constexpr const std::vector<Collider>& getColliders() const {
return model.getColliders();
}
constexpr const Model& getModel() const {
return model;
}
Expand Down
41 changes: 27 additions & 14 deletions Jukcraft/src/core/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,40 @@ namespace Jukcraft {
}

void App::run() {
float lastTime = static_cast<float>(glfwGetTime());
float lastTime2 = lastTime;

float lastTime = static_cast<float>(glfwGetTime()), timer = lastTime;
float deltaTimePerTick = 0.0f, renderDeltaTime = 0.0f, nowTime = 0.0f;
int frames = 0, updates = 0;

float deltaTime = 0.0f;
while (!window->shouldClose()) {
const float time = static_cast<float>(glfwGetTime());
const float deltaTime = time - lastTime;
lastTime = time;

game->tick(deltaTime);
nowTime = static_cast<float>(glfwGetTime());
renderDeltaTime = (nowTime - lastTime);
deltaTimePerTick += (nowTime - lastTime) * TICK_RATE;
lastTime = nowTime;

lastTime2 = lastTime;
while (glfwGetTime() - lastTime < 1.0f / TICK_RATE) {
const float time2 = static_cast<float>(glfwGetTime());
const float deltaTime2 = time2 - lastTime2;
lastTime2 = time2;
game->renderNewFrame(deltaTime2);
// - Only update at 60 tick / s
while (deltaTimePerTick >= 1.0) {
deltaTime = deltaTimePerTick / TICK_RATE;
game->tick(deltaTime); // - Update function
updates++;
deltaTimePerTick--;

window->endFrame();
}


game->renderNewFrame(renderDeltaTime);
frames++;

window->endFrame();

if (glfwGetTime() - timer > 1.0) {
timer++;
LOG_TRACE("FPS: {}, Updates: {}", frames, updates);
updates = 0, frames = 0;
}


}
}
void App::onKeyPress(int key) {
Expand Down
28 changes: 16 additions & 12 deletions Jukcraft/src/core/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@ static constexpr float WALKING_SPEED = 4.317f;
static constexpr float sensitivity = 0.005f;

namespace Jukcraft {
Camera::Camera(gfx::Shader& shader, Player& player)
Camera::Camera(gfx::Shader& shader, std::unique_ptr<Player>& player)
:shader(shader), player(player), speed(WALKING_SPEED) {
ubo.allocate(sizeof(ShaderCameraData), nullptr, true);
mappedUbo = reinterpret_cast<ShaderCameraData*>(ubo.map(0, sizeof(ShaderCameraData)));

ubo.bindRange(gfx::BufferBindingTarget::Uniform, 0, 0, sizeof(ShaderCameraData));
lastCursorPos = App::GetWindow().getCursorPos();

}


void Camera::update(const float deltaTime) {
glm::vec3 interpolatedPos = glm::mix(player->position, player->oldPosition, player->interpolationStep);
player->interpolationStep -= deltaTime;

const glm::vec2 cursorPos = App::GetWindow().getCursorPos();
const glm::vec2 deltaCursor = cursorPos - lastCursorPos;
lastCursorPos = cursorPos;


player.yaw = player.yaw + deltaCursor.x * sensitivity, 2.0f * glm::pi<float>();
player.pitch = glm::clamp(player.pitch - deltaCursor.y * sensitivity, -glm::pi<float>() / 2, glm::pi<float>() / 2);
player->yaw = player->yaw + deltaCursor.x * sensitivity, 2.0f * glm::pi<float>();
player->pitch = glm::clamp(player->pitch - deltaCursor.y * sensitivity, -glm::pi<float>() / 2, glm::pi<float>() / 2);
// It is negative because the origin is in the top left corner, not bottom left

if (!App::Get().isMouseCaptured())
Expand All @@ -46,27 +50,27 @@ namespace Jukcraft {

const float multiplier = speed;
if (input.y) {
player.velocity.y = input.y * multiplier;
player->velocity.y = input.y * multiplier;
} if (input.x || input.z) {
const float angle = player.yaw - glm::atan<float>((float)input.z, (float)input.x) + glm::pi<float>() / 2;
player.velocity.x = glm::cos(angle) * multiplier;
player.velocity.z = glm::sin(angle) * multiplier;
const float angle = player->yaw - glm::atan<float>((float)input.z, (float)input.x) + glm::pi<float>() / 2;
player->velocity.x = glm::cos(angle) * multiplier;
player->velocity.z = glm::sin(angle) * multiplier;
}


glm::mat4 proj = glm::perspective(
glm::radians(90.0f),
glm::radians(70.0f),
static_cast<float>(App::GetWindow().getWidth()) / App::GetWindow().getHeight(),
0.1f,
500.0f
);
glm::mat4 view = glm::rotate(glm::mat4(1.0f), player.pitch, -glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, player.yaw + glm::pi<float>() / 2, glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::translate(view, -player.position);
glm::mat4 view = glm::rotate(glm::mat4(1.0f), player->pitch, -glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, player->yaw + glm::pi<float>() / 2, glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::translate(view, -interpolatedPos - glm::vec3(0, player->getEyeLevel(), 0));

mappedUbo->transform = proj * view;

mappedUbo->pos = glm::vec4(player.position, 1.0f);
mappedUbo->pos = glm::vec4(player->position, 1.0f);
ubo.flush(0, sizeof(ShaderCameraData));
}
}
4 changes: 2 additions & 2 deletions Jukcraft/src/core/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace Jukcraft {

class Camera {
public:
Camera(gfx::Shader& shader, Player& player);
Camera(gfx::Shader& shader, std::unique_ptr<Player>& player);
void update(const float delta_time);
private:
gfx::Buffer ubo;
gfx::Shader& shader;

glm::vec2 lastCursorPos;
Player& player;
std::unique_ptr<Player>& player;

float speed;

Expand Down
11 changes: 6 additions & 5 deletions Jukcraft/src/core/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Jukcraft {

Game::Game()
:shader("assets/shaders/terrain/vert.glsl", "assets/shaders/terrain/frag.glsl"), player(glm::vec3(5.0f, 70.0f, 10.0f), glm::vec3(0.0f), glm::vec3(0.0f), glm::pi<float>() / 2.0f, 0.0f), camera(shader, player), textureManager(16) {
:shader("assets/shaders/terrain/vert.glsl", "assets/shaders/terrain/frag.glsl"), camera(shader, player), textureManager(16) {
textureManager.pushSubTexture("assets/textures/stone.png");
textureManager.pushSubTexture("assets/textures/grass.png");
textureManager.pushSubTexture("assets/textures/grass_side.png");
Expand All @@ -24,11 +24,12 @@ namespace Jukcraft {
blocks.emplace_back("Planks", 5, models.cube, std::vector<uint8_t>{5, 5, 5, 5, 5, 5}, 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);
}

void Game::onMousePress(int button) {

HitRay hitray(*world, player);
HitRay hitray(*world, *player);
World& w = *world;
while (hitray.distance < HIT_RANGE)
if (hitray.step(button,
Expand All @@ -44,7 +45,7 @@ namespace Jukcraft {
world->setBlock(nextBlock, 0);
break;
case GLFW_MOUSE_BUTTON_RIGHT:
world->setBlock(currentBlock, holding);
world->trySetBlock(*player, currentBlock, holding);
break;
case GLFW_MOUSE_BUTTON_MIDDLE:
holding = world->getBlock(nextBlock);
Expand All @@ -54,12 +55,12 @@ namespace Jukcraft {


void Game::tick(const float deltaTime) {
world->tick(deltaTime);
world->tick(deltaTime);
player->tick(deltaTime);
}

void Game::renderNewFrame(const float deltaTime) {
camera.update(deltaTime);
player.tick(deltaTime);

Renderer::Begin(world->getSkyColor());
world->render();
Expand Down
2 changes: 1 addition & 1 deletion Jukcraft/src/core/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Jukcraft {
BlockID holding = 5;
bool inWorld = true;

Player player;
std::unique_ptr<Player> player;

std::vector<Block> blocks;
};
Expand Down
4 changes: 2 additions & 2 deletions Jukcraft/src/core/HitRay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


namespace Jukcraft {
HitRay::HitRay(World& world, Entity& entity)
:world(world), position(entity.getPos() - glm::vec3(0.5f)), block((glm::ivec3)glm::round(entity.getPos() - glm::vec3(0.5f))), distance(0.0f) {
HitRay::HitRay(World& world, LivingEntity& entity)
:world(world), position(entity.getPos() - glm::vec3(0.5f) + glm::vec3(0, entity.getEyeLevel(), 0)), block((glm::ivec3)glm::round(entity.getPos() - glm::vec3(0.5f) + glm::vec3(0, entity.getEyeLevel(), 0))), distance(0.0f) {
vector = glm::vec3(
glm::cos(entity.getYaw()) * glm::cos(entity.getPitch()),
glm::sin(entity.getPitch()),
Expand Down
2 changes: 1 addition & 1 deletion Jukcraft/src/core/HitRay.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Jukcraft {
using HitCallback = std::function<void(int, const glm::vec3&, const glm::vec3&)>;

struct HitRay {
HitRay(World& world, Entity& entity);
HitRay(World& world, LivingEntity& entity);
bool check(int button, HitCallback callback,
float distance, const glm::vec3& currentBlock, const glm::vec3& nextBlock);
bool step(int button, HitCallback callback);
Expand Down
4 changes: 4 additions & 0 deletions Jukcraft/src/core/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ namespace Jukcraft {
return source;
}

template<typename T>
int sign(T x) {
return (x > 0) - (x <= 0);
}
}
16 changes: 16 additions & 0 deletions Jukcraft/src/entity/BipedEntity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "pch.h"
#include "entity/BipedEntity.h"
#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() {

}

}
12 changes: 12 additions & 0 deletions Jukcraft/src/entity/BipedEntity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include "entity/LivingEntity.h"

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);
virtual ~BipedEntity();
};
}
Loading

0 comments on commit 569d991

Please sign in to comment.