Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in extremely basic inertia system for players #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 59 additions & 17 deletions source/client/world/ClientPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,43 +74,85 @@ void ClientPlayer::updateCamera() {

void ClientPlayer::move(float direction) {
direction += m_viewAngleH;
inertiaBuffer.x += 0.001 * cosf(direction * RADIANS_PER_DEGREES);
inertiaBuffer.y += 0.001 * sinf(direction * RADIANS_PER_DEGREES);

m_velocity.x = 0.04f * cosf(direction * RADIANS_PER_DEGREES);
m_velocity.y = 0.04f * sinf(direction * RADIANS_PER_DEGREES);
glm::dvec2 test = glm::dvec2(inertiaBuffer.x, inertiaBuffer.y);
double testSpeed = glm::length(test);

//limit speed
if (testSpeed > maxSpeed){

test = glm::normalize(test);
test *= (maxSpeed + testSpeed) / 20;

//smooth transition
inertiaBuffer.x -= test.x;
inertiaBuffer.y -= test.y;
}
}

void ClientPlayer::applyFriction(){
inertiaBuffer.x /= 1.05;
inertiaBuffer.y /= 1.05;
}

void ClientPlayer::applyVelocity(){
m_velocity.x = inertiaBuffer.x;
m_velocity.y = inertiaBuffer.y;
}



void ClientPlayer::processInputs() {


if(gk::GamePad::isKeyPressed(GameKey::Jump) && !m_isJumping) {
m_isJumping = true;
m_velocity.z = m_jumpSpeed;
}

if(gk::GamePad::isKeyPressed(GameKey::Fly)) {
m_velocity.z = 0.1;
if (gk::GamePad::isKeyPressed(GameKey::Sprint)) {
maxSpeed = 0.05;
}
//sneak overrides sprint
else if(gk::GamePad::isKeyPressed(GameKey::Sneak)) {
maxSpeed = 0.01;
} else {
maxSpeed = 0.03;
}

bool hasPressed = false;

if(gk::GamePad::isKeyPressed(GameKey::Forward)){
move(0.0f);
hasPressed = true;
}

if(gk::GamePad::isKeyPressed(GameKey::Sneak)) {
m_velocity.z = -0.1;
if(gk::GamePad::isKeyPressed(GameKey::Back)){
move(180.0f);
hasPressed = true;
}

if(gk::GamePad::isKeyPressed(GameKey::Forward)) move(0.0f);
else if(gk::GamePad::isKeyPressed(GameKey::Back)) move(180.0f);
if(gk::GamePad::isKeyPressed(GameKey::Left)){
move(90.0f);
hasPressed = true;
}

if(gk::GamePad::isKeyPressed(GameKey::Left)) move(90.0f);
else if(gk::GamePad::isKeyPressed(GameKey::Right)) move(-90.0f);
if(gk::GamePad::isKeyPressed(GameKey::Right)){
move(-90.0f);
hasPressed = true;
}

if (gk::GamePad::isKeyPressed(GameKey::Left) && gk::GamePad::isKeyPressed(GameKey::Forward)) move(45.0f);
if (gk::GamePad::isKeyPressed(GameKey::Right) && gk::GamePad::isKeyPressed(GameKey::Forward)) move(-45.0f);
if (gk::GamePad::isKeyPressed(GameKey::Left) && gk::GamePad::isKeyPressed(GameKey::Back)) move(135.0f);
if (gk::GamePad::isKeyPressed(GameKey::Right) && gk::GamePad::isKeyPressed(GameKey::Back)) move(-135.0f);
applyVelocity();

if (gk::GamePad::isKeyPressed(GameKey::Sprint)) {
m_velocity.x *= 1.5f;
m_velocity.y *= 1.5f;
if (!hasPressed){
applyFriction();
}
}

void ClientPlayer::updatePosition(const ClientWorld &world) {

ClientChunk *chunk = (ClientChunk *)world.getChunkAtBlockPos(m_x, m_y, m_z);
if (chunk && chunk->isInitialized()) {
if (!Config::isFlyModeEnabled) {
Expand Down
8 changes: 8 additions & 0 deletions source/client/world/ClientPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "Player.hpp"


#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
Expand All @@ -54,8 +55,13 @@ class ClientPlayer : public Player {

void updateCamera();

double maxSpeed = 4;

void move(float direction);

void applyVelocity();
void applyFriction();

void processInputs();
void updatePosition(const ClientWorld &world);

Expand All @@ -65,6 +71,8 @@ class ClientPlayer : public Player {
float dirTargetedY() const { return m_forwardDir.y; }
float dirTargetedZ() const { return m_forwardDir.z; }

gk::Vector3d inertiaBuffer = gk::Vector3d();

static ClientPlayer &getInstance() { return *s_instance; }
static void setInstance(ClientPlayer *instance) { s_instance = instance; }

Expand Down