Fixed PhysicsSolver, added zoom, linear_damping

This commit is contained in:
MihailRis 2022-03-03 21:36:23 +03:00 committed by GitHub
parent 2aba3e91db
commit 10931372cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 83 additions and 46 deletions

View File

@ -23,13 +23,13 @@ CPP_DEPS :=
# Every subdirectory with source files must be described here # Every subdirectory with source files must be described here
SUBDIRS := \ SUBDIRS := \
src \
src/files \ src/files \
src/graphics \ src/graphics \
src/lighting \ src/lighting \
src/loaders \ src/loaders \
src/objects \ src/objects \
src/physics \ src/physics \
src \
src/voxels \ src/voxels \
src/window \ src/window \

View File

@ -1,27 +1,31 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables # Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \ CPP_SRCS += \
../src/graphics/Batch2D.cpp \
../src/graphics/LineBatch.cpp \ ../src/graphics/LineBatch.cpp \
../src/graphics/Mesh.cpp \ ../src/graphics/Mesh.cpp \
../src/graphics/Shader.cpp \ ../src/graphics/Shader.cpp \
../src/graphics/Texture.cpp \ ../src/graphics/Texture.cpp \
../src/graphics/VoxelRenderer.cpp \ ../src/graphics/VoxelRenderer.cpp
../src/graphics/Batch2D.cpp
OBJS += \ OBJS += \
./src/graphics/Batch2D.o \
./src/graphics/LineBatch.o \ ./src/graphics/LineBatch.o \
./src/graphics/Mesh.o \ ./src/graphics/Mesh.o \
./src/graphics/Shader.o \ ./src/graphics/Shader.o \
./src/graphics/Texture.o \ ./src/graphics/Texture.o \
./src/graphics/VoxelRenderer.o \ ./src/graphics/VoxelRenderer.o
./src/graphics/Batch2D.o
CPP_DEPS += \ CPP_DEPS += \
./src/graphics/Batch2D.d \
./src/graphics/LineBatch.d \ ./src/graphics/LineBatch.d \
./src/graphics/Mesh.d \ ./src/graphics/Mesh.d \
./src/graphics/Shader.d \ ./src/graphics/Shader.d \
./src/graphics/Texture.d \ ./src/graphics/Texture.d \
./src/graphics/VoxelRenderer.d \ ./src/graphics/VoxelRenderer.d
./src/graphics/Batch2D.d
# Each subdirectory must supply rules for building sources it contributes # Each subdirectory must supply rules for building sources it contributes

View File

@ -1,3 +1,7 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables # Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \ CPP_SRCS += \
../src/objects/Player.cpp ../src/objects/Player.cpp

View File

@ -4,16 +4,16 @@
# Add inputs and outputs from these tool invocations to the build variables # Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \ CPP_SRCS += \
../src/voxel_engine.cpp \ ../src/Assets.cpp \
../src/Assets.cpp ../src/voxel_engine.cpp
OBJS += \ OBJS += \
./src/voxel_engine.o \ ./src/Assets.o \
./src/Assets.o ./src/voxel_engine.o
CPP_DEPS += \ CPP_DEPS += \
./src/voxel_engine.d \ ./src/Assets.d \
./src/Assets.d ./src/voxel_engine.d
# Each subdirectory must supply rules for building sources it contributes # Each subdirectory must supply rules for building sources it contributes

View File

@ -1,4 +1,4 @@
#include "Hitbox.h" #include "Hitbox.h"
Hitbox::Hitbox(vec3 position, vec3 halfsize) : position(position), halfsize(halfsize), velocity(0.0f,0.0f,0.0f) { Hitbox::Hitbox(vec3 position, vec3 halfsize) : position(position), halfsize(halfsize), velocity(0.0f,0.0f,0.0f), linear_damping(0.1f) {
} }

View File

@ -12,6 +12,7 @@ public:
vec3 position; vec3 position;
vec3 halfsize; vec3 halfsize;
vec3 velocity; vec3 velocity;
float linear_damping;
bool grounded = false; bool grounded = false;
Hitbox(vec3 position, vec3 halfsize); Hitbox(vec3 position, vec3 halfsize);

View File

@ -4,7 +4,7 @@
#include <iostream> #include <iostream>
#define E 0.01 #define E 0.03
PhysicsSolver::PhysicsSolver(vec3 gravity) : gravity(gravity) { PhysicsSolver::PhysicsSolver(vec3 gravity) : gravity(gravity) {
} }
@ -12,6 +12,7 @@ PhysicsSolver::PhysicsSolver(vec3 gravity) : gravity(gravity) {
void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned substeps, bool shifting) { void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned substeps, bool shifting) {
for (unsigned i = 0; i < substeps; i++){ for (unsigned i = 0; i < substeps; i++){
float dt = delta / (float)substeps; float dt = delta / (float)substeps;
float linear_damping = hitbox->linear_damping;
vec3& pos = hitbox->position; vec3& pos = hitbox->position;
vec3& half = hitbox->halfsize; vec3& half = hitbox->halfsize;
vec3& vel = hitbox->velocity; vec3& vel = hitbox->velocity;
@ -76,6 +77,7 @@ void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned s
hitbox->grounded = false; hitbox->grounded = false;
if (vel.y < 0.0){ if (vel.y < 0.0){
for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){ for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){
bool broken = false;
for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){ for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){
int y = floor(pos.y-half.y-E); int y = floor(pos.y-half.y-E);
if (chunks->isObstacle(x,y,z)){ if (chunks->isObstacle(x,y,z)){
@ -85,9 +87,12 @@ void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned s
vel.x *= max(0.0, 1.0 - dt * f); vel.x *= max(0.0, 1.0 - dt * f);
vel.z *= max(0.0, 1.0 - dt * f); vel.z *= max(0.0, 1.0 - dt * f);
hitbox->grounded = true; hitbox->grounded = true;
broken = true;
break; break;
} }
} }
if (broken)
break;
} }
} }
if (vel.y > 0.0){ if (vel.y > 0.0){
@ -103,6 +108,9 @@ void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned s
} }
} }
vel.x *= max(0.0, 1.0 - dt * linear_damping);
vel.z *= max(0.0, 1.0 - dt * linear_damping);
pos.x += vel.x * dt; pos.x += vel.x * dt;
pos.y += vel.y * dt; pos.y += vel.y * dt;
pos.z += vel.z * dt; pos.z += vel.z * dt;

View File

@ -64,15 +64,27 @@ void close_world(WorldFiles* wfile, Chunks* chunks){
delete wfile; delete wfile;
} }
#define CROUCH_SPEED_MUL 0.25f
#define CROUCH_SHIFT_Y -0.2f
#define RUN_SPEED_MUL 1.5f
#define CROUCH_ZOOM 0.9f
#define RUN_ZOOM 1.1f
#define C_ZOOM 0.5f
#define ZOOM_SPEED 16.0f
#define DEFAULT_AIR_DAMPING 0.1f
#define PLAYER_NOT_ONGROUND_DAMPING 18.0f
void update_controls(PhysicsSolver* physics, void update_controls(PhysicsSolver* physics,
Chunks* chunks, Chunks* chunks,
Player* player, Player* player,
float delta){ float delta){
// Controls // Controls
Camera* camera = player->camera; Camera* camera = player->camera;
Hitbox* hitbox = player->hitbox; Hitbox* hitbox = player->hitbox;
bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL); bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL);
bool shift = Events::pressed(GLFW_KEY_LEFT_SHIFT) && hitbox->grounded && !sprint; bool shift = Events::pressed(GLFW_KEY_LEFT_SHIFT) && hitbox->grounded && !sprint;
bool zoom = Events::pressed(GLFW_KEY_C);
float speed = player->speed; float speed = player->speed;
int substeps = (int)(delta * 1000); int substeps = (int)(delta * 1000);
@ -82,17 +94,20 @@ void update_controls(PhysicsSolver* physics,
camera->position.y = hitbox->position.y + 0.5f; camera->position.y = hitbox->position.y + 0.5f;
camera->position.z = hitbox->position.z; camera->position.z = hitbox->position.z;
float dt = min(1.0f, delta * 16); float dt = min(1.0f, delta * ZOOM_SPEED);
if (shift){ if (shift){
speed *= 0.25f; speed *= CROUCH_SPEED_MUL;
camera->position.y -= 0.2f; camera->position.y += CROUCH_SHIFT_Y;
camera->zoom = 0.9f * dt + camera->zoom * (1.0f - dt); camera->zoom = CROUCH_ZOOM * dt + camera->zoom * (1.0f - dt);
} else if (sprint){ } else if (sprint){
speed *= 1.5f; speed *= RUN_SPEED_MUL;
camera->zoom = 1.1f * dt + camera->zoom * (1.0f - dt); camera->zoom = RUN_ZOOM * dt + camera->zoom * (1.0f - dt);
} else { } else {
camera->zoom = dt + camera->zoom * (1.0f - dt); camera->zoom = dt + camera->zoom * (1.0f - dt);
} }
if (zoom)
camera->zoom = C_ZOOM * dt + camera->zoom * (1.0f - dt);
if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){ if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){
hitbox->velocity.y = 6.0f; hitbox->velocity.y = 6.0f;
} }
@ -114,10 +129,17 @@ void update_controls(PhysicsSolver* physics,
dir.x -= camera->right.x; dir.x -= camera->right.x;
dir.z -= camera->right.z; dir.z -= camera->right.z;
} }
if (length(dir) > 0.0f)
hitbox->linear_damping = DEFAULT_AIR_DAMPING;
if (length(dir) > 0.0f){
dir = normalize(dir); dir = normalize(dir);
hitbox->velocity.x = dir.x * speed;
hitbox->velocity.z = dir.z * speed; if (!hitbox->grounded)
hitbox->linear_damping = PLAYER_NOT_ONGROUND_DAMPING;
hitbox->velocity.x += dir.x * speed * delta * 16;
hitbox->velocity.z += dir.z * speed * delta * 16;
}
if (Events::_cursor_locked){ if (Events::_cursor_locked){
player->camY += -Events::deltaY / Window::height * 2; player->camY += -Events::deltaY / Window::height * 2;
@ -137,29 +159,27 @@ void update_controls(PhysicsSolver* physics,
void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player, Lighting* lighting){ void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player, Lighting* lighting){
Camera* camera = player->camera; Camera* camera = player->camera;
{ vec3 end;
vec3 end; vec3 norm;
vec3 norm; vec3 iend;
vec3 iend; voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend);
voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend); if (vox != nullptr){
if (vox != nullptr){ lineBatch->box(iend.x+0.5f, iend.y+0.5f, iend.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f);
lineBatch->box(iend.x+0.5f, iend.y+0.5f, iend.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f);
if (Events::jclicked(GLFW_MOUSE_BUTTON_1)){ if (Events::jclicked(GLFW_MOUSE_BUTTON_1)){
int x = (int)iend.x; int x = (int)iend.x;
int y = (int)iend.y; int y = (int)iend.y;
int z = (int)iend.z; int z = (int)iend.z;
chunks->set(x,y,z, 0); chunks->set(x,y,z, 0);
lighting->onBlockSet(x,y,z,0); lighting->onBlockSet(x,y,z,0);
} }
if (Events::jclicked(GLFW_MOUSE_BUTTON_2)){ if (Events::jclicked(GLFW_MOUSE_BUTTON_2)){
int x = (int)(iend.x)+(int)(norm.x); int x = (int)(iend.x)+(int)(norm.x);
int y = (int)(iend.y)+(int)(norm.y); int y = (int)(iend.y)+(int)(norm.y);
int z = (int)(iend.z)+(int)(norm.z); int z = (int)(iend.z)+(int)(norm.z);
if (!physics->isBlockInside(x,y,z, player->hitbox)){ if (!physics->isBlockInside(x,y,z, player->hitbox)){
chunks->set(x, y, z, player->choosenBlock); chunks->set(x, y, z, player->choosenBlock);
lighting->onBlockSet(x,y,z, player->choosenBlock); lighting->onBlockSet(x,y,z, player->choosenBlock);
}
} }
} }
} }