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

@ -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) {
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) {
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))) {
double dx = (x+w/2 - camx);
double dy = (y+h/2 - camy);
double dz = (z+d/2 - camz);
double total = sqrt(dx*dx + dy*dy + dz*dz);
dx = 170.0*dx/total;
dy = 170.0*dy/total;
dz = 170.0*dz/total;
dx = 17.0*dx/total;
dy = 17.0*dy/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);
}
}
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
@ -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) {
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 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 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)));
//printf("(%lf %lf) (%lf %lf) (%lf %lf)\n", vx, camvx, vy, camvy, vz, camvz);
if(true) {
double oldvx = camvx;
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;
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 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 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)));
fx += dx/(dtime*dtime);
fy += dy/(dtime*dtime);
fz += dz/(dtime*dtime);
}
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 friction = 0.8;
double gravity_factor = 9.8;
bool noResetSpeed = false;
bool noFriction = false;
// ---------------------------------------------------------------------------------------------------- //
double fx;
double fy;
double fz;
bool updateForces = false;
int player_hp;
bool stop_evetything;
@ -48,6 +51,7 @@ int draw_type;
int fade_dmg;
bool has_changed;
int collisionVal = 0;
double room_width;
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) {
for(int k = 0; k < current_room->map_size; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]);
if(dist <= min_dist) {
if(updateForces) {
updateF(current_room->map[k], (double)dtime);
}
return true;
}
}
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);
if(dist <= min_dist) {
if(updateForces) {
updateF(current_room->tps[k]->hitbox, (double)dtime);
}
int old_chx = player_chx;
int old_chy = player_chy;
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++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->ents[k]->pos);
if(dist <= min_dist) {
if(updateForces) {
updateF(current_room->ents[k]->pos, (double)dtime);
}
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)));
if(*(current_room->ents[k]->hitpoints) <= 0) {
@ -122,45 +207,37 @@ bool is_colliding(float dtime) {
}
void movePlayerG(double dtime) {
camx += camvx*dtime;
noResetSpeed = false;
noFriction = false;
if(is_colliding(dtime)) {
camx -= camvx*dtime;
if(!noResetSpeed) {
camvx = 0.0;
}
}
if(!noFriction) {
camvx = camvx*(1-dtime*friction);
}
updateForces = true;
fx = 0.0;
fy = 0.0;
fz = 0.0;
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;
noResetSpeed = false;
noFriction = false;
double delx = camvx*dtime;
double dely = camvy*dtime;
double delz = camvz*dtime;
camx += delx;
camy += dely;
camz += delz;
if(is_colliding(dtime)) {
camz -= camvz*dtime;
if(!noResetSpeed) {
camvz = 0.0;
}
}
if(!noFriction) {
camvz = camvz*(1-dtime*friction);
//camvx = 0.0;
//camvy = 0.0;
//camvz = 0.0;
}
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() {

View File

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