@ -1,9 +1,9 @@
@@ -1,9 +1,9 @@
# include "game.h"
player_st * glob_player ;
void set_motor_newtons ( Body * thing , int motorID , double x , double y ) ;
void set_motor_max_velocity ( Body * thing , int motorID , double max ) ;
void get_new_physics ( Body * * phys ) ;
// move the collision poly to the position of the player
@ -89,6 +89,9 @@ void get_new_physics(Body **phys) {
@@ -89,6 +89,9 @@ void get_new_physics(Body **phys) {
memset ( physics - > motors , 0 , sizeof ( Motor ) * 100 ) ;
physics - > max_motors = 100 ;
physics - > num_motors = 0 ;
// gravity
add_motor ( physics , 0.0 , 0.0 ) ;
// friction
add_motor ( physics , 0.0 , 0.0 ) ;
physics - > updateCollisionPoly = default_update_collision_poly ;
@ -221,9 +224,6 @@ Vect get_normal(Vect start, Vect end) {
@@ -221,9 +224,6 @@ Vect get_normal(Vect start, Vect end) {
return norm ;
}
// reference:
// http://www.dyn4j.org/2010/01/sat/#sat-inter
//
@ -246,26 +246,22 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
@@ -246,26 +246,22 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
one_position = vect_add ( one_position , one - > collision_poly [ i ] ) ;
end = one - > collision_poly [ i ] ;
axes [ i - 1 ] = get_normal ( start , end ) ;
printf ( " .%d \n " , i - 1 ) ;
start = end ;
}
end = one - > collision_poly [ 0 ] ;
axes [ one - > collision_poly_size - 1 ] = get_normal ( start , end ) ;
printf ( " .%d \n " , one - > collision_poly_size - 1 ) ;
start = two - > collision_poly [ 0 ] ;
for ( int i = 1 ; i < two - > collision_poly_size ; i + + ) {
two_position = vect_add ( two_position , two - > collision_poly [ i ] ) ;
end = two - > collision_poly [ i ] ;
axes [ i - 1 + one - > collision_poly_size ] = get_normal ( start , end ) ;
printf ( " .%d \n " , i - 1 + one - > collision_poly_size ) ;
start = end ;
}
end = two - > collision_poly [ 0 ] ;
axes [ two - > collision_poly_size + one - > collision_poly_size - 1 ] = get_normal ( start , end ) ;
printf ( " .%d \n " , two - > collision_poly_size + one - > collision_poly_size - 1 ) ;
double * proj_one , * proj_two ;
proj_one = proj_two = 0 ;
@ -280,13 +276,12 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
@@ -280,13 +276,12 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
if ( proj_two ) {
free ( proj_two ) ;
}
printf ( " Testing on axis: %f %f \n " , axes [ i ] . x , axes [ i ] . y ) ;
proj_one = project_col_poly ( one , axes [ i ] ) ;
proj_two = project_col_poly ( two , axes [ i ] ) ;
if ( ( proj_one [ 0 ] > = proj_two [ 1 ] )
| | ( proj_two [ 0 ] > = proj_one [ 1 ] ) ) {
printf ( " not overlapping \ n" ) ;
logwrite ( INFO , " No collisio n" ) ;
free ( axes ) ;
free ( proj_one ) ;
free ( proj_two ) ;
@ -300,7 +295,7 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
@@ -300,7 +295,7 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
// one of the shapes is contained
if ( ( overlap > ( proj_one [ 1 ] - proj_one [ 0 ] )
| | ( overlap > ( proj_two [ 1 ] - proj_two [ 0 ] ) ) ) ) {
printf ( " COntained " ) ;
logwrite ( INFO , " Contained collision " ) ;
double min = proj_one [ 0 ] - proj_two [ 0 ] ;
double max = proj_one [ 1 ] - proj_two [ 1 ] ;
@ -312,7 +307,6 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
@@ -312,7 +307,6 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
}
printf ( " OVERLAP : %e \n " , overlap ) ;
if ( overlap < min_overlap & & overlap > 0 ) {
min_overlap = overlap ;
min_axis = axes [ i ] ;
@ -434,17 +428,17 @@ Wall *get_long_wall(int numNodes, int *nodes) {
@@ -434,17 +428,17 @@ Wall *get_long_wall(int numNodes, int *nodes) {
return wallwall ;
}
void accel_thing ( Body * thing , float x , float y ) {
void accel_thing ( Body * thing , double x , double y ) {
/* takes acceleration in m/s2 and converts to m/ms adding
* it to physics_thing
*/
// convert to m / millisecond
float x_adj = x / 1000.0 ;
float y_adj = y / 1000.0 ;
double x_adj = x / 1000.0 ;
double y_adj = y / 1000.0 ;
( * thing ) . acc . y + = ( float ) y_adj ;
( * thing ) . acc . x + = ( float ) x_adj ;
thing - > acc . y + = y_adj ;
thing - > acc . x + = x_adj ;
}
@ -489,10 +483,6 @@ void add_motor(Body *thing, double x, double y) {
@@ -489,10 +483,6 @@ void add_motor(Body *thing, double x, double y) {
// basic collision handler for testing
//
// TODO: Maybe adding all the MTVs together for the body will make it work better,
// will this solve the problem of negative normals? noo.. idk. But anyway it is more
// efficient for processing many collisions because you only do the physics for an
// MTV once rather than for all of the collisions separately.
int process_collisions ( Body * thing , Vect * trans ) {
Vect translation ;
translation . x = translation . y = 0 ;
@ -550,11 +540,11 @@ int process_collisions(Body *thing, Vect *trans) {
@@ -550,11 +540,11 @@ int process_collisions(Body *thing, Vect *trans) {
void advance_thing ( Body * thing ) {
// TODO: fix ordering of collision detection + physics sim so that collisions
// are less bad.
// TODO: Implement broad phase and narrow phase, else massively speed up collisions somehow.
thing - > acc . x = 0 ;
thing - > acc . y = 0 ;
printf ( " Position: %f %f \n " , thing - > position . x , thing - > position . y ) ;
set_motor_newtons ( thing , M_FRICTION , 0 , 0 ) ;
set_motor_max_velocity ( thing , M_FRICTION , 0 ) ;
if ( ! thing - > collision_poly [ 0 ] . y ) {
thing - > updateCollisionPoly ( thing ) ;
@ -566,6 +556,7 @@ void advance_thing(Body * thing) {
@@ -566,6 +556,7 @@ void advance_thing(Body * thing) {
thing - > updateCollisionPoly ( thing ) ;
Vect translation ;
Vect friction ;
translation . x = translation . y = 0 ;
int numcols = 0 ;
if ( ( numcols = process_collisions ( thing , & translation ) ) ) {
@ -589,12 +580,32 @@ void advance_thing(Body * thing) {
@@ -589,12 +580,32 @@ void advance_thing(Body * thing) {
revert_vel . x = cos ( vect_dir ( translation ) ) * mag ;
revert_vel . y = sin ( vect_dir ( translation ) ) * mag ;
thing - > vel . x - = revert_vel . x ;
thing - > vel . y - = revert_vel . y ;
// accel
double norm = 9.81 * thing - > obj_mass ;
double fric_const = 0.52 ;
Vect x ;
x . y = 0 ; x . x = 1 ;
if ( vect_scalar_projection ( thing - > vel , translation ) > 0 ) {
friction . x = translation . y ;
friction . y = - translation . x ;
} else {
friction . x = - translation . y ;
friction . y = translation . x ;
}
// force
friction . x = friction . x * norm * fric_const ;
friction . y = friction . y * norm * fric_const ;
printf ( " friction accel: %e %e \n " , friction . x , friction . y ) ;
printf ( " velocity: %e %e \n " , thing - > vel . x , thing - > vel . y ) ;
set_motor_newtons ( thing , M_FRICTION , friction . x , friction . y ) ;
set_motor_max_velocity ( thing , M_FRICTION , vect_mag ( project_vect ( thing - > vel , friction ) ) ) ;
// restitution force
Vect rest ;
/*rest.x = 0;*/
/*rest.y = 0;*/
/*double impulse = thing->obj_mass * vect_mag(thing->vel) * thing->obj_elasticity;*/
@ -610,7 +621,6 @@ void advance_thing(Body * thing) {
@@ -610,7 +621,6 @@ void advance_thing(Body * thing) {
thing - > last_advance_time = SDL_GetTicks ( ) ; // in milliseconds
if ( time_delta > 18 ) {
logwrite ( ERROR , " Simulation too slow \n " ) ;
return ;
}
// motors
@ -666,10 +676,10 @@ void advance_thing(Body * thing) {
@@ -666,10 +676,10 @@ void advance_thing(Body * thing) {
/*}*/
if ( fabs ( ( * thing ) . vel . x ) < 0.00 001 ) {
if ( fabsl ( ( * thing ) . vel . x ) < 0.001 ) {
( * thing ) . vel . x = 0 ;
}
if ( fabs ( ( * thing ) . vel . y ) < 0.00 001 ) {
if ( fabsl ( ( * thing ) . vel . y ) < 0.001 ) {
( * thing ) . vel . y = 0 ;
}
@ -741,7 +751,7 @@ void add_to_world(world_thing thing) {
@@ -741,7 +751,7 @@ void add_to_world(world_thing thing) {
}
void get_floor ( void ) {
int floorsize = 100 ;
int floorsize = 1000 ;
FloorPoly * polys = generate_floor_simple ( floorsize ) ;
Floor * floor = calloc ( 1 , sizeof ( Floor ) ) ;
@ -796,7 +806,7 @@ void walk_player(int x, int y) {
@@ -796,7 +806,7 @@ void walk_player(int x, int y) {
add_motor_newtons ( glob_player - > physics , M_PLAYER_WALK , 0 , 100 * 10 * y ) ;
return ;
}
add_motor_newtons ( glob_player - > physics , M_PLAYER_WALK , 100 * x , 100 * y ) ;
add_motor_newtons ( glob_player - > physics , M_PLAYER_WALK , 100 * 10 * x , 100 * y ) ;
}
void step ( int interval ) {