Added flight mode ('F' button)

This commit is contained in:
MihailRis 2022-03-04 17:44:14 +03:00 committed by GitHub
parent 1dd9a72fb3
commit b6c328fe1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 9 deletions

View File

@ -57,6 +57,7 @@ void setup_definitions() {
block->lightPassing = true;
block->skyLightPassing = true;
block->obstacle = false;
block->selectable = false;
Block::blocks[block->id] = block;
// STONE
@ -106,6 +107,7 @@ void setup_definitions() {
block->lightPassing = true;
block->skyLightPassing = false;
block->obstacle = false;
block->selectable = false;
Block::blocks[block->id] = block;
}
#endif // DECLARATIONS_H

View File

@ -11,6 +11,7 @@ public:
float speed;
Camera* camera;
Hitbox* hitbox;
bool flight = false;
int choosenBlock;
float camX, camY;
float cameraShaking = 0.0f;

View File

@ -10,16 +10,16 @@
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, float gravityScale) {
for (unsigned i = 0; i < substeps; i++){
float dt = delta / (float)substeps;
float linear_damping = hitbox->linear_damping;
vec3& pos = hitbox->position;
vec3& half = hitbox->halfsize;
vec3& vel = hitbox->velocity;
vel.x += gravity.x*dt;
vel.y += gravity.y*dt;
vel.z += gravity.z*dt;
vel.x += gravity.x*dt * gravityScale;
vel.y += gravity.y*dt * gravityScale;
vel.z += gravity.z*dt * gravityScale;
float px = pos.x;
float pz = pos.z;

View File

@ -14,7 +14,7 @@ class PhysicsSolver {
vec3 gravity;
public:
PhysicsSolver(vec3 gravity);
void step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned substeps, bool shifting);
void step(Chunks* chunks, Hitbox* hitbox, float delta, unsigned substeps, bool shifting, float gravityScale);
bool isBlockInside(int x, int y, int z, Hitbox* hitbox);
};

View File

@ -78,6 +78,7 @@ void close_world(WorldFiles* wfile, Chunks* chunks){
#define CAMERA_SHAKING_OFFSET_Y 0.031f
#define CAMERA_SHAKING_SPEED 1.6f
#define CAMERA_SHAKING_DELTA_K 10.0f
#define FLIGHT_SPEED_MUL 5.0f
void update_controls(PhysicsSolver* physics,
Chunks* chunks,
@ -105,9 +106,12 @@ void update_controls(PhysicsSolver* physics,
bool zoom = Events::pressed(GLFW_KEY_C);
float speed = player->speed;
if (player->flight){
speed *= FLIGHT_SPEED_MUL;
}
int substeps = (int)(delta * 1000);
substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps));
physics->step(chunks, hitbox, delta, substeps, shift);
physics->step(chunks, hitbox, delta, substeps, shift, player->flight ? 0.0f : 1.0f);
camera->position.x = hitbox->position.x;
camera->position.y = hitbox->position.y + 0.5f;
camera->position.z = hitbox->position.z;
@ -123,7 +127,11 @@ void update_controls(PhysicsSolver* physics,
player->cameraShaking = player->cameraShaking * (1.0f - delta * CAMERA_SHAKING_DELTA_K) + factor * delta * CAMERA_SHAKING_DELTA_K;
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 -= player->interpVel * 0.05f;
camera->position -= min(player->interpVel * 0.05f, 1.0f);
if (Events::jpressed(GLFW_KEY_F)){
player->flight = !player->flight;
}
// Field of view manipulations
float dt = min(1.0f, delta * ZOOM_SPEED);
@ -140,7 +148,6 @@ void update_controls(PhysicsSolver* physics,
}
if (zoom)
zoomValue *= C_ZOOM;
camera->zoom = zoomValue * dt + camera->zoom * (1.0f - dt);
if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){
@ -166,6 +173,16 @@ void update_controls(PhysicsSolver* physics,
}
hitbox->linear_damping = DEFAULT_AIR_DAMPING;
if (player->flight){
hitbox->linear_damping = PLAYER_NOT_ONGROUND_DAMPING;
hitbox->velocity.y *= 1.0f - delta * 9;
if (Events::pressed(GLFW_KEY_SPACE)){
hitbox->velocity.y += speed * delta * 9;
}
if (Events::pressed(GLFW_KEY_LEFT_SHIFT)){
hitbox->velocity.y -= speed * delta * 9;
}
}
if (length(dir) > 0.0f){
dir = normalize(dir);

View File

@ -13,6 +13,7 @@ public:
bool lightPassing = false;
bool skyLightPassing = false;
bool obstacle = true;
bool selectable = true;
Block(unsigned int id, int texture);
};

View File

@ -192,7 +192,7 @@ voxel* Chunks::rayCast(vec3 a, vec3 dir, float maxDist, vec3& end, vec3& norm, v
while (t <= maxDist){
voxel* voxel = get(ix, iy, iz);
if (voxel == nullptr || voxel->id){
if (voxel == nullptr || Block::blocks[voxel->id]->selectable){
end.x = px + t * dx;
end.y = py + t * dy;
end.z = pz + t * dz;