refactoring

This commit is contained in:
lllzebralll 2022-12-08 21:20:45 +03:00
parent a1f9155498
commit 8e68a47712
10 changed files with 160 additions and 159 deletions

View File

@ -65,7 +65,7 @@ void HudRenderer::drawDebug(Level* level, Assets* assets, int fps, bool occlusio
} }
void HudRenderer::draw(Level* level, Assets* assets, bool devdata){ void HudRenderer::draw(Level* level, Assets* assets){
uicamera->fov = Window::height; uicamera->fov = Window::height;
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
@ -81,7 +81,7 @@ void HudRenderer::draw(Level* level, Assets* assets, bool devdata){
batch->texture(nullptr); batch->texture(nullptr);
batch->color = vec4(1.0f); batch->color = vec4(1.0f);
if (Events::_cursor_locked && !devdata) { if (Events::_cursor_locked && !level->player->debug) {
glLineWidth(2); glLineWidth(2);
batch->line(Window::width/2, Window::height/2-6, Window::width/2, Window::height/2+6, 0.2f, 0.2f, 0.2f, 1.0f); batch->line(Window::width/2, Window::height/2-6, Window::width/2, Window::height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(Window::width/2+6, Window::height/2, Window::width/2-6, Window::height/2, 0.2f, 0.2f, 0.2f, 1.0f); batch->line(Window::width/2+6, Window::height/2, Window::width/2-6, Window::height/2, 0.2f, 0.2f, 0.2f, 1.0f);
@ -215,7 +215,7 @@ void HudRenderer::draw(Level* level, Assets* assets, bool devdata){
// batch->render(); // batch->render();
if (Events::_cursor_locked && !devdata){ if (Events::_cursor_locked && !level->player->debug){
// Shader* crosshairShader = assets->getShader("crosshair"); // Shader* crosshairShader = assets->getShader("crosshair");
// crosshairShader->use(); // crosshairShader->use();
// crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width); // crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width);

View File

@ -14,7 +14,7 @@ class HudRenderer {
public: public:
HudRenderer(); HudRenderer();
~HudRenderer(); ~HudRenderer();
void draw(Level* level, Assets* assets, bool devdata); void draw(Level* level, Assets* assets);
void drawDebug(Level* level, Assets* assets, int fps, bool occlusion); void drawDebug(Level* level, Assets* assets, int fps, bool occlusion);
}; };

View File

@ -11,7 +11,7 @@ Player::Player(glm::vec3 position, float speed, Camera* camera) :
camera(camera), camera(camera),
choosenBlock(1), choosenBlock(1),
camX(0.0f), camY(0.0f){ camX(0.0f), camY(0.0f){
hitbox = new Hitbox(position, vec3(0.2f,0.9f,0.2f)); hitbox = new Hitbox(position, vec3(0.3f,0.9f,0.3f));
} }
Player::~Player(){ Player::~Player(){

View File

@ -15,6 +15,7 @@ public:
Hitbox* hitbox; Hitbox* hitbox;
bool flight = false; bool flight = false;
bool noclip = false; bool noclip = false;
bool debug = false;
int choosenBlock; int choosenBlock;
float camX, camY; float camX, camY;
float cameraShaking = 0.0f; float cameraShaking = 0.0f;

View File

@ -5,7 +5,6 @@
#include <iostream> #include <iostream>
#define E 0.03 #define E 0.03
#define DEFAULT_FRICTION 10.0
PhysicsSolver::PhysicsSolver(vec3 gravity) : gravity(gravity) { PhysicsSolver::PhysicsSolver(vec3 gravity) : gravity(gravity) {
} }
@ -28,89 +27,7 @@ void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned s
float pz = pos.z; float pz = pos.z;
if (collisions) { if (collisions) {
if (vel.x < 0.0){ colisionCalc(chunks, hitbox, &vel, &pos, half);
for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){
for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){
int x = floor(pos.x-half.x-E);
if (chunks->isObstacle(x,y,z)){
vel.x *= 0.0;
pos.x = x + 1 + half.x + E;
break;
}
}
}
}
if (vel.x > 0.0){
for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){
for (int z = floor(pos.z-half.z+E); z <= floor(pos.z+half.z-E); z++){
int x = floor(pos.x+half.x+E);
if (chunks->isObstacle(x,y,z)){
vel.x *= 0.0;
pos.x = x - half.x - E;
break;
}
}
}
}
if (vel.z < 0.0){
for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){
for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){
int z = floor(pos.z-half.z-E);
if (chunks->isObstacle(x,y,z)){
vel.z *= 0.0;
pos.z = z + 1 + half.z + E;
break;
}
}
}
}
if (vel.z > 0.0){
for (int y = floor(pos.y-half.y+E); y <= floor(pos.y+half.y-E); y++){
for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){
int z = floor(pos.z+half.z+E);
if (chunks->isObstacle(x,y,z)){
vel.z *= 0.0;
pos.z = z - half.z - E;
break;
}
}
}
}
if (vel.y < 0.0){
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++){
int y = floor(pos.y-half.y-E);
if (chunks->isObstacle(x,y,z)){
vel.y *= 0.0;
pos.y = y + 1 + half.y;
int f = DEFAULT_FRICTION;
vel.x *= max(0.0, 1.0 - dt * f);
vel.z *= max(0.0, 1.0 - dt * f);
hitbox->grounded = true;
broken = true;
break;
}
}
if (broken)
break;
}
}
if (vel.y > 0.0){
for (int x = floor(pos.x-half.x+E); x <= floor(pos.x+half.x-E); x++){
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);
if (chunks->isObstacle(x,y,z)){
vel.y *= 0.0;
pos.y = y - half.y - E;
break;
}
}
}
}
} }
vel.x *= max(0.0, 1.0 - dt * linear_damping); vel.x *= max(0.0, 1.0 - dt * linear_damping);
@ -152,6 +69,85 @@ void PhysicsSolver::step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned s
} }
} }
void PhysicsSolver::colisionCalc(Chunks* chunks, Hitbox* hitbox, vec3* vel, vec3* pos, vec3 half){
if (vel->x < 0.0){
for (int y = floor(pos->y-half.y+E); y <= floor(pos->y+half.y-E); y++){
for (int z = floor(pos->z-half.z+E); z <= floor(pos->z+half.z-E); z++){
int x = floor(pos->x-half.x-E);
if (chunks->isObstacle(x,y,z)){
vel->x *= 0.0;
pos->x = x + 1 + half.x + E;
break;
}
}
}
}
if (vel->x > 0.0){
for (int y = floor(pos->y-half.y+E); y <= floor(pos->y+half.y-E); y++){
for (int z = floor(pos->z-half.z+E); z <= floor(pos->z+half.z-E); z++){
int x = floor(pos->x+half.x+E);
if (chunks->isObstacle(x,y,z)){
vel->x *= 0.0;
pos->x = x - half.x - E;
break;
}
}
}
}
if (vel->z < 0.0){
for (int y = floor(pos->y-half.y+E); y <= floor(pos->y+half.y-E); y++){
for (int x = floor(pos->x-half.x+E); x <= floor(pos->x+half.x-E); x++){
int z = floor(pos->z-half.z-E);
if (chunks->isObstacle(x,y,z)){
vel->z *= 0.0;
pos->z = z + 1 + half.z + E;
break;
}
}
}
}
if (vel->z > 0.0){
for (int y = floor(pos->y-half.y+E); y <= floor(pos->y+half.y-E); y++){
for (int x = floor(pos->x-half.x+E); x <= floor(pos->x+half.x-E); x++){
int z = floor(pos->z+half.z+E);
if (chunks->isObstacle(x,y,z)){
vel->z *= 0.0;
pos->z = z - half.z - E;
break;
}
}
}
}
if (vel->y < 0.0){
for (int x = floor(pos->x-half.x+E); x <= floor(pos->x+half.x-E); x++){
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);
if (chunks->isObstacle(x,y,z)){
vel->y *= 0.0;
pos->y = y + 1 + half.y;
hitbox->grounded = true;
break;
}
}
}
}
if (vel->y > 0.0){
for (int x = floor(pos->x-half.x+E); x <= floor(pos->x+half.x-E); x++){
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);
if (chunks->isObstacle(x,y,z)){
vel->y *= 0.0;
pos->y = y - half.y - E;
break;
}
}
}
}
}
bool PhysicsSolver::isBlockInside(int x, int y, int z, Hitbox* hitbox) { bool PhysicsSolver::isBlockInside(int x, int y, int z, Hitbox* hitbox) {
vec3& pos = hitbox->position; vec3& pos = hitbox->position;
vec3& half = hitbox->halfsize; vec3& half = hitbox->halfsize;

View File

@ -21,6 +21,7 @@ public:
bool shifting, bool shifting,
float gravityScale, float gravityScale,
bool collisions); bool collisions);
void colisionCalc(Chunks* chunks, Hitbox* hitbox, vec3* vel, vec3* pos, vec3 half);
bool isBlockInside(int x, int y, int z, Hitbox* hitbox); bool isBlockInside(int x, int y, int z, Hitbox* hitbox);
}; };

View File

@ -19,8 +19,8 @@
#define RUN_ZOOM 1.1f #define RUN_ZOOM 1.1f
#define C_ZOOM 0.1f #define C_ZOOM 0.1f
#define ZOOM_SPEED 16.0f #define ZOOM_SPEED 16.0f
#define PLAYER_GROUNDED_DAMPING 1.0f #define PLAYER_GROUND_DAMPING 10.0f
#define PLAYER_NOT_GROUNDED_DAMPING 10.0f #define PLAYER_AIR_DAMPING 7.0f
#define CAMERA_SHAKING_OFFSET 0.025f #define CAMERA_SHAKING_OFFSET 0.025f
#define CAMERA_SHAKING_OFFSET_Y 0.031f #define CAMERA_SHAKING_OFFSET_Y 0.031f
#define CAMERA_SHAKING_SPEED 1.6f #define CAMERA_SHAKING_SPEED 1.6f
@ -35,13 +35,14 @@ PlayerController::PlayerController(Level* level) : level(level) {
void PlayerController::update_controls(float delta){ void PlayerController::update_controls(float delta){
Player* player = level->player; Player* player = level->player;
for (int i = 1; i < 10; i++){ /*block choose*/{
if (Events::jpressed(GLFW_KEY_0+i)){ for (int i = 1; i < 10; i++){
player->choosenBlock = i; if (Events::jpressed(GLFW_KEY_0+i)){
player->choosenBlock = i;
}
} }
} }//end
// 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);
@ -65,18 +66,20 @@ void PlayerController::update_controls(float delta){
if (player->flight && hitbox->grounded) if (player->flight && hitbox->grounded)
player->flight = false; player->flight = false;
// Camera shaking
player->interpVel = player->interpVel * (1.0f - delta * 5) + hitbox->velocity * delta * 0.1f; /*camera shaking*/{
if (hitbox->grounded && player->interpVel.y < 0.0f){ player->interpVel = player->interpVel * (1.0f - delta * 5) + hitbox->velocity * delta * 0.1f;
player->interpVel.y *= -30.0f; if (hitbox->grounded && player->interpVel.y < 0.0f){
} player->interpVel.y *= -30.0f;
float factor = hitbox->grounded ? length(vec2(hitbox->velocity.x, hitbox->velocity.z)) : 0.0f; }
player->cameraShakingTimer += delta * factor * CAMERA_SHAKING_SPEED; float factor = hitbox->grounded ? length(vec2(hitbox->velocity.x, hitbox->velocity.z)) : 0.0f;
float shakeTimer = player->cameraShakingTimer; player->cameraShakingTimer += delta * factor * CAMERA_SHAKING_SPEED;
player->cameraShaking = player->cameraShaking * (1.0f - delta * CAMERA_SHAKING_DELTA_K) + factor * delta * CAMERA_SHAKING_DELTA_K; float shakeTimer = player->cameraShakingTimer;
camera->position += camera->right * sin(shakeTimer) * CAMERA_SHAKING_OFFSET * player->cameraShaking; player->cameraShaking = player->cameraShaking * (1.0f - delta * CAMERA_SHAKING_DELTA_K) + factor * delta * CAMERA_SHAKING_DELTA_K;
camera->position += camera->up * abs(cos(shakeTimer)) * CAMERA_SHAKING_OFFSET_Y * player->cameraShaking; camera->position += camera->right * sin(shakeTimer) * CAMERA_SHAKING_OFFSET * player->cameraShaking;
camera->position -= min(player->interpVel * 0.05f, 1.0f); camera->position += camera->up * abs(cos(shakeTimer)) * CAMERA_SHAKING_OFFSET_Y * player->cameraShaking;
camera->position -= min(player->interpVel * 0.05f, 1.0f);
}//end
if ((Events::jpressed(GLFW_KEY_F) && !player->noclip) || if ((Events::jpressed(GLFW_KEY_F) && !player->noclip) ||
(Events::jpressed(GLFW_KEY_N) && player->flight == player->noclip)){ (Events::jpressed(GLFW_KEY_N) && player->flight == player->noclip)){
@ -89,25 +92,25 @@ void PlayerController::update_controls(float delta){
player->noclip = !player->noclip; player->noclip = !player->noclip;
} }
// Field of view manipulations /*field of view manipulations*/{
float dt = min(1.0f, delta * ZOOM_SPEED); float dt = min(1.0f, delta * ZOOM_SPEED);
float zoomValue = 1.0f; float zoomValue = 1.0f;
if (shift){ if (shift){
speed *= CROUCH_SPEED_MUL; speed *= CROUCH_SPEED_MUL;
camera->position.y += CROUCH_SHIFT_Y; camera->position.y += CROUCH_SHIFT_Y;
zoomValue = CROUCH_ZOOM; zoomValue = CROUCH_ZOOM;
} else if (sprint){ } else if (sprint){
speed *= RUN_SPEED_MUL; speed *= RUN_SPEED_MUL;
zoomValue = RUN_ZOOM; zoomValue = RUN_ZOOM;
} }
if (zoom) if (zoom)
zoomValue *= C_ZOOM; zoomValue *= C_ZOOM;
camera->zoom = zoomValue * dt + camera->zoom * (1.0f - dt); camera->zoom = zoomValue * dt + camera->zoom * (1.0f - dt);
}//end
if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){ if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){
hitbox->velocity.y = JUMP_FORCE; hitbox->velocity.y = JUMP_FORCE;
} }
vec3 dir(0,0,0); vec3 dir(0,0,0);
if (Events::pressed(GLFW_KEY_W)){ if (Events::pressed(GLFW_KEY_W)){
dir.x += camera->dir.x; dir.x += camera->dir.x;
@ -126,9 +129,9 @@ void PlayerController::update_controls(float delta){
dir.z -= camera->right.z; dir.z -= camera->right.z;
} }
hitbox->linear_damping = PLAYER_GROUNDED_DAMPING; hitbox->linear_damping = PLAYER_GROUND_DAMPING;
if (player->flight){ if (player->flight){
hitbox->linear_damping = PLAYER_NOT_GROUNDED_DAMPING; hitbox->linear_damping = PLAYER_AIR_DAMPING;
hitbox->velocity.y *= 1.0f - delta * 9; hitbox->velocity.y *= 1.0f - delta * 9;
if (Events::pressed(GLFW_KEY_SPACE)){ if (Events::pressed(GLFW_KEY_SPACE)){
hitbox->velocity.y += speed * delta * 9; hitbox->velocity.y += speed * delta * 9;
@ -137,41 +140,38 @@ void PlayerController::update_controls(float delta){
hitbox->velocity.y -= speed * delta * 9; hitbox->velocity.y -= speed * delta * 9;
} }
} }
if (!hitbox->grounded)
hitbox->linear_damping = PLAYER_AIR_DAMPING;
if (length(dir) > 0.0f){ if (length(dir) > 0.0f){
dir = normalize(dir); dir = normalize(dir);
if (!hitbox->grounded)
hitbox->linear_damping = PLAYER_NOT_GROUNDED_DAMPING;
hitbox->velocity.x += dir.x * speed * delta * 9; hitbox->velocity.x += dir.x * speed * delta * 9;
hitbox->velocity.z += dir.z * speed * delta * 9; hitbox->velocity.z += dir.z * speed * delta * 9;
} }
// camera rotate /*camera rotate*/{
if (Events::_cursor_locked){
float rotX = -Events::deltaX / Window::height * 2;
float rotY = -Events::deltaY / Window::height * 2;
if (zoom){
rotX /= 4;
rotY /= 4;
}
player->camX += rotX;
player->camY += rotY;
if (Events::_cursor_locked){ if (player->camY < -radians(89.9f)){
float rotX = -Events::deltaX / Window::height * 2; player->camY = -radians(89.9f);
float rotY = -Events::deltaY / Window::height * 2; }
if (zoom){ if (player->camY > radians(89.9f)){
rotX /= 4; player->camY = radians(89.9f);
rotY /= 4; }
}
player->camX += rotX;
player->camY += rotY;
if (player->camY < -radians(89.9f)){ camera->rotation = mat4(1.0f);
player->camY = -radians(89.9f); camera->rotate(player->camY, player->camX, 0);
} }
if (player->camY > radians(89.9f)){ }//end
player->camY = radians(89.9f);
}
camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
}
} }
// end camera rotate
void PlayerController::update_interaction(){ void PlayerController::update_interaction(){
Chunks* chunks = level->chunks; Chunks* chunks = level->chunks;

View File

@ -77,8 +77,12 @@ void write_world(World* world, Level* level){
void update_level(World* world, Level* level, float delta, long frame, VoxelRenderer* renderer) { void update_level(World* world, Level* level, float delta, long frame, VoxelRenderer* renderer) {
level->playerController->update_controls(delta); level->playerController->update_controls(delta);
if (Events::_cursor_locked) if (Events::_cursor_locked){
level->playerController->update_interaction(); level->playerController->update_interaction();
} else
{
level->playerController->selectedBlockId = -1;
}
vec3 position = level->player->hitbox->position; vec3 position = level->player->hitbox->position;
level->chunks->setCenter(world->wfile, position.x, position.z); level->chunks->setCenter(world->wfile, position.x, position.z);
@ -119,7 +123,6 @@ void mainloop(Level* level, Assets* assets) {
float lastTime = glfwGetTime(); float lastTime = glfwGetTime();
float delta = 0.0f; float delta = 0.0f;
bool occlusion = true; bool occlusion = true;
bool devdata = false;
Window::swapInterval(1); Window::swapInterval(1);
while (!Window::isShouldClose()){ while (!Window::isShouldClose()){
frame++; frame++;
@ -137,7 +140,7 @@ void mainloop(Level* level, Assets* assets) {
occlusion = !occlusion; occlusion = !occlusion;
} }
if (Events::jpressed(GLFW_KEY_F3)){ if (Events::jpressed(GLFW_KEY_F3)){
devdata = !devdata; level->player->debug = !level->player->debug;
} }
if (Events::jpressed(GLFW_KEY_F5)){ if (Events::jpressed(GLFW_KEY_F5)){
for (unsigned i = 0; i < level->chunks->volume; i++) { for (unsigned i = 0; i < level->chunks->volume; i++) {
@ -159,9 +162,9 @@ void mainloop(Level* level, Assets* assets) {
for (int i = 0; i < freeLoaders; i++) for (int i = 0; i < freeLoaders; i++)
level->chunksController->loadVisible(world->wfile); level->chunksController->loadVisible(world->wfile);
worldRenderer.draw(world, camera, occlusion, devdata); worldRenderer.draw(world, camera, occlusion);
hud.draw(level, assets, devdata); hud.draw(level, assets);
if (devdata) { if (level->player->debug) {
hud.drawDebug(level, assets, fps, occlusion); hud.drawDebug(level, assets, fps, occlusion);
} }

View File

@ -73,7 +73,7 @@ bool WorldRenderer::drawChunk(size_t index, Camera* camera, Shader* shader, bool
} }
void WorldRenderer::draw(World* world, Camera* camera, bool occlusion, bool devdata){ void WorldRenderer::draw(World* world, Camera* camera, bool occlusion){
Chunks* chunks = level->chunks; Chunks* chunks = level->chunks;
vec4 skyColor(0.7f, 0.81f, 1.0f, 1.0f); vec4 skyColor(0.7f, 0.81f, 1.0f, 1.0f);
@ -149,7 +149,7 @@ void WorldRenderer::draw(World* world, Camera* camera, bool occlusion, bool devd
lineBatch->render(); lineBatch->render();
} }
if (devdata) { if (level->player->debug) {
linesShader->use(); linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView()); linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView());

View File

@ -37,7 +37,7 @@ public:
WorldRenderer(Level* level, Assets* assets); WorldRenderer(Level* level, Assets* assets);
~WorldRenderer(); ~WorldRenderer();
void draw(World* world, Camera* camera, bool occlusion, bool devdata); void draw(World* world, Camera* camera, bool occlusion);
}; };