|
|
|
@ -27,44 +27,98 @@ player_st get_player(int x, int y) {
@@ -27,44 +27,98 @@ player_st get_player(int x, int y) {
|
|
|
|
|
|
|
|
|
|
player.physics.x_pos = x; |
|
|
|
|
player.physics.y_pos = y; |
|
|
|
|
|
|
|
|
|
player.physics.x_acc = 0; |
|
|
|
|
player.physics.y_acc = 0; |
|
|
|
|
|
|
|
|
|
player.physics.x_vel = 0; |
|
|
|
|
player.physics.y_vel = 0; |
|
|
|
|
|
|
|
|
|
player.max_walking_speed = 5; |
|
|
|
|
player.max_walking_speed = 1001;
|
|
|
|
|
|
|
|
|
|
player.physics.glob_gravity = true; |
|
|
|
|
player.physics.glob_friction = 0.0; |
|
|
|
|
|
|
|
|
|
player.physics.obj_mass = 100; |
|
|
|
|
|
|
|
|
|
return (player); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void accel_thing(physics_thing * thing, long x, long y) { |
|
|
|
|
void accel_thing(physics_thing * thing, float x, float y) { |
|
|
|
|
/* takes acceleration in m/s2 and converts to m/ms adding
|
|
|
|
|
* it to physics_thing
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
long x_adj = x / 1000.0; |
|
|
|
|
long y_adj = y / 1000.0; |
|
|
|
|
float x_adj = x / 1000.0; |
|
|
|
|
float y_adj = y / 1000.0; |
|
|
|
|
|
|
|
|
|
(*thing).y_acc += (float)y_adj; |
|
|
|
|
(*thing).x_acc += (float)x_adj; |
|
|
|
|
|
|
|
|
|
logwrite(DEBUG, "Accel thing\n"); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int advance_glob(physics_thing * thing) { |
|
|
|
|
logwrite(DEBUG, "Global physics\n"); |
|
|
|
|
|
|
|
|
|
// gravity
|
|
|
|
|
if ((*thing).glob_gravity == true) { |
|
|
|
|
// accel_thing(thing, 0, 9.81);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// friction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((*thing).glob_friction != 0.0) { |
|
|
|
|
/* god this is so jank */ |
|
|
|
|
float normal = (*thing).obj_mass * 9.81; |
|
|
|
|
float F_force = normal * (*thing).glob_friction; |
|
|
|
|
float dir = atan2((*thing).y_acc, (*thing).x_acc); |
|
|
|
|
|
|
|
|
|
dir += M_PI; |
|
|
|
|
|
|
|
|
|
float f_x = (float)(cos((double)dir)) * F_force; |
|
|
|
|
float f_y = (float)(sin((double)dir)) * F_force; |
|
|
|
|
|
|
|
|
|
if (abs((*thing).x_acc ) <= abs(f_x)) { |
|
|
|
|
(*thing).x_acc = 0; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
(*thing).x_acc += f_x; |
|
|
|
|
} |
|
|
|
|
if (abs((*thing).y_acc) <= abs(f_y)) { |
|
|
|
|
(*thing).y_acc = 0; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
(*thing).y_acc += f_y; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void advance_thing(physics_thing * thing) { |
|
|
|
|
static uint32_t last_advance_time; |
|
|
|
|
advance_glob(thing); |
|
|
|
|
|
|
|
|
|
uint16_t time_delta = last_advance_time - SDL_GetTicks(); |
|
|
|
|
static uint32_t last_advance_time; |
|
|
|
|
|
|
|
|
|
// milliseconds
|
|
|
|
|
last_advance_time = SDL_GetTicks(); |
|
|
|
|
uint32_t time_delta = SDL_GetTicks() - last_advance_time ; |
|
|
|
|
|
|
|
|
|
(*thing).x_vel = (*thing).x_acc * (float)time_delta; |
|
|
|
|
(*thing).y_vel = (*thing).y_acc * (float)time_delta; |
|
|
|
|
last_advance_time = SDL_GetTicks(); // in milliseconds
|
|
|
|
|
|
|
|
|
|
(*thing).x_pos += (int)((*thing).x_acc * 1/2 * (float)time_delta * (float)time_delta);
|
|
|
|
|
(*thing).y_pos += (int)((*thing).y_acc * 1/2 * (float)time_delta * (float)time_delta);
|
|
|
|
|
(*thing).x_vel += (*thing).x_acc * (float)time_delta; |
|
|
|
|
(*thing).y_vel += (*thing).y_acc * (float)time_delta; |
|
|
|
|
|
|
|
|
|
(*thing).x_pos += (int)((*thing).x_vel * 1/2 * (float)time_delta);
|
|
|
|
|
(*thing).y_pos += (int)((*thing).y_vel * 1/2 * (float)time_delta);
|
|
|
|
|
|
|
|
|
|
printf("%f, %f \n", (*thing).x_acc, (*thing).y_acc ); |
|
|
|
|
|
|
|
|
|
// apply global forces
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -72,7 +126,7 @@ void add_to_world(world_thing thing) {
@@ -72,7 +126,7 @@ void add_to_world(world_thing thing) {
|
|
|
|
|
world[things_in_world] = thing; |
|
|
|
|
things_in_world += 1; |
|
|
|
|
|
|
|
|
|
logwrite(INFO, "Added object to world"); |
|
|
|
|
logwrite(INFO, "Added object to world\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void startgame(SDL_Renderer * ren) { |
|
|
|
@ -87,48 +141,90 @@ void startgame(SDL_Renderer * ren) {
@@ -87,48 +141,90 @@ void startgame(SDL_Renderer * ren) {
|
|
|
|
|
add_to_world(player_world); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
float get_abs(x,y) { |
|
|
|
|
float get_abs(float x,float y) { |
|
|
|
|
return (sqrt(x*x + y*y)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void walk(physics_thing * thing, int max_walking_speed, int x, int y) { |
|
|
|
|
if (get_abs(x, y) < max_walking_speed) { |
|
|
|
|
//accel_thing(thing, 0.001, 0.001 );
|
|
|
|
|
(*thing).x_acc = 0.00001; |
|
|
|
|
(*thing).y_acc = 0; |
|
|
|
|
|
|
|
|
|
void walk(physics_thing * thing, float max_walking_speed, int x, int y) { |
|
|
|
|
|
|
|
|
|
if (get_abs((*thing).x_vel, (*thing).y_vel) < max_walking_speed /1000 ) { |
|
|
|
|
// accel_thing(thing, max_walking_speed * x, max_walking_speed * y );
|
|
|
|
|
(*thing).x_vel += max_walking_speed * 0.001 * x ; |
|
|
|
|
(*thing).y_vel += max_walking_speed * 0.001 * y ; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void walk_player(int x, int y) { |
|
|
|
|
walk(&player.physics, player.max_walking_speed, x, y); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void process_input(SDL_Keysym key) { |
|
|
|
|
switch (key.sym) { |
|
|
|
|
case SDLK_w: |
|
|
|
|
walk_player(0, 1); |
|
|
|
|
break; |
|
|
|
|
case SDLK_a: |
|
|
|
|
walk_player(-1, 0); |
|
|
|
|
break; |
|
|
|
|
case SDLK_s: |
|
|
|
|
walk_player(0, -1); |
|
|
|
|
break; |
|
|
|
|
case SDLK_d: |
|
|
|
|
walk_player(1, 0); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
void process_keydown(SDL_Keysym key) { |
|
|
|
|
/*
|
|
|
|
|
*switch (key.sym) { |
|
|
|
|
* case SDLK_w: |
|
|
|
|
* walk_player(0, 1); |
|
|
|
|
* break; |
|
|
|
|
* case SDLK_a: |
|
|
|
|
* walk_player(-1, 0); |
|
|
|
|
* break; |
|
|
|
|
* case SDLK_s: |
|
|
|
|
* walk_player(0, -1); |
|
|
|
|
* break; |
|
|
|
|
* case SDLK_d: |
|
|
|
|
* walk_player(1, 0); |
|
|
|
|
* break; |
|
|
|
|
*} |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void process_keyup(SDL_Keysym key) { |
|
|
|
|
/*
|
|
|
|
|
*switch (key.sym) { |
|
|
|
|
* case SDLK_w: |
|
|
|
|
* walk_player(0, -1); |
|
|
|
|
* break; |
|
|
|
|
* case SDLK_a: |
|
|
|
|
* walk_player(1, 0); |
|
|
|
|
* break; |
|
|
|
|
* case SDLK_s: |
|
|
|
|
* walk_player(0, 1); |
|
|
|
|
* break; |
|
|
|
|
* case SDLK_d: |
|
|
|
|
* walk_player(-1, 0); |
|
|
|
|
* break; |
|
|
|
|
*} |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void step(int interval) { |
|
|
|
|
advance_thing(&(player.physics)); |
|
|
|
|
// calc time interval
|
|
|
|
|
//
|
|
|
|
|
// advance game physics
|
|
|
|
|
void step(int interval) {
|
|
|
|
|
const uint8_t * keyboard; |
|
|
|
|
// player.physics.x_acc = 0;
|
|
|
|
|
// player.physics.y_acc = 0;
|
|
|
|
|
|
|
|
|
|
keyboard = SDL_GetKeyboardState(NULL); |
|
|
|
|
|
|
|
|
|
if (keyboard[SDL_SCANCODE_W]) { |
|
|
|
|
walk_player(0, -1); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (keyboard[SDL_SCANCODE_A]) { |
|
|
|
|
walk_player(-1, 0); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (keyboard[SDL_SCANCODE_S]) { |
|
|
|
|
walk_player(0, 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (keyboard[SDL_SCANCODE_D]) { |
|
|
|
|
walk_player(1, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
advance_thing(&(player.physics)); |
|
|
|
|
logwrite(DEBUG, "Update Physics \n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|