Skip to content

Commit

Permalink
Add collision detection for players (#196)
Browse files Browse the repository at this point in the history
* Create player class to hold logic about the player movement and such

* [Player] Make the feel of forces of gravity feel good

* [Client Game] Improve logic surrounding the player id and where the player is referenced from

* make player not small boi
  • Loading branch information
Hopson97 authored Apr 9, 2020
1 parent f0acbc4 commit 50321e1
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tags

open-builder/
OpenBuilder/
.zip
*.zip

texture_packs/ElijahHyper

Expand Down
Binary file removed OpenBuilder.zip
Binary file not shown.
2 changes: 0 additions & 2 deletions game/voxels.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ game.data.addVoxel {
game.data.addVoxel {
name = "openbuilder_sand",
description = "Sand",
collidable = false,
render = {
top = "sand",
sides = "sand",
Expand All @@ -75,7 +74,6 @@ game.data.addVoxel {
game.data.addVoxel {
name = "openbuilder_wood",
description = "Common Wood",
collidable = false,
render = {
top = "logtop",
sides = "log",
Expand Down
2 changes: 1 addition & 1 deletion scripts/CreateDeployable.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mkdir OpenBuilder\res
mkdir OpenBuilder\texture_packs


xcopy /s .\Release\open-builder.exe OpenBuilder
xcopy /s .\Release\builderverse.exe OpenBuilder

xcopy /s /e .\game OpenBuilder\game
xcopy /s /e .\shaders OpenBuilder\shaders
Expand Down
9 changes: 5 additions & 4 deletions src/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ add_library(ob-client
client_state_controller.cpp
client_engine.cpp

game/game.cpp
game/game_def.cpp
game/game_type.cpp
game/chunk_mesh.cpp
game/chunk_mesh_generation.cpp
game/client_world.cpp
game/game.cpp
game/game_def.cpp
game/game_type.cpp
game/player.cpp


lua/client_lua_callback.cpp
lua/gui_api.cpp
Expand All @@ -44,7 +46,6 @@ add_library(ob-client
window.cpp

network/client.cpp
network/client_packet_handling.cpp

renderer/camera.cpp
renderer/gui_renderer.cpp
Expand Down
5 changes: 3 additions & 2 deletions src/client/client_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ void ClientEngine::runClient()

void ClientEngine::update()
{
static sf::Clock clock;
m_fpsCounter.update();
m_game.tick(0.16f);
m_game.tick(clock.restart().asSeconds());
m_gui.update();
if (((int)m_fpsCounter.frameCount % 256) == 0) {
// std::cout << m_fpsCounter.frameTime << '\n';
std::cout << m_fpsCounter.frameTime << '\n';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/client_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ClientEngine {

ClientStateController m_controller;

sf::Window* mp_window;
sf::Window* mp_window = nullptr;
Keyboard m_keyboard;
InputState m_inputState;

Expand Down
26 changes: 7 additions & 19 deletions src/client/game/client_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ ClientWorld::ClientWorld()
m_entityModel = m_entityShader.getUniformLocation("modelMatrix");
}

void ClientWorld::setPlayerId(u32 id)
{
std::cout << "Got player ID: " << id << std::endl;
m_playerId = id;
}

void ClientWorld::setupData(int maxEntities)
{
m_issetup = true;
Expand All @@ -46,9 +40,6 @@ void ClientWorld::tick(float dt)
if (!m_issetup) {
return;
}
auto& player = getPlayer();
player.position += player.velocity * dt;
player.velocity *= 0.90 * dt;

// Update chunks
i32 count = 0;
Expand Down Expand Up @@ -97,6 +88,7 @@ void ClientWorld::tick(float dt)
m_chunkUpdates.push_back({p.x, p.y, p.z + 1});
}
}

m_voxelUpdates.clear();
}

Expand All @@ -108,7 +100,7 @@ void ClientWorld::render(const Camera& camera)
m_voxelTextures.textures.bind();

// Render chunks, getting the block at the player position for """effects"""
auto playerVoxel = m_chunks.getVoxel(toVoxelPosition(getPlayer().position));
auto playerVoxel = m_chunks.getVoxel(toVoxelPosition(camera.getPosition()));
auto waterId = m_voxelData.getVoxelId(CommonVoxel::Water);
m_chunkRenderer.renderChunks(camera, playerVoxel == waterId);

Expand Down Expand Up @@ -206,17 +198,13 @@ void ClientWorld::createChunkFromCompressed(const ChunkPosition& position,
m_chunkUpdates.push_back(position);
}

EntityState& ClientWorld::getPlayer()
{
return m_entities[m_playerId];
}

u32 ClientWorld::getPlayerId() const
void ClientWorld::updateVoxel(const VoxelUpdate& update)
{
return m_playerId;
m_voxelUpdates.push_back(update);
}

void ClientWorld::updateVoxel(const VoxelUpdate& update)
const VoxelData& ClientWorld::getVoxel(int x, int y, int z) const
{
m_voxelUpdates.push_back(update);
auto voxel = m_chunks.getVoxel({x, y, z});
return m_voxelData.getVoxelData(voxel);
}
8 changes: 2 additions & 6 deletions src/client/game/client_world.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ struct VoxelTextureMap {
class ClientWorld {
public:
ClientWorld();

void setPlayerId(u32 id);
void setupData(int maxEntities);

void tick(float dt);
Expand All @@ -39,12 +37,10 @@ class ClientWorld {
bool hasChunk(const ChunkPosition& position) const;
void createChunkFromCompressed(const ChunkPosition& position,
const CompressedVoxels& voxels);

EntityState& getPlayer();
u32 getPlayerId() const;

void updateVoxel(const VoxelUpdate& update);

const VoxelData& getVoxel(int x, int y, int z) const;

private:
std::vector<EntityState> m_entities;

Expand Down
76 changes: 13 additions & 63 deletions src/client/game/game_def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ void SelectedBoxRenderer::render(const Camera& camera, const VoxelPosition& posi
glCheck(glDisable(GL_BLEND));
}


ClientGameDef::ClientGameDef()
: m_client(m_world, m_player)
{
}

bool ClientGameDef::start(const std::string ipAddress)
{
auto connection = m_client.connectTo(ipAddress);
Expand All @@ -42,8 +48,6 @@ bool ClientGameDef::start(const std::string ipAddress)
shutdown();
return false;
}
m_client.setWorld(m_world);

m_camera = Camera::createCamera();
m_selectionBoxRenderer.create();

Expand Down Expand Up @@ -82,13 +86,12 @@ void ClientGameDef::handleInput(const Keyboard& keyboard, const InputState& inpu
}

if (inputState.isMouseLocked) {
handlePlayerInput(keyboard);
m_player.input(keyboard);
}

// Test whether the voxel the player is looking at is interactable
auto& player = m_world.getPlayer();
auto& position = player.position;
auto& rotation = player.rotation;
auto& position = m_player.m_state.position;
auto& rotation = m_player.m_state.rotation;

m_isVoxelSelected = false;
auto voxels = getIntersectedVoxels(position, forwardsVector(rotation), 8);
Expand All @@ -108,15 +111,17 @@ void ClientGameDef::tick(float dt)
return;
}

m_camera.update(m_world.getPlayer());
m_camera.update(m_player.m_state);
m_world.tick(dt);
if (m_client.getConnnectionState() == ConnectionState::Disconnected) {
shutdown();
}

m_player.tick(m_world, dt);

auto thisTime = m_timer.getElapsedTime();
if (thisTime - m_lastTime > sf::milliseconds(50)) {
m_client.sendPlayerState(m_world.getPlayer());
m_client.sendPlayerState(m_player.m_state);
}
}

Expand All @@ -127,58 +132,3 @@ void ClientGameDef::render()
m_selectionBoxRenderer.render(m_camera, m_currentSelectedVoxelPos);
}
}

void ClientGameDef::handlePlayerInput(const Keyboard& keyboard)
{
auto& ctx = *Window::context;
static auto lastMousePosition = sf::Mouse::getPosition(ctx);

auto& player = m_world.getPlayer();
glm::vec3& rotation = player.rotation;
glm::vec3& velocity = player.velocity;

float verticalSensitivity = ClientConfig::get().verticalSensitivity;
float horizontalSensitivity = ClientConfig::get().horizontalSensitivity;
auto change = sf::Mouse::getPosition(ctx) - lastMousePosition;
rotation.x += static_cast<float>(change.y / 8.0f * verticalSensitivity);
rotation.y += static_cast<float>(change.x / 8.0f * horizontalSensitivity);
sf::Mouse::setPosition({(int)ctx.getSize().x / 2, (int)ctx.getSize().y / 2}, ctx);

// This fixes mouse jittering on mac
#ifndef __APPLE__
lastMousePosition = sf::Mouse::getPosition(ctx);
#else
lastMousePosition.x = (int)window.getSize().x / 2;
lastMousePosition.y = (int)window.getSize().y / 2;
#endif

float PLAYER_SPEED = 1.0f;
if (keyboard.isKeyDown(sf::Keyboard::LControl)) {
PLAYER_SPEED *= 10;
}

if (keyboard.isKeyDown(sf::Keyboard::W)) {
velocity += forwardsVector(rotation) * PLAYER_SPEED;
}
else if (keyboard.isKeyDown(sf::Keyboard::S)) {
velocity += backwardsVector(rotation) * PLAYER_SPEED;
}
if (keyboard.isKeyDown(sf::Keyboard::A)) {
velocity += leftVector(rotation) * PLAYER_SPEED;
}
else if (keyboard.isKeyDown(sf::Keyboard::D)) {
velocity += rightVector(rotation) * PLAYER_SPEED;
}
if (keyboard.isKeyDown(sf::Keyboard::Space)) {
velocity.y += PLAYER_SPEED * 2;
}
else if (keyboard.isKeyDown(sf::Keyboard::LShift)) {
velocity.y -= PLAYER_SPEED * 2;
}
if (rotation.x < -80.0f) {
rotation.x = -79.9f;
}
else if (rotation.x > 85.0f) {
rotation.x = 84.9f;
}
}
6 changes: 4 additions & 2 deletions src/client/game/game_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../gl/vertex_array.h"
#include "../renderer/camera.h"
#include "client_world.h"
#include "player.h"

class Keyboard;
struct InputState;
Expand All @@ -25,6 +26,7 @@ struct SelectedBoxRenderer {

class ClientGameDef {
public:
ClientGameDef();
virtual ~ClientGameDef() = default;

void handleEvent(const sf::Event& event);
Expand All @@ -40,14 +42,14 @@ class ClientGameDef {
bool start(const std::string ipAddress);

private:
void handlePlayerInput(const Keyboard& keyboard);

virtual void onShutdown() = 0;

ClientWorld m_world;
Camera m_camera;
Client m_client;

Player m_player;

SelectedBoxRenderer m_selectionBoxRenderer;
VoxelPosition m_currentSelectedVoxelPos;
bool m_isVoxelSelected = false;
Expand Down
Loading

0 comments on commit 50321e1

Please sign in to comment.