physics
This commit is contained in:
parent
541596e8c0
commit
7b79815188
BIN
obj/base.o
BIN
obj/base.o
Binary file not shown.
BIN
obj/display.o
BIN
obj/display.o
Binary file not shown.
BIN
obj/entities.o
BIN
obj/entities.o
Binary file not shown.
BIN
obj/generation.o
BIN
obj/generation.o
Binary file not shown.
BIN
obj/hash.o
BIN
obj/hash.o
Binary file not shown.
BIN
obj/main.o
BIN
obj/main.o
Binary file not shown.
BIN
obj/menus.o
BIN
obj/menus.o
Binary file not shown.
BIN
obj/move.o
BIN
obj/move.o
Binary file not shown.
BIN
obj/proj.o
BIN
obj/proj.o
Binary file not shown.
|
@ -90,7 +90,7 @@ void moving_xyz(double x, double y, double z, double w, double h, double d, doub
|
||||||
//printf("%lf %lf %lf\n", ret->x, ret->y, ret->z);
|
//printf("%lf %lf %lf\n", ret->x, ret->y, ret->z);
|
||||||
}
|
}
|
||||||
|
|
||||||
void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) {
|
void detectHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
|
||||||
if(ret->red == 193) {
|
if(ret->red == 193) {
|
||||||
ret->red = 0;
|
ret->red = 0;
|
||||||
ret->green = 192;
|
ret->green = 192;
|
||||||
|
@ -104,6 +104,34 @@ void detectHit(float dtime, int* hp, int* dmg, 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;
|
||||||
|
noResetSpeed = true;
|
||||||
|
}
|
||||||
|
if(true) {
|
||||||
|
double oldvz = camvz;
|
||||||
|
camvz += vz/dtime;
|
||||||
|
camz -= oldvz*dtime;
|
||||||
|
camz += camvz*dtime;
|
||||||
|
noResetSpeed = 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) {
|
||||||
double dx = (x+w/2 - camx);
|
double dx = (x+w/2 - camx);
|
||||||
double dy = (y+h/2 - camy);
|
double dy = (y+h/2 - camy);
|
||||||
|
@ -129,7 +157,7 @@ void go_to_player(double x, double y, double z, double w, double h, double d, do
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void explodeOnHit(float dtime, int* hp, int* dmg, cube_0* ret) {
|
void explodeOnHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
|
||||||
player_hp -= (*dmg);
|
player_hp -= (*dmg);
|
||||||
if(*dmg != 0) {
|
if(*dmg != 0) {
|
||||||
fade_dmg = 255;
|
fade_dmg = 255;
|
||||||
|
|
|
@ -13,7 +13,8 @@ void speen3(double x, double y, double z, double w, double h, double d, double h
|
||||||
void moving_xyz(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 moving_xyz(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);
|
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 detectHit(float dtime, int* hp, int* dmg, cube_0* ret);
|
void detectHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
|
||||||
void explodeOnHit(float dtime, int* hp, int* dmg, cube_0* ret);
|
void explodeOnHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
|
||||||
|
void translatePlayer(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -63,7 +63,7 @@ void init_ent_generator(int n) {
|
||||||
hashtbl_entities[4].id = 4;
|
hashtbl_entities[4].id = 4;
|
||||||
hashtbl_entities[4].name = "Platform";
|
hashtbl_entities[4].name = "Platform";
|
||||||
hashtbl_entities[4].updatePos = &moving_xyz;
|
hashtbl_entities[4].updatePos = &moving_xyz;
|
||||||
hashtbl_entities[4].onHit = NULL;
|
hashtbl_entities[4].onHit = &translatePlayer;
|
||||||
hashtbl_entities[4].onDeath = NULL;
|
hashtbl_entities[4].onDeath = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ typedef struct fct_entry {
|
||||||
char* name ;
|
char* name ;
|
||||||
void (*updatePos)(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 (*updatePos)(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) ;
|
||||||
// act as velocity function
|
// act as velocity function
|
||||||
void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret) ;
|
void (*onHit)(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) ;
|
||||||
// triggers when object is hit
|
// triggers when object is hit
|
||||||
void (*onDeath)(float dtime) ;
|
void (*onDeath)(float dtime) ;
|
||||||
|
|
||||||
|
|
27
src/main.c
27
src/main.c
|
@ -43,7 +43,7 @@ void processInput(GLFWwindow *window, float dtime) {
|
||||||
*/
|
*/
|
||||||
camvz = -speed*cos(rot_hz)*cos(rot_vt);
|
camvz = -speed*cos(rot_hz)*cos(rot_vt);
|
||||||
camvx = -speed*sin(rot_hz)*cos(rot_vt);
|
camvx = -speed*sin(rot_hz)*cos(rot_vt);
|
||||||
camvy = speed*sin(rot_vt);
|
camvy = vtmult*speed*sin(rot_vt);
|
||||||
}
|
}
|
||||||
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||||
/*
|
/*
|
||||||
|
@ -73,7 +73,7 @@ void processInput(GLFWwindow *window, float dtime) {
|
||||||
}*/
|
}*/
|
||||||
camvz = speed*cos(rot_hz)*cos(rot_vt);
|
camvz = speed*cos(rot_hz)*cos(rot_vt);
|
||||||
camvx = speed*sin(rot_hz)*cos(rot_vt);
|
camvx = speed*sin(rot_hz)*cos(rot_vt);
|
||||||
camvy = -speed*sin(rot_vt);
|
camvy = -vtmult*speed*sin(rot_vt);
|
||||||
}
|
}
|
||||||
if(glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
|
if(glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
|
||||||
/*for(int k = 0; k < 10; k++) {
|
/*for(int k = 0; k < 10; k++) {
|
||||||
|
@ -303,8 +303,10 @@ int main_alt() {
|
||||||
|
|
||||||
int fps = 60;
|
int fps = 60;
|
||||||
int interval = 1000000/fps;
|
int interval = 1000000/fps;
|
||||||
|
double slp_time = 1.0/fps;
|
||||||
|
|
||||||
float delta;
|
float delta = 0.0f;
|
||||||
|
double deltad = 0.0;
|
||||||
|
|
||||||
clock_t finish = clock();
|
clock_t finish = clock();
|
||||||
clock_t origin = clock();
|
clock_t origin = clock();
|
||||||
|
@ -315,7 +317,6 @@ int main_alt() {
|
||||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
triCount = 0;
|
triCount = 0;
|
||||||
origin = clock();
|
|
||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
generate_nearby_chunks(1);
|
generate_nearby_chunks(1);
|
||||||
|
@ -333,34 +334,30 @@ int main_alt() {
|
||||||
glUseProgram(shaderProgramR);
|
glUseProgram(shaderProgramR);
|
||||||
glBindVertexArray(RVAO);
|
glBindVertexArray(RVAO);
|
||||||
|
|
||||||
finish = clock();
|
|
||||||
delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC;
|
|
||||||
gl_drawInteger(shaderProgramR, (int)(1.0f/(delta)), 0.0f, -0.92f, 0.05, 32, 255, 32, 0.005, -1);
|
gl_drawInteger(shaderProgramR, (int)(1.0f/(delta)), 0.0f, -0.92f, 0.05, 32, 255, 32, 0.005, -1);
|
||||||
gl_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1);
|
gl_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1);
|
||||||
gl_drawData(shaderProgramR);
|
gl_drawData(shaderProgramR);
|
||||||
|
|
||||||
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
|
||||||
// -------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------
|
||||||
finish = clock();
|
|
||||||
delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC;
|
|
||||||
processInput(window, delta);
|
processInput(window, delta);
|
||||||
movePlayerG(delta);
|
movePlayerG(deltad);
|
||||||
teleport_on_edge();
|
teleport_on_edge();
|
||||||
update_entities(delta);
|
update_entities(delta);
|
||||||
updateProj(delta);
|
updateProj(delta);
|
||||||
//printf("%f\n", 1.0f/(delta));
|
|
||||||
//printf("%lf, %lf, %lf\n", camx, camy, camz);
|
|
||||||
|
|
||||||
usleep(max(0, interval-(int)(1000000*delta)));
|
usleep(max(0, interval-(int)(1000000*delta)));
|
||||||
finish = clock();
|
sim_time += deltad;
|
||||||
sim_time += ((double)finish - (double)origin)/CLOCKS_PER_SEC;
|
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
|
finish = clock();
|
||||||
|
delta = slp_time+((float)finish - (float)origin)/CLOCKS_PER_SEC;
|
||||||
|
deltad = slp_time+((double)finish - (double)origin)/CLOCKS_PER_SEC;
|
||||||
|
origin = clock();
|
||||||
}
|
}
|
||||||
|
|
||||||
//printf("10\n");
|
|
||||||
//fflush(stdout);
|
|
||||||
hashtbl_free(visited);
|
hashtbl_free(visited);
|
||||||
free_proj();
|
free_proj();
|
||||||
free_pool();
|
free_pool();
|
||||||
|
|
34
src/move.c
34
src/move.c
|
@ -18,9 +18,13 @@
|
||||||
// ---------------------------------------------------------------------------------------------------- //
|
// ---------------------------------------------------------------------------------------------------- //
|
||||||
double sensitivity = 0.06;
|
double sensitivity = 0.06;
|
||||||
double fov = 90.0;
|
double fov = 90.0;
|
||||||
//double speed = 0.22;
|
double creative_speed = 0.3;
|
||||||
double speed = 1000.0;
|
double speed = 8.0;
|
||||||
|
double vtmult = 1.5;
|
||||||
double min_dist = 0.7;
|
double min_dist = 0.7;
|
||||||
|
double friction = 0.8;
|
||||||
|
double gravity_factor = 9.8;
|
||||||
|
bool noResetSpeed = false;
|
||||||
// ---------------------------------------------------------------------------------------------------- //
|
// ---------------------------------------------------------------------------------------------------- //
|
||||||
|
|
||||||
int player_hp;
|
int player_hp;
|
||||||
|
@ -34,11 +38,6 @@ double camz;
|
||||||
double camvx;
|
double camvx;
|
||||||
double camvy;
|
double camvy;
|
||||||
double camvz;
|
double camvz;
|
||||||
|
|
||||||
double gravity_factor;
|
|
||||||
|
|
||||||
double friction;
|
|
||||||
|
|
||||||
double rot_hz;
|
double rot_hz;
|
||||||
double rot_vt;
|
double rot_vt;
|
||||||
|
|
||||||
|
@ -59,8 +58,6 @@ void init_csts() {
|
||||||
camvx = 0.0;
|
camvx = 0.0;
|
||||||
camvy = 0.0;
|
camvy = 0.0;
|
||||||
camvz = 0.0;
|
camvz = 0.0;
|
||||||
friction = 0.0005;
|
|
||||||
gravity_factor = 20000.0;
|
|
||||||
rot_hz = 0.0;
|
rot_hz = 0.0;
|
||||||
rot_vt = 0.0;
|
rot_vt = 0.0;
|
||||||
draw_type = 0;
|
draw_type = 0;
|
||||||
|
@ -109,7 +106,7 @@ bool is_colliding(float dtime) {
|
||||||
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(current_room->ents[k]->onHit != NULL) {
|
if(current_room->ents[k]->onHit != NULL) {
|
||||||
(*current_room->ents[k]->onHit)(dtime, current_room->ents[k]->hitpoints, ¤t_room->ents[k]->damage, &(*(current_room->ents[k]->pos)));
|
(*current_room->ents[k]->onHit)(dtime, current_room->ents[k]->hitpoints, ¤t_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) {
|
||||||
if(current_room->ents[k]->onDeath != NULL) {
|
if(current_room->ents[k]->onDeath != NULL) {
|
||||||
(*current_room->ents[k]->onDeath)(dtime);
|
(*current_room->ents[k]->onDeath)(dtime);
|
||||||
|
@ -123,28 +120,37 @@ bool is_colliding(float dtime) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void movePlayerG(float dtime) {
|
void movePlayerG(double dtime) {
|
||||||
camx += camvx*dtime;
|
camx += camvx*dtime;
|
||||||
|
noResetSpeed = false;
|
||||||
if(is_colliding(dtime)) {
|
if(is_colliding(dtime)) {
|
||||||
camx -= camvx*dtime;
|
camx -= camvx*dtime;
|
||||||
|
if(!noResetSpeed) {
|
||||||
camvx = 0.0;
|
camvx = 0.0;
|
||||||
}
|
}
|
||||||
camvx *= pow(friction, (double)dtime*10);
|
}
|
||||||
|
camvx = camvx*(1-dtime*friction);
|
||||||
|
|
||||||
camvy -= gravity_factor*dtime;
|
camvy -= gravity_factor*dtime;
|
||||||
camy += camvy*dtime;
|
camy += camvy*dtime;
|
||||||
|
noResetSpeed = false;
|
||||||
if(is_colliding(dtime)) {
|
if(is_colliding(dtime)) {
|
||||||
camy -= camvy*dtime;
|
camy -= camvy*dtime;
|
||||||
|
if(!noResetSpeed) {
|
||||||
camvy = 0.0;
|
camvy = 0.0;
|
||||||
}
|
}
|
||||||
camvy *= pow(friction, (double)dtime*10);
|
}
|
||||||
|
camvy = camvy*(1-dtime*friction);
|
||||||
|
|
||||||
camz += camvz*dtime;
|
camz += camvz*dtime;
|
||||||
|
noResetSpeed = false;
|
||||||
if(is_colliding(dtime)) {
|
if(is_colliding(dtime)) {
|
||||||
camz -= camvz*dtime;
|
camz -= camvz*dtime;
|
||||||
|
if(!noResetSpeed) {
|
||||||
camvz = 0.0;
|
camvz = 0.0;
|
||||||
}
|
}
|
||||||
camvz *= pow(friction, (double)dtime*10);
|
}
|
||||||
|
camvz = camvz*(1-dtime*friction);
|
||||||
}
|
}
|
||||||
|
|
||||||
void teleport_on_edge() {
|
void teleport_on_edge() {
|
||||||
|
|
|
@ -5,6 +5,6 @@ void init_csts();
|
||||||
bool is_colliding(float dtime);
|
bool is_colliding(float dtime);
|
||||||
|
|
||||||
void teleport_on_edge();
|
void teleport_on_edge();
|
||||||
void movePlayerG(float dtime);
|
void movePlayerG(double dtime);
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -36,7 +36,7 @@ typedef struct entity {
|
||||||
void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, struct entity* ent, cube_0* ret);
|
void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, struct entity* ent, cube_0* ret);
|
||||||
|
|
||||||
// triggers when object is hit
|
// triggers when object is hit
|
||||||
void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret);
|
void (*onHit)(float dtime, int* hp, int* dmg, struct entity* ent, cube_0* ret);
|
||||||
|
|
||||||
// triggers when <hitpoints> goes negative (death)
|
// triggers when <hitpoints> goes negative (death)
|
||||||
void (*onDeath)(float dtime);
|
void (*onDeath)(float dtime);
|
||||||
|
@ -104,7 +104,7 @@ extern double camvy;
|
||||||
extern double camvz;
|
extern double camvz;
|
||||||
|
|
||||||
extern double friction;
|
extern double friction;
|
||||||
|
extern double vtmult;
|
||||||
extern double gravity_factor;
|
extern double gravity_factor;
|
||||||
|
|
||||||
extern double rot_hz;
|
extern double rot_hz;
|
||||||
|
@ -147,4 +147,6 @@ extern float rectDefault[18];
|
||||||
|
|
||||||
extern int triCount;
|
extern int triCount;
|
||||||
|
|
||||||
|
extern bool noResetSpeed;
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -13,7 +13,7 @@ Teleporters :
|
||||||
[9.0, 1.0, 4.0, 1.0, 4.0, 2.0, 0.0, 0.0, 0, 0, 255; 1, 0]
|
[9.0, 1.0, 4.0, 1.0, 4.0, 2.0, 0.0, 0.0, 0, 0, 255; 1, 0]
|
||||||
|
|
||||||
Entities:
|
Entities:
|
||||||
[0.0, 14.0, 0.0, 2.0, 1.0, 2.0, 0.0, 0.0, 193, 192, 0, 1, 0, 4, 0.0, 5.0, 0.0, 80, 1, 45]
|
[0.0, 14.0, 0.0, 2.0, 1.0, 2.0, 0.0, 0.0, 193, 192, 0, 1, 0, 4, 0.0, 5.0, 0.0, 4, 1, 45]
|
||||||
|
|
||||||
Weight :
|
Weight :
|
||||||
50
|
50
|
||||||
|
|
|
@ -8,9 +8,9 @@ Teleporters :
|
||||||
[-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0]
|
[-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0]
|
||||||
|
|
||||||
Entities:
|
Entities:
|
||||||
[-15.0, 4.0, -15.0, 4.0, 1.0, 4.0, 0.0, 0.0, 192, 128, 192, 10, 0, 4, 0.0, 5.0, 0.0, 50, 1, 90]
|
[-15.0, 4.0, -15.0, 4.0, 1.0, 4.0, 0.0, 0.0, 192, 128, 192, 10, 0, 4, 0.0, 5.0, 0.0, 2, 1, 90]
|
||||||
[-5.0, 9.0, -15.0, 4.0, 1.0, 4.0, 0.0, 0.0, 192, 128, 192, 10, 0, 4, 5.0, 0.0, 0.0, 50, 1, 0]
|
[-5.0, 9.0, -15.0, 4.0, 1.0, 4.0, 0.0, 0.0, 192, 128, 192, 10, 0, 4, 5.0, 0.0, 0.0, 2, 1, 0]
|
||||||
[0.0, 10.0, -5.0, 4.0, 1.0, 4.0, 0.0, 0.0, 192, 128, 192, 10, 0, 4, 0.0, 0.0, 5.0, 50, 1, 270]
|
[0.0, 10.0, -5.0, 4.0, 1.0, 4.0, 0.0, 0.0, 192, 128, 192, 10, 0, 4, 0.0, 0.0, 5.0, 2, 1, 270]
|
||||||
|
|
||||||
Weight :
|
Weight :
|
||||||
50
|
50
|
||||||
|
|
Loading…
Reference in New Issue