1
1
Fork 0
Browse Source

phys progress

thread-physics
alistair 5 years ago
parent
commit
44398c5662
  1. BIN
      a.out
  2. 176
      game.c
  3. 24
      game.h
  4. BIN
      game.o
  5. BIN
      main
  6. 14
      main.c
  7. BIN
      main.o

BIN
a.out

Binary file not shown.

176
game.c

@ -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");
}

24
game.h

@ -12,6 +12,18 @@ @@ -12,6 +12,18 @@
#include "garbo.h"
#include "draw.h"
// unused since I'm implementing 2D physics
typedef union {
struct {
float x;
float y;
float z;
};
float vect[3];
} vector;
typedef struct {
int x_pos;
int y_pos;
@ -22,6 +34,15 @@ typedef struct { @@ -22,6 +34,15 @@ typedef struct {
float x_acc;
float y_acc;
// properties
float obj_mass; // kgs
// fields
float glob_friction;
bool glob_gravity; // t/f
} physics_thing;
typedef struct {
@ -50,7 +71,8 @@ int things_in_world; @@ -50,7 +71,8 @@ int things_in_world;
void startgame(SDL_Renderer * ren) ;
void process_input(SDL_Keysym key);
void process_keydown(SDL_Keysym key);
void process_keyup(SDL_Keysym key);
void step(int interval);

BIN
game.o

Binary file not shown.

BIN
main

Binary file not shown.

14
main.c

@ -77,16 +77,11 @@ int main () { @@ -77,16 +77,11 @@ int main () {
draw_pictures(ren);
bool splashscreen = true;
uint32_t last_advance_time;
uint32_t current_time;
while (!close) {
/* Update Physics */
current_time = SDL_GetTicks();
if ((current_time - last_advance_time) > 100) {
step(100);
last_advance_time = current_time;
}
step(100);
/* Redraw Screen */
redraw(ren);
@ -106,10 +101,13 @@ int main () { @@ -106,10 +101,13 @@ int main () {
}
SDL_Keysym key;
key = event.key.keysym;
process_input(key);
process_keydown(key);
// send key to game engine
break;
case SDL_KEYUP:
process_keyup(key);
default:
if (splashscreen) {
draw_pictures(ren);

BIN
main.o

Binary file not shown.
Loading…
Cancel
Save