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;
glDisable(GL_DEPTH_TEST);
@ -81,7 +81,7 @@ void HudRenderer::draw(Level* level, Assets* assets, bool devdata){
batch->texture(nullptr);
batch->color = vec4(1.0f);
if (Events::_cursor_locked && !devdata) {
if (Events::_cursor_locked && !level->player->debug) {
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+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();
if (Events::_cursor_locked && !devdata){
if (Events::_cursor_locked && !level->player->debug){
// Shader* crosshairShader = assets->getShader("crosshair");
// crosshairShader->use();
// crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width);

View File

@ -14,7 +14,7 @@ class HudRenderer {
public:
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);
};

View File

@ -11,7 +11,7 @@ Player::Player(glm::vec3 position, float speed, Camera* camera) :
camera(camera),
choosenBlock(1),
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(){

View File

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

View File

@ -5,7 +5,6 @@
#include <iostream>
#define E 0.03
#define DEFAULT_FRICTION 10.0
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;
if (collisions) {
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++){
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;
}
}
}
}
colisionCalc(chunks, hitbox, &vel, &pos, half);
}
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) {
vec3& pos = hitbox->position;
vec3& half = hitbox->halfsize;

View File

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

View File

@ -19,8 +19,8 @@
#define RUN_ZOOM 1.1f
#define C_ZOOM 0.1f
#define ZOOM_SPEED 16.0f
#define PLAYER_GROUNDED_DAMPING 1.0f
#define PLAYER_NOT_GROUNDED_DAMPING 10.0f
#define PLAYER_GROUND_DAMPING 10.0f
#define PLAYER_AIR_DAMPING 7.0f
#define CAMERA_SHAKING_OFFSET 0.025f
#define CAMERA_SHAKING_OFFSET_Y 0.031f
#define CAMERA_SHAKING_SPEED 1.6f
@ -35,13 +35,14 @@ PlayerController::PlayerController(Level* level) : level(level) {
void PlayerController::update_controls(float delta){
Player* player = level->player;
/*block choose*/{
for (int i = 1; i < 10; i++){
if (Events::jpressed(GLFW_KEY_0+i)){
player->choosenBlock = i;
}
}
}//end
// Controls
Camera* camera = player->camera;
Hitbox* hitbox = player->hitbox;
bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL);
@ -65,7 +66,8 @@ void PlayerController::update_controls(float delta){
if (player->flight && hitbox->grounded)
player->flight = false;
// Camera shaking
/*camera shaking*/{
player->interpVel = player->interpVel * (1.0f - delta * 5) + hitbox->velocity * delta * 0.1f;
if (hitbox->grounded && player->interpVel.y < 0.0f){
player->interpVel.y *= -30.0f;
@ -77,6 +79,7 @@ void PlayerController::update_controls(float delta){
camera->position += camera->right * sin(shakeTimer) * CAMERA_SHAKING_OFFSET * player->cameraShaking;
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) ||
(Events::jpressed(GLFW_KEY_N) && player->flight == player->noclip)){
@ -89,7 +92,7 @@ void PlayerController::update_controls(float delta){
player->noclip = !player->noclip;
}
// Field of view manipulations
/*field of view manipulations*/{
float dt = min(1.0f, delta * ZOOM_SPEED);
float zoomValue = 1.0f;
if (shift){
@ -103,11 +106,11 @@ void PlayerController::update_controls(float delta){
if (zoom)
zoomValue *= C_ZOOM;
camera->zoom = zoomValue * dt + camera->zoom * (1.0f - dt);
}//end
if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){
hitbox->velocity.y = JUMP_FORCE;
}
vec3 dir(0,0,0);
if (Events::pressed(GLFW_KEY_W)){
dir.x += camera->dir.x;
@ -126,9 +129,9 @@ void PlayerController::update_controls(float delta){
dir.z -= camera->right.z;
}
hitbox->linear_damping = PLAYER_GROUNDED_DAMPING;
hitbox->linear_damping = PLAYER_GROUND_DAMPING;
if (player->flight){
hitbox->linear_damping = PLAYER_NOT_GROUNDED_DAMPING;
hitbox->linear_damping = PLAYER_AIR_DAMPING;
hitbox->velocity.y *= 1.0f - delta * 9;
if (Events::pressed(GLFW_KEY_SPACE)){
hitbox->velocity.y += speed * delta * 9;
@ -137,19 +140,16 @@ void PlayerController::update_controls(float delta){
hitbox->velocity.y -= speed * delta * 9;
}
}
if (!hitbox->grounded)
hitbox->linear_damping = PLAYER_AIR_DAMPING;
if (length(dir) > 0.0f){
dir = normalize(dir);
if (!hitbox->grounded)
hitbox->linear_damping = PLAYER_NOT_GROUNDED_DAMPING;
hitbox->velocity.x += dir.x * 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;
@ -170,8 +170,8 @@ void PlayerController::update_controls(float delta){
camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
}
}//end
}
// end camera rotate
void PlayerController::update_interaction(){
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) {
level->playerController->update_controls(delta);
if (Events::_cursor_locked)
if (Events::_cursor_locked){
level->playerController->update_interaction();
} else
{
level->playerController->selectedBlockId = -1;
}
vec3 position = level->player->hitbox->position;
level->chunks->setCenter(world->wfile, position.x, position.z);
@ -119,7 +123,6 @@ void mainloop(Level* level, Assets* assets) {
float lastTime = glfwGetTime();
float delta = 0.0f;
bool occlusion = true;
bool devdata = false;
Window::swapInterval(1);
while (!Window::isShouldClose()){
frame++;
@ -137,7 +140,7 @@ void mainloop(Level* level, Assets* assets) {
occlusion = !occlusion;
}
if (Events::jpressed(GLFW_KEY_F3)){
devdata = !devdata;
level->player->debug = !level->player->debug;
}
if (Events::jpressed(GLFW_KEY_F5)){
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++)
level->chunksController->loadVisible(world->wfile);
worldRenderer.draw(world, camera, occlusion, devdata);
hud.draw(level, assets, devdata);
if (devdata) {
worldRenderer.draw(world, camera, occlusion);
hud.draw(level, assets);
if (level->player->debug) {
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;
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();
}
if (devdata) {
if (level->player->debug) {
linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjection()*camera->getView());

View File

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