fixed bug when vertically-angled cubes resulted in incorrect normal vector calculation

This commit is contained in:
Alexandre 2025-03-01 19:33:37 +01:00
parent 162ea1bd30
commit f8f90f7824
12 changed files with 71 additions and 23 deletions

BIN
bin/back

Binary file not shown.

View File

@ -1,5 +1,10 @@
Blocks :
[-16.0, -1.0, -16.0,32.0, 1.0, 32.0, 0.0, 0.0, 128, 128, 128]
[ -4.0,-10.0, -4.0, 8.0, 2.0, 8.0, 0.0, 0.0, 128, 128, 128]
[ -4.0, -6.5, -2.0, 2.0, 1.0, 2.0, 0.0, 0.3, 128, 128, 128]
[ -4.0, -6.5, 2.0, 2.0, 1.0, 2.0, 0.3, 0.0, 128, 128, 128]
[ -6.0, 10.0, 0.0, 6.0, 2.0, 2.0, 0.0, 0.0, 32, 192, 32] // cp1
[-25.0, 21.0, 18.0, 2.0, 2.0, 2.0, 0.0, 0.0, 192, 192, 192]

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -398,8 +398,8 @@ void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, do
// rotate (x)
if(rx != NULL) {*rx = c->x + c->w/2.0 + xry;}
if(ry != NULL) {*ry = c->y + c->h/2.0 + yry*cos(c->vt_angle) - zry*sin(c->vt_angle);}
if(rz != NULL) {*rz = c->z + c->d/2.0 + zry*cos(c->vt_angle) + yry*sin(c->vt_angle);}
if(ry != NULL) {*ry = c->y + c->h/2.0 + yry*cos(c->vt_angle) + zry*sin(c->vt_angle);}
if(rz != NULL) {*rz = c->z + c->d/2.0 + zry*cos(c->vt_angle) - yry*sin(c->vt_angle);}
}
// ------------------------------------------------------------------------------------------------ //

View File

@ -136,8 +136,8 @@ void gl_renderCube(cube_0* c, double offx, double offy, double offz) {
scale[1][1] = (float)(c->h);
scale[2][2] = (float)(c->d);
glm_translate(model, (vec3){(float)(c->x+c->w/2.0+offx), (float)(c->y+c->h/2.0+offy), (float)(c->z+c->d/2.0+offz)});
glm_rotate(model, (float)(c->vt_angle), (vec3){1.0f, 0.0f, 0.0f});
glm_rotate(model, (float)(c->hz_angle), (vec3){0.0f, 1.0f, 0.0f});
glm_rotate(model, (float)(-c->vt_angle), (vec3){1.0f, 0.0f, 0.0f});
glm_rotate(model, (float)(-c->hz_angle), (vec3){0.0f, 1.0f, 0.0f});
glm_mat2_identity(texShift);
texShift[0][0] = (float)(maxd(maxd(c->w, c->h), c->d)/mind(mind(c->w, c->h), c->d));
@ -194,6 +194,7 @@ void gl_renderAll(room* rtd, double offx, double offy, double offz) {
for(int k = 0; k < rtd->map_size; k++) {
if(is_visible(rtd->map[k], offx, offy, offz)) {
gl_renderCube(rtd->map[k], offx, offy, offz);
//debugMove(rtd->map[k]);
}
}
glBindTexture(GL_TEXTURE_2D, textures[6]);
@ -210,6 +211,7 @@ void gl_renderAll(room* rtd, double offx, double offy, double offz) {
glBindTexture(GL_TEXTURE_2D, textures[rtd->ents[k]->tex2]);
}
gl_renderCube(rtd->ents[k]->pos, offx, offy, offz);
//debugMove(rtd->ents[k]->pos);
}
}
}

View File

@ -690,10 +690,6 @@ int main_alt() {
usleep(max(0, interval-(int)(1000000*calc_T)));
sim_time += interval_s + maxd(0.0, (double)(interval_s-(calc_T)));
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(window);
glfwPollEvents();
dmgCD = maxf(dmgCD - delta, 0.0f);
if(lastDmg != player_hp) {
lastDmg = player_hp;
@ -706,6 +702,10 @@ int main_alt() {
}
incr = 0.0f;
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(window);
glfwPollEvents();
count++;
}

View File

@ -7,12 +7,16 @@
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include "hash.h"
#include "base.h"
#include "entities.h"
#include "proj.h"
#include "move.h"
#include "display.h"
#include "music.h"
// ---------------------------------------------------------------------------------------------------- //
@ -149,22 +153,28 @@ void getSF(cube_0* cb, int sf) {
if(sf == 0 || sf == 1) {
project_to_cube(cb->x + (sf==0)*(cb->w), cb->y , cb->z , &p1, &p2, &p3, cb);
surface[0] = (pt_2d){.x = p1, .y = p2, .z = p3};
project_to_cube(cb->x + (sf==0)*(cb->w), cb->y + cb->h, cb->z , &p1, &p2, &p3, cb);
surface[1] = (pt_2d){.x = p1, .y = p2, .z = p3};
project_to_cube(cb->x + (sf==0)*(cb->w), cb->y , cb->z + cb->d, &p1, &p2, &p3, cb);
surface[2] = (pt_2d){.x = p1, .y = p2, .z = p3};
} else if(sf == 2 || sf == 3) {
project_to_cube(cb->x , cb->y + (sf==2)*(cb->h), cb->z , &p1, &p2, &p3, cb);
surface[0] = (pt_2d){.x = p1, .y = p2, .z = p3};
project_to_cube(cb->x + cb->w, cb->y + (sf==2)*(cb->h), cb->z , &p1, &p2, &p3, cb);
surface[1] = (pt_2d){.x = p1, .y = p2, .z = p3};
project_to_cube(cb->x , cb->y + (sf==2)*(cb->h), cb->z + cb->d, &p1, &p2, &p3, cb);
surface[2] = (pt_2d){.x = p1, .y = p2, .z = p3};
} else {
project_to_cube(cb->x , cb->y , cb->z + (sf==4)*(cb->d), &p1, &p2, &p3, cb);
surface[0] = (pt_2d){.x = p1, .y = p2, .z = p3};
project_to_cube(cb->x + cb->w, cb->y , cb->z + (sf==4)*(cb->d), &p1, &p2, &p3, cb);
surface[1] = (pt_2d){.x = p1, .y = p2, .z = p3};
project_to_cube(cb->x , cb->y + cb->h, cb->z + (sf==4)*(cb->d), &p1, &p2, &p3, cb);
surface[2] = (pt_2d){.x = p1, .y = p2, .z = p3};
}
@ -177,7 +187,7 @@ void getDirectors() {
void getNormal() {
normal.x = directors[0].y*directors[1].z - directors[1].y*directors[0].z;
normal.y = -directors[0].z*directors[1].x + directors[1].z*directors[0].x;
normal.y = directors[0].z*directors[1].x - directors[1].z*directors[0].x;
normal.z = directors[0].x*directors[1].y - directors[1].x*directors[0].y;
double norm = sqrt(normal.x*normal.x + normal.y*normal.y + normal.z*normal.z);
normal.x /= norm;
@ -192,6 +202,31 @@ void normalize(pt_2d* p) {
p->z /= norm;
}
void debugMove(cube_0* cb) {
for(int d = 0; d < 6; d++) {
cb->x -= min_dist;
cb->y -= min_dist;
cb->z -= min_dist;
cb->w += 2*min_dist;
cb->h += 2*min_dist;
cb->d += 2*min_dist;
getSF(cb, d);
cb->x += min_dist;
cb->y += min_dist;
cb->z += min_dist;
cb->w -= 2*min_dist;
cb->h -= 2*min_dist;
cb->d -= 2*min_dist;
for(int k = 0; k < 3; k++) {
cube_0* cb2 = create_cube_0(surface[k].x-0.1, surface[k].y-0.1, surface[k].z-0.1, 0.2, 0.2, 0.2, 0.0, 0.0, 255, 255, 255);
gl_renderCube(cb2, 0.0, 0.0, 0.0);
free(cb2);
}
}
}
void updateF(cube_0* cb, double dtime) {
for(int d = 0; d < 6; d++) {
cb->x -= min_dist;
@ -209,11 +244,12 @@ void updateF(cube_0* cb, double dtime) {
cb->d -= 2*min_dist;
getDirectors();
getNormal();
if(d%2==1) {
if(d==2 || d==5 || d==1) {
normal.x *= -1.0;
normal.y *= -1.0;
normal.z *= -1.0;
}
//printf("%lf %lf\n", dot3D(normal, directors[0]), dot3D(normal, directors[1]));
pt_2d vt = (pt_2d){.x = camx-camvx*dtime - surface[0].x, .y = camy-camvy*dtime - surface[0].y, .z = camz-camvz*dtime - surface[0].z};
pt_2d vtdt = (pt_2d){.x = camx - surface[0].x, .y = camy - surface[0].y, .z = camz - surface[0].z};
normalize(&vtdt);
@ -252,6 +288,9 @@ bool is_colliding(float dtime) {
if(vstd != NULL) {
for(int k = 0; k < vstd->map_size; k++) {
double dist = distance_pt_cube_0_3d_infinite(camx-2*room_width*w, camy, camz-2*room_depth*h, vstd->map[k]);
//if(vstd->map[k]->vt_angle != 0.0) {
// printf("(%lf)\n", dist);
//}
if(dist <= min_dist) {
if(updateForces) {
updateF(vstd->map[k], (double)dtime);
@ -342,7 +381,7 @@ void movePlayerG(float dtime) {
is_clipping = true;
if(is_colliding(dtime)) {
if(is_clipping) {
player_hp -= (dtime)*clip_dps;
//player_hp -= (dtime)*clip_dps;
}
//printf("HIT\n");
//printf("[%lf, %lf, %lf]\n{%lf, %lf, %lf}\n\n", fx, fy, fz, camvx, camvy, camvz);

View File

@ -6,6 +6,8 @@ bool is_colliding(float dtime);
void update_buttons(float dtime);
void debugMove(cube_0* cb);
void teleport_on_edge();
void movePlayerG(float dtime);