physics v1.2 (need to fix angles)

This commit is contained in:
Alexandre 2025-02-07 17:59:12 +01:00
parent 1859c47de2
commit 8e9d3fe513
7 changed files with 215 additions and 154 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -90,7 +90,7 @@ int convex_seg(int x1, int x2, double theta) {
} }
double convex_pt(double a, double b, double theta) { double convex_pt(double a, double b, double theta) {
return (a+(b-a)*theta) ; return (a+(b-a)*theta);
} }
double convex_tri(double a, double tha, double b, double thb, double c, double thc) { double convex_tri(double a, double tha, double b, double thb, double c, double thc) {
@ -119,33 +119,33 @@ bool is_an_integer(char c) {
pt_2d vect_diff(pt_2d p1, pt_2d p2) { pt_2d vect_diff(pt_2d p1, pt_2d p2) {
pt_2d res; pt_2d res;
res.x = p2.x - p1.x ; res.x = p2.x - p1.x;
res.y = p2.y - p1.y ; res.y = p2.y - p1.y;
res.z = p2.z - p1.z ; res.z = p2.z - p1.z;
return res; return res;
} }
double dot2D(pt_2d p1, pt_2d p2) { double dot2D(pt_2d p1, pt_2d p2) {
return p1.x * p2.x + p1.y * p2.y ; return p1.x * p2.x + p1.y * p2.y;
} }
double dot3D(pt_2d p1, pt_2d p2) { double dot3D(pt_2d p1, pt_2d p2) {
return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z ; return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z;
} }
double to_double(int n) { double to_double(int n) {
return (double)n ; return (double)n;
} }
int to_int(double n) { int to_int(double n) {
return (int)n ; return (int)n;
} }
int line_count(char* filename) { int line_count(char* filename) {
FILE* ptr = fopen(filename, "r"); FILE* ptr = fopen(filename, "r");
char c = 'd'; char c = 'd';
int n = 0 ; int n = 0;
while(c != EOF) { while(c != EOF) {
if(c == '\n') { if(c == '\n') {
n += 1; n += 1;
@ -157,11 +157,11 @@ int line_count(char* filename) {
} }
int str_to_int(char* s) { int str_to_int(char* s) {
int res = 0 ; int res = 0;
int i = 0 ; int i = 0;
while(s[i] != '\0' && is_an_integer(s[i])) { while(s[i] != '\0' && is_an_integer(s[i])) {
res *= 10 ; res *= 10;
res += (int)s[i] - 48 ; res += (int)s[i] - 48;
i++; i++;
}; };
return res; return res;
@ -177,57 +177,57 @@ bool str_equal(char* s1, char* s2) {
// ------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------ //
cube_0* create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) { cube_0* create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) {
cube_0* cb = malloc(sizeof(cube_0)) ; cube_0* cb = malloc(sizeof(cube_0));
cb->red = r ; cb->red = r;
cb->green = g ; cb->green = g;
cb->blue = b ; cb->blue = b;
cb->x = x ; cb->x = x;
cb->y = y ; cb->y = y;
cb->z = z ; cb->z = z;
cb->w = w ; cb->w = w;
cb->h = h ; cb->h = h;
cb->d = d ; cb->d = d;
cb->hz_angle = hz_a ; cb->hz_angle = hz_a;
cb->vt_angle = vt_a ; cb->vt_angle = vt_a;
return cb ; return cb;
} }
void fill_cube_0(cube_0* cb, double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) { void fill_cube_0(cube_0* cb, double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) {
cb->red = r ; cb->red = r;
cb->green = g ; cb->green = g;
cb->blue = b ; cb->blue = b;
cb->x = x ; cb->x = x;
cb->y = y ; cb->y = y;
cb->z = z ; cb->z = z;
cb->w = w ; cb->w = w;
cb->h = h ; cb->h = h;
cb->d = d ; cb->d = d;
cb->hz_angle = hz_a ; cb->hz_angle = hz_a;
cb->vt_angle = vt_a ; cb->vt_angle = vt_a;
} }
cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) { cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) {
cube cb = malloc(sizeof(cube_0)); cube cb = malloc(sizeof(cube_0));
cb = create_cube_0(x, y, z, w, h, d, hz_a, vt_a, r, g, b) ; cb = create_cube_0(x, y, z, w, h, d, hz_a, vt_a, r, g, b);
return cb; return cb;
} }
void free_cube(cube c) { void free_cube(cube c) {
free(c) ; free(c);
} }
teleporter* create_teleporter( teleporter* create_teleporter(
double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b, double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b,
int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest
) { ) {
teleporter* tp = malloc(sizeof(teleporter)) ; teleporter* tp = malloc(sizeof(teleporter));
tp->dest_chx = chx_dest ; tp->dest_chx = chx_dest;
tp->dest_chy = chy_dest ; tp->dest_chy = chy_dest;
tp->dest_x = x_dest ; tp->dest_x = x_dest;
tp->dest_y = y_dest ; tp->dest_y = y_dest;
tp->dest_z = z_dest ; tp->dest_z = z_dest;
tp->hitbox = create_cube_0(x, y, z, w, h, d, hz_a, vt_a, r, g, b); tp->hitbox = create_cube_0(x, y, z, w, h, d, hz_a, vt_a, r, g, b);
return tp ; return tp;
} }
void copy_cube(cube_0* src, cube_0* dest) { void copy_cube(cube_0* src, cube_0* dest) {
@ -264,91 +264,91 @@ double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, do
((ex - sx) * (ex - sx) + (ey - sy) * (ey - sy) + (ez - sz) * (ez - sz)) ((ex - sx) * (ex - sx) + (ey - sy) * (ey - sy) + (ez - sz) * (ez - sz))
); );
if(theta >= 0.0 && theta <= 1.0) { if(theta >= 0.0 && theta <= 1.0) {
return (distance_pt_pt_3d(x, y, z, convex_pt(sx, ex, theta), convex_pt(sy, ey, theta), convex_pt(sz, ez, theta))) ; return (distance_pt_pt_3d(x, y, z, convex_pt(sx, ex, theta), convex_pt(sy, ey, theta), convex_pt(sz, ez, theta)));
} else if (theta < 0.0) { } else if (theta < 0.0) {
return (distance_pt_pt_3d(x, y, z, sx, sy, sz)) ; return (distance_pt_pt_3d(x, y, z, sx, sy, sz));
} else { } else {
return (distance_pt_pt_3d(x, y, z, ex, ey, ez)) ; return (distance_pt_pt_3d(x, y, z, ex, ey, ez));
} }
} }
double distance_pt_cube_axis(double coord, double begin, double end) { double distance_pt_cube_axis(double coord, double begin, double end) {
if(coord < begin) { if(coord < begin) {
return (begin-coord) ; return (begin-coord);
} else if(coord > end) { } else if(coord > end) {
return (coord-end) ; return (coord-end);
} else { } else {
return 0.0 ; return 0.0;
} }
} }
double distance_pt_cube_axis_max(double coord, double begin, double end) { double distance_pt_cube_axis_max(double coord, double begin, double end) {
if(coord < (begin+end)/2) { if(coord < (begin+end)/2) {
return absf(end-coord) ; return absf(end-coord);
} else { } else {
return absf(begin-coord) ; return absf(begin-coord);
} }
} }
double distance_pt_cube_aligned_3d(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd) { double distance_pt_cube_aligned_3d(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd) {
return (distance_pt_cube_axis(x0, cx, cx+cw)+distance_pt_cube_axis(y0, cy, cy+ch)+distance_pt_cube_axis(z0, cz, cz+cd)) ; return (distance_pt_cube_axis(x0, cx, cx+cw)+distance_pt_cube_axis(y0, cy, cy+ch)+distance_pt_cube_axis(z0, cz, cz+cd));
} }
double distance_pt_cube_aligned_3d_max(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd) { double distance_pt_cube_aligned_3d_max(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd) {
return (distance_pt_cube_axis_max(x0, cx, cx+cw)+distance_pt_cube_axis_max(y0, cy, cy+ch)+distance_pt_cube_axis_max(z0, cz, cz+cd)) ; return (distance_pt_cube_axis_max(x0, cx, cx+cw)+distance_pt_cube_axis_max(y0, cy, cy+ch)+distance_pt_cube_axis_max(z0, cz, cz+cd));
} }
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);
double y = y0 - (c->y + c->h/2.0) ; double y = y0 - (c->y + c->h/2.0);
double z = z0 - (c->z + c->d/2.0) ; double z = z0 - (c->z + c->d/2.0);
// rotate the point : y then x // rotate the point : y then x
double xry = x*cos(c->hz_angle) + z*sin(c->hz_angle) ; double xry = x*cos(c->hz_angle) + z*sin(c->hz_angle);
double yry = y ; double yry = y;
double zry = z*cos(c->hz_angle) - x*sin(c->hz_angle) ; double zry = z*cos(c->hz_angle) - x*sin(c->hz_angle);
double xrx = xry ; double xrx = xry;
double yrx = yry*cos(c->vt_angle) - zry*sin(c->vt_angle) ; double yrx = yry*cos(c->vt_angle) - zry*sin(c->vt_angle);
double zrx = zry*cos(c->vt_angle) + yry*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 // now the cube and pt are aligned, and (0, 0, 0) is at the cube's (bary)center
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);
} }
// ------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------ //
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) {
// align pt to (0, 0, 0) // align pt to (0, 0, 0)
double x = x0 - camx ; double x = x0 - camx;
double y = y0 - camy ; double y = y0 - camy;
double z = z0 - camz ; double z = z0 - camz;
// rotate (y) // rotate (y)
double xry = x*cos(rot_hz) - z*sin(rot_hz) ; double xry = x*cos(rot_hz) - z*sin(rot_hz);
double yry = y ; double yry = y;
double zry = z*cos(rot_hz) + x*sin(rot_hz) ; double zry = z*cos(rot_hz) + x*sin(rot_hz);
// rotate (x) // rotate (x)
if(rx != NULL) {*rx = xry ;} if(rx != NULL) {*rx = xry;}
if(ry != NULL) {*ry = yry*cos(rot_vt) + zry*sin(rot_vt) ;} if(ry != NULL) {*ry = yry*cos(rot_vt) + zry*sin(rot_vt);}
if(rz != NULL) {*rz = zry*cos(rot_vt) - yry*sin(rot_vt) ;} if(rz != NULL) {*rz = zry*cos(rot_vt) - yry*sin(rot_vt);}
} }
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c) { void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c) {
// align pt to (0, 0, 0) // align pt to (0, 0, 0)
double x = x0 - (c->x + c->w/2.0) ; double x = x0 - (c->x + c->w/2.0);
double y = y0 - (c->y + c->h/2.0) ; double y = y0 - (c->y + c->h/2.0);
double z = z0 - (c->z + c->d/2.0) ; double z = z0 - (c->z + c->d/2.0);
// rotate (y) // rotate (y)
double xry = x*cos(c->hz_angle) + z*sin(c->hz_angle) ; double xry = x*cos(c->hz_angle) + z*sin(c->hz_angle);
double yry = y ; double yry = y;
double zry = z*cos(c->hz_angle) - x*sin(c->hz_angle) ; double zry = z*cos(c->hz_angle) - x*sin(c->hz_angle);
// rotate (x) // rotate (x)
if(rx != NULL) {*rx = c->x + c->w/2.0 + xry ;} 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(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(rz != NULL) {*rz = c->z + c->d/2.0 + zry*cos(c->vt_angle) + yry*sin(c->vt_angle);}
} }
@ -369,14 +369,14 @@ void add_entity(entity** arr, int* memlen, int* len, entity* ent) {
entity** newarr = malloc(sizeof(entity*)*2*(*memlen)); entity** newarr = malloc(sizeof(entity*)*2*(*memlen));
for(int k = 0; k < *len; k++) { for(int k = 0; k < *len; k++) {
newarr[k] = malloc(sizeof(entity)); newarr[k] = malloc(sizeof(entity));
newarr[k] = arr[k] ; newarr[k] = arr[k];
free(arr[k]); free(arr[k]);
} }
free(*arr); free(*arr);
arr = newarr ; arr = newarr;
*memlen *= 2; *memlen *= 2;
} }
free(arr[*len]); free(arr[*len]);
arr[*len] = ent ; arr[*len] = ent;
*len += 1; *len += 1;
} }

View File

@ -57,25 +57,25 @@ void update_entities(float dtime) {
// ------------------------------------------------------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------------------------------------------------------ //
void speen(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void speen(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) {
ret->hz_angle += ((double)dtime)*15.0; ret->hz_angle += ((double)dtime)*1.5;
} }
void speen2(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void speen2(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) {
ret->hz_angle += ((double)dtime)*22.5; ret->hz_angle += ((double)dtime)*2.5;
if((int)(5.0*ret->hz_angle) != (int)(5.0*(ret->hz_angle - ((double)dtime)*22.5))) { if((int)(5.0*ret->hz_angle) != (int)(5.0*(ret->hz_angle - ((double)dtime)*22.5))) {
double dx = (x+w/2 - camx); double dx = (x+w/2 - camx);
double dy = (y+h/2 - camy); double dy = (y+h/2 - camy);
double dz = (z+d/2 - camz); double dz = (z+d/2 - camz);
double total = sqrt(dx*dx + dy*dy + dz*dz); double total = sqrt(dx*dx + dy*dy + dz*dz);
dx = 170.0*dx/total; dx = 17.0*dx/total;
dy = 170.0*dy/total; dy = 17.0*dy/total;
dz = 170.0*dz/total; dz = 17.0*dz/total;
appendProj(x+w/2, y+h/2, z+d/2, 0.1, 0.1, 0.1, -dx, -dy, -dz, 0.0, 0.0, 0.0, 255, 0, 0, 10, 3.0); appendProj(x+w/2, y+h/2, z+d/2, 0.1, 0.1, 0.1, -dx, -dy, -dz, 0.0, 0.0, 0.0, 255, 0, 0, 10, 3.0);
} }
} }
void speen3(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void speen3(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) {
ret->vt_angle += ((double)dtime)*22.5; ret->vt_angle += ((double)dtime)*2.5;
} }
// metad{1,2,3} = og pos // metad{1,2,3} = og pos
@ -105,31 +105,12 @@ void detectHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
} }
void translatePlayer(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) { void translatePlayer(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
double vx = ent->metad4*(cos((double)(ent->metai1*(sim_time+(double)dtime)/ent->metai2 + ent->metai3*3.14159/180.0))-cos((double)(ent->metai1*sim_time/ent->metai2 + ent->metai3*3.14159/180.0))); double dx = ent->metad4*(cos((double)(ent->metai1*(sim_time+(double)dtime)/ent->metai2 + ent->metai3*3.14159/180.0))-cos((double)(ent->metai1*sim_time/ent->metai2 + ent->metai3*3.14159/180.0)));
double vy = ent->metad5*(cos((double)(ent->metai1*(sim_time+(double)dtime)/ent->metai2 + ent->metai3*3.14159/180.0))-cos((double)(ent->metai1*sim_time/ent->metai2 + ent->metai3*3.14159/180.0))); double dy = ent->metad5*(cos((double)(ent->metai1*(sim_time+(double)dtime)/ent->metai2 + ent->metai3*3.14159/180.0))-cos((double)(ent->metai1*sim_time/ent->metai2 + ent->metai3*3.14159/180.0)));
double vz = ent->metad6*(cos((double)(ent->metai1*(sim_time+(double)dtime)/ent->metai2 + ent->metai3*3.14159/180.0))-cos((double)(ent->metai1*sim_time/ent->metai2 + ent->metai3*3.14159/180.0))); double dz = ent->metad6*(cos((double)(ent->metai1*(sim_time+(double)dtime)/ent->metai2 + ent->metai3*3.14159/180.0))-cos((double)(ent->metai1*sim_time/ent->metai2 + ent->metai3*3.14159/180.0)));
//printf("(%lf %lf) (%lf %lf) (%lf %lf)\n", vx, camvx, vy, camvy, vz, camvz); fx += dx/(dtime*dtime);
if(true) { fy += dy/(dtime*dtime);
double oldvx = camvx; fz += dz/(dtime*dtime);
camvx = vx/dtime;
camx -= oldvx*dtime;
camx += camvx*dtime;
noResetSpeed = true;
}
if(true) {
double oldvy = camvy;
camvy += vy/dtime;
camy -= oldvy*dtime;
camy += camvy*dtime;
}
if(true) {
double oldvz = camvz;
camvz = vz/dtime;
camz -= oldvz*dtime;
camz += camvz*dtime;
noResetSpeed = true;
}
noFriction = true;
} }
void go_to_player(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void go_to_player(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) {

View File

@ -24,10 +24,13 @@ double vtmult = 1.5;
double min_dist = 0.7; double min_dist = 0.7;
double friction = 0.8; double friction = 0.8;
double gravity_factor = 9.8; double gravity_factor = 9.8;
bool noResetSpeed = false;
bool noFriction = false;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
double fx;
double fy;
double fz;
bool updateForces = false;
int player_hp; int player_hp;
bool stop_evetything; bool stop_evetything;
@ -48,6 +51,7 @@ int draw_type;
int fade_dmg; int fade_dmg;
bool has_changed; bool has_changed;
int collisionVal = 0;
double room_width; double room_width;
double room_depth; double room_depth;
@ -83,16 +87,94 @@ void set_player_coords(int old_chx, int old_chy) {
} }
} }
pt_2d surface[3];
pt_2d directors[2];
pt_2d normal;
double p1, p2, p3;
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 + cb->d*(sf==4), &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 + cb->d*(sf==4), &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 + cb->d*(sf==4), &p1, &p2, &p3, cb);
surface[2] = (pt_2d){.x = p1, .y = p2, .z = p3};
}
}
void getDirectors() {
directors[0] = (pt_2d){.x = surface[1].x - surface[0].x, .y = surface[1].y - surface[0].y, .z = surface[1].z - surface[0].z};
directors[1] = (pt_2d){.x = surface[2].x - surface[0].x, .y = surface[2].y - surface[0].y, .z = surface[2].z - surface[0].z};
}
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.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;
normal.y /= norm;
normal.z /= norm;
}
void updateF(cube_0* cb, double dtime) {
for(int d = 0; d < 6; d++) {
getSF(cb, d);
getDirectors();
getNormal();
if(d%2==1) {
normal.x *= -1.0;
normal.y *= -1.0;
normal.z *= -1.0;
}
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};
if((dot3D(vt, normal) <= -min_dist && dot3D(vtdt, normal) > -min_dist) || (dot3D(vt, normal) >= min_dist && dot3D(vtdt, normal) < min_dist)) {
double normv = sqrt(camvx*camvx + camvy*camvy + camvz*camvz);
double alpha = acos(dot3D(normal, (pt_2d){.x = camvx, .y = camvy, .z = camvz})/normv);
double beta = 3.1415926535 - 2*alpha;
double nu = sqrt(2)*normv*sqrt(1-cos(beta));
pt_2d u = (pt_2d){.x = normal.x*nu, .y = normal.y*nu, .z = normal.z*nu};
camvx += u.x;
camvy += u.y;
camvz += u.z;
}
}
}
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(camx, camy, camz, current_room->map[k]);
if(dist <= min_dist) { if(dist <= min_dist) {
if(updateForces) {
updateF(current_room->map[k], (double)dtime);
}
return true; return true;
} }
} }
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(camx, camy, camz, current_room->tps[k]->hitbox);
if(dist <= min_dist) { if(dist <= min_dist) {
if(updateForces) {
updateF(current_room->tps[k]->hitbox, (double)dtime);
}
int old_chx = player_chx; int old_chx = player_chx;
int old_chy = player_chy; int old_chy = player_chy;
player_chx = current_room->tps[k]->dest_chx; player_chx = current_room->tps[k]->dest_chx;
@ -106,6 +188,9 @@ 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(camx, camy, camz, current_room->ents[k]->pos);
if(dist <= min_dist) { if(dist <= min_dist) {
if(updateForces) {
updateF(current_room->ents[k]->pos, (double)dtime);
}
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)));
if(*(current_room->ents[k]->hitpoints) <= 0) { if(*(current_room->ents[k]->hitpoints) <= 0) {
@ -122,45 +207,37 @@ bool is_colliding(float dtime) {
} }
void movePlayerG(double dtime) { void movePlayerG(double dtime) {
camx += camvx*dtime; updateForces = true;
noResetSpeed = false; fx = 0.0;
noFriction = false; fy = 0.0;
if(is_colliding(dtime)) { fz = 0.0;
camx -= camvx*dtime;
if(!noResetSpeed) {
camvx = 0.0;
}
}
if(!noFriction) {
camvx = camvx*(1-dtime*friction);
}
camvy -= gravity_factor*dtime; camvy -= gravity_factor*dtime;
camy += camvy*dtime;
noResetSpeed = false;
noFriction = false;
if(is_colliding(dtime)) {
camy -= camvy*dtime;
if(!noResetSpeed) {
camvy = 0.0;
}
}
if(!noFriction) {
camvy = camvy*(1-dtime*friction);
}
camz += camvz*dtime; double delx = camvx*dtime;
noResetSpeed = false; double dely = camvy*dtime;
noFriction = false; double delz = camvz*dtime;
camx += delx;
camy += dely;
camz += delz;
if(is_colliding(dtime)) { if(is_colliding(dtime)) {
camz -= camvz*dtime; //camvx = 0.0;
if(!noResetSpeed) { //camvy = 0.0;
camvz = 0.0; //camvz = 0.0;
}
}
if(!noFriction) {
camvz = camvz*(1-dtime*friction);
} }
camx -= delx;
camy -= dely;
camz -= delz;
printf("%lf | %lf | %lf\n", fx, fy, fz);
updateForces = false;
camvx += fx*dtime;
camvy += fy*dtime;
camvz += fz*dtime;
printf("%lf | %lf | %lf\n\n", camvx, camvy, camvz);
camx += camvx*dtime;
camy += camvy*dtime;
camz += camvz*dtime;
} }
void teleport_on_edge() { void teleport_on_edge() {

View File

@ -147,7 +147,10 @@ extern float rectDefault[18];
extern int triCount; extern int triCount;
extern bool noResetSpeed; extern bool updateForces;
extern bool noFriction;
extern double fx;
extern double fy;
extern double fz;
#endif #endif