switched norm to ||.||infinite that fixed collisions (yay)

This commit is contained in:
Alexandre 2025-02-09 12:34:21 +01:00
parent cf9e278068
commit 30717e9c1f
19 changed files with 91 additions and 32 deletions

View File

@ -1,5 +1,5 @@
CC = gcc CC = gcc
FLAGS = -O2 -Wall -Wextra -g FLAGS = -Wall -Wextra -g
LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm
all: bin/back all: bin/back

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -289,6 +289,13 @@ double distance_pt_cube_aligned_3d(double x0, double y0, double z0, double cx, d
return sqrt((x0-clx)*(x0-clx) + (y0-cly)*(y0-cly) + (z0-clz)*(z0-clz)); return sqrt((x0-clx)*(x0-clx) + (y0-cly)*(y0-cly) + (z0-clz)*(z0-clz));
} }
double distance_pt_cube_aligned_3d_infinite(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd) {
double clx = distance_pt_cube_axis(x0, cx, cx+cw);
double cly = distance_pt_cube_axis(y0, cy, cy+ch);
double clz = distance_pt_cube_axis(z0, cz, cz+cd);
return maxd(maxd(absf(clx-x0), absf(cly-y0)), absf(clz-z0));
}
double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0* c) { double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0* c) {
// places the origin at the center of the cube // places the origin at the center of the cube
double x = x0 - (c->x + c->w/2.0); double x = x0 - (c->x + c->w/2.0);
@ -308,6 +315,25 @@ double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0* c) {
return distance_pt_cube_aligned_3d(xrx, yrx, zrx, -c->w/2.0, -c->h/2.0, -c->d/2.0, c->w, c->h, c->d); return distance_pt_cube_aligned_3d(xrx, yrx, zrx, -c->w/2.0, -c->h/2.0, -c->d/2.0, c->w, c->h, c->d);
} }
double distance_pt_cube_0_3d_infinite(double x0, double y0, double z0, cube_0* c) {
// places the origin at the center of the cube
double x = x0 - (c->x + c->w/2.0);
double y = y0 - (c->y + c->h/2.0);
double z = z0 - (c->z + c->d/2.0);
// rotate the point : y then x
double xry = x*cos(c->hz_angle) + z*sin(c->hz_angle);
double yry = y;
double zry = z*cos(c->hz_angle) - x*sin(c->hz_angle);
double xrx = xry;
double yrx = yry*cos(c->vt_angle) - zry*sin(c->vt_angle);
double zrx = zry*cos(c->vt_angle) + yry*sin(c->vt_angle);
// now the cube and pt are aligned, and (0, 0, 0) is at the cube's (bary)center
return distance_pt_cube_aligned_3d_infinite(xrx, yrx, zrx, -c->w/2.0, -c->h/2.0, -c->d/2.0, c->w, c->h, c->d);
}
// ------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------ //
void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz) { void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz) {

View File

@ -49,6 +49,7 @@ double distance_pt_pt_2d_sq(double x0, double y0, double x1, double y1);
double distance_pt_pt_3d_sq(double x0, double y0, double z0, double x1, double y1, double z1); double distance_pt_pt_3d_sq(double x0, double y0, double z0, double x1, double y1, double z1);
double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez); double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez);
double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0* c); double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0* c);
double distance_pt_cube_0_3d_infinite(double x0, double y0, double z0, cube_0* c);
void remove_entity(entity** arr, int* memlen, int* len, int index); void remove_entity(entity** arr, int* memlen, int* len, int index);
void add_entity(entity** arr, int* memlen, int* len, entity* ent); void add_entity(entity** arr, int* memlen, int* len, entity* ent);

View File

@ -150,9 +150,9 @@ void build_starting_chunk(int chx, int chy) {
new->tps_size = 4; new->tps_size = 4;
new->map[0] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255); new->map[0] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255);
new->map[1] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255); new->map[1] = create_cube_0(0.0, 15.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255);
new->map[2] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255); new->map[2] = create_cube_0(0.0, 30.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255);
new->map[3] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255); new->map[3] = create_cube_0(0.0, 45.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255);
new->map[4] = create_cube_0(0.0, 1.0, 0.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128); new->map[4] = create_cube_0(0.0, 1.0, 0.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
new->map[5] = create_cube_0(4.0, 1.0, 0.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128); new->map[5] = create_cube_0(4.0, 1.0, 0.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
new->map[6] = create_cube_0(0.0, 1.0, 4.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128); new->map[6] = create_cube_0(0.0, 1.0, 4.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);

View File

@ -22,6 +22,7 @@
double sim_time; double sim_time;
int triCount; int triCount;
unsigned int fffff;
double jPress = false; double jPress = false;
void processInput(GLFWwindow *window, float dtime) { void processInput(GLFWwindow *window, float dtime) {
@ -316,8 +317,9 @@ int main_alt() {
// ---------------------------------------------------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------------------------------------------------- //
fffff = shaderProgram;
int fps = 60; int fps = 90;
int interval = 1000000/fps; int interval = 1000000/fps;
double slp_time = 1.0/fps; double slp_time = 1.0/fps;

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
#include <stdbool.h> #include <stdbool.h>
@ -178,11 +179,13 @@ void gl_drawChar(unsigned int fragShader, char ch, float x, float y, float size,
gl_drawRect(fragShader, x-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size/2+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-size-width, size/2+2*width, 2*width, r, g, b);
} else if(ch == 'k' || ch == 'K') { } else if(ch == 'k' || ch == 'K') {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-width, size/2+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x-width, y-size/2-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x+size/2-width, y+size/2-width, 2*width, size/2+2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, size/2+2*width, r, g, b);
gl_drawRect(fragShader, x-width, y+size/2-width, size/2+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-width, y-size/2-width, size/2+2*width, 2*width, r, g, b);
} else if(ch == 'l' || ch == 'L') { } else if(ch == 'l' || ch == 'L') {
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
@ -206,11 +209,11 @@ void gl_drawChar(unsigned int fragShader, char ch, float x, float y, float size,
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-width, 2*width, size+2*width, r, g, b); gl_drawRect(fragShader, x+size/2-width, y-width, 2*width, size+2*width, r, g, b);
} else if(ch == 'q' || ch == 'Q') { } else if(ch == 'q' || ch == 'Q') {
gl_drawRect(fragShader, x-size/2-width, y-size/2-width, 2*width, 3*size/2+2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size/2-width, 2*width, 3*size/2+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size/2-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x+size/4-width, y-size-width, 2*width, size/2+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(ch == 'r' || ch == 'R') { } else if(ch == 'r' || ch == 'R') {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
@ -231,22 +234,26 @@ void gl_drawChar(unsigned int fragShader, char ch, float x, float y, float size,
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(ch == 'v' || ch == 'V') { } else if(ch == 'v' || ch == 'V') {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x+size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x+size/4-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x-size/4-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-width, size/4+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/4-width, y-width, size/4+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/4-width, y-size-width, size/2+2*width, 2*width, r, g, b);
} else if(ch == 'w' || ch == 'W') { } else if(ch == 'w' || ch == 'W') {
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-width, y-size-width, 2*width, size+2*width, r, g, b); gl_drawRect(fragShader, x-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
} else if(ch == 'x' || ch == 'X') { } else if(ch == 'x' || ch == 'X') {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-width, y-size/2-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x+size/2-width, y+size/2-width, 2*width, size/2+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, size/2+2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y+size/2-width, 2*width, size/2+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b); gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, size/2+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y+size/2-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size/2-width, size+2*width, 2*width, r, g, b);
} else if(ch == 'y' || ch == 'Y') { } else if(ch == 'y' || ch == 'Y') {
gl_drawRect(fragShader, x+size/2-width, y-width, 2*width, size+2*width, r, g, b); gl_drawRect(fragShader, x+size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-width, y-size-width, 2*width, size+2*width, r, g, b); gl_drawRect(fragShader, x-width, y-size-width, 2*width, size+2*width, r, g, b);
@ -287,6 +294,10 @@ void gl_drawString(unsigned int fragShader, char* str, float x, float y, float s
} }
} }
void gl_printf(unsigned int fragShader, int count, ...) {
}
void gl_initDrawRect(unsigned int shaderProgram) { void gl_initDrawRect(unsigned int shaderProgram) {
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
} }

View File

@ -21,11 +21,14 @@ double fov = 90.0;
double creative_speed = 0.3; double creative_speed = 0.3;
double speed = 8.0; double speed = 8.0;
double vtmult = 1.5; double vtmult = 1.5;
double min_dist = 0.1; double min_dist = 0.4;
double friction = 0.3; double friction = 0.3;
double gravity_factor = 9.8; double gravity_factor = 9.8;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
bool is_clipping = false;
int clip_dps = 500;
int njumps; int njumps;
double fx; double fx;
double fy; double fy;
@ -145,11 +148,19 @@ void normalize(pt_2d* p) {
void updateF(cube_0* cb, double dtime) { void updateF(cube_0* cb, double dtime) {
for(int d = 0; d < 6; d++) { for(int d = 0; d < 6; d++) {
cb->x -= min_dist; cb->y -= min_dist; cb->z -= min_dist; cb->x -= min_dist;
cb->w += 2*min_dist; cb->h += 2*min_dist; cb->d += 2*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); getSF(cb, d);
cb->x += min_dist; cb->y += min_dist; cb->z += min_dist; cb->x += min_dist;
cb->w -= 2*min_dist; cb->h -= 2*min_dist; cb->d -= 2*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;
getDirectors(); getDirectors();
getNormal(); getNormal();
if(d%2==1) { if(d%2==1) {
@ -165,7 +176,7 @@ void updateF(cube_0* cb, double dtime) {
(dot3D(vt, normal) <= 0.0 && dot3D(vtdt, normal) >= 0.0) || (dot3D(vt, normal) <= 0.0 && dot3D(vtdt, normal) >= 0.0) ||
(dot3D(vt, normal) >= 0.0 && dot3D(vtdt, normal) <= 0.0) (dot3D(vt, normal) >= 0.0 && dot3D(vtdt, normal) <= 0.0)
) { ) {
//printf("%d\n", d); printf("%d\n", d);
double normv = sqrt(camvx*camvx + camvy*camvy + camvz*camvz); double normv = sqrt(camvx*camvx + camvy*camvy + camvz*camvz);
double alpha = acos(dot3D(normal, (pt_2d){.x = camvx, .y = camvy, .z = camvz})/normv); double alpha = acos(dot3D(normal, (pt_2d){.x = camvx, .y = camvy, .z = camvz})/normv);
@ -181,13 +192,15 @@ void updateF(cube_0* cb, double dtime) {
camvx /= 1.41; camvx /= 1.41;
camvy /= 1.41; camvy /= 1.41;
camvz /= 1.41; camvz /= 1.41;
is_clipping = false;
} }
} }
} }
bool is_colliding(float dtime) { bool is_colliding(float dtime) {
for(int k = 0; k < current_room->map_size; k++) { for(int k = 0; k < current_room->map_size; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]); double dist = distance_pt_cube_0_3d_infinite(camx, camy, camz, current_room->map[k]);
if(dist <= min_dist) { if(dist <= min_dist) {
if(updateForces) { if(updateForces) {
updateF(current_room->map[k], (double)dtime); updateF(current_room->map[k], (double)dtime);
@ -196,7 +209,7 @@ bool is_colliding(float dtime) {
} }
} }
for(int k = 0; k < current_room->tps_size; k++) { for(int k = 0; k < current_room->tps_size; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->tps[k]->hitbox); double dist = distance_pt_cube_0_3d_infinite(camx, camy, camz, current_room->tps[k]->hitbox);
if(dist <= min_dist) { if(dist <= min_dist) {
if(updateForces) { if(updateForces) {
updateF(current_room->tps[k]->hitbox, (double)dtime); updateF(current_room->tps[k]->hitbox, (double)dtime);
@ -212,7 +225,7 @@ bool is_colliding(float dtime) {
} }
} }
for(int k = 0; k < current_room->ent_len; k++) { for(int k = 0; k < current_room->ent_len; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->ents[k]->pos); double dist = distance_pt_cube_0_3d_infinite(camx, camy, camz, current_room->ents[k]->pos);
if(dist <= min_dist) { if(dist <= min_dist) {
if(current_room->ents[k]->onHit != NULL) { if(current_room->ents[k]->onHit != NULL) {
(*current_room->ents[k]->onHit)(dtime, current_room->ents[k]->hitpoints, &current_room->ents[k]->damage, current_room->ents[k], &(*(current_room->ents[k]->pos))); (*current_room->ents[k]->onHit)(dtime, current_room->ents[k]->hitpoints, &current_room->ents[k]->damage, current_room->ents[k], &(*(current_room->ents[k]->pos)));
@ -248,7 +261,11 @@ void movePlayerG(float dtime) {
camx += delx; camx += delx;
camy += dely; camy += dely;
camz += delz; camz += delz;
is_clipping = true;
if(is_colliding(dtime)) { if(is_colliding(dtime)) {
if(is_clipping) {
player_hp -= (dtime)*clip_dps;
}
//printf("HIT\n"); //printf("HIT\n");
//printf("[%lf, %lf, %lf]\n{%lf, %lf, %lf}\n\n", fx, fy, fz, camvx, camvy, camvz); //printf("[%lf, %lf, %lf]\n{%lf, %lf, %lf}\n\n", fx, fy, fz, camvx, camvy, camvz);
} }

View File

@ -155,4 +155,6 @@ extern double fz;
extern int njumps; extern int njumps;
extern unsigned int fffff;
#endif #endif