1
1
Fork 0
Browse Source

better movement

thread-physics
alistair 5 years ago
parent
commit
c16e7e5545
  1. 68
      game.c
  2. 3
      game.h

68
game.c

@ -40,6 +40,10 @@ player_st get_player(int x, int y) { @@ -40,6 +40,10 @@ player_st get_player(int x, int y) {
player.physics.glob_friction = 0.0;
player.physics.obj_mass = 100;
//kgs
player.physics.motor_x= 0;
player.physics.motor_y = 0;
return (player);
}
@ -59,13 +63,25 @@ void accel_thing(physics_thing * thing, float x, float y) { @@ -59,13 +63,25 @@ void accel_thing(physics_thing * thing, float x, float y) {
}
void set_motor_ms(physics_thing * thing, float x, float y) {
// set motor force in newtons
float x_adj = x;
float y_adj = y;
(*thing).motor_x = (float)y_adj;
(*thing).motor_y = (float)x_adj;
logwrite(DEBUG, "set motor 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);
accel_thing(thing, 0, 9.81);
}
// friction
@ -101,6 +117,9 @@ int advance_glob(physics_thing * thing) { @@ -101,6 +117,9 @@ int advance_glob(physics_thing * thing) {
}
void advance_thing(physics_thing * thing) {
(*thing).x_acc = 0;
(*thing).y_acc = 0;
advance_glob(thing);
static uint32_t last_advance_time;
@ -109,13 +128,24 @@ void advance_thing(physics_thing * thing) { @@ -109,13 +128,24 @@ void advance_thing(physics_thing * thing) {
last_advance_time = SDL_GetTicks(); // in milliseconds
// motor
float motor_x = (float)(*thing).motor_x / (float)(*thing).obj_mass ;
float motor_y = (float)(*thing).motor_y / (float)(*thing).obj_mass ;
accel_thing(thing, motor_x, motor_y);
// accelerate based on accel
(*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 );
printf("player position: %d, %d \n", (*thing).x_pos, (*thing).y_pos );
printf("player acc: %f, %f \n", (*thing).x_acc, (*thing).y_acc);
// apply global forces
@ -147,17 +177,25 @@ float get_abs(float x,float y) { @@ -147,17 +177,25 @@ float get_abs(float x,float y) {
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 ) {
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 ;
(*thing).x_acc += max_walking_speed * 0.001 * x ;
(*thing).y_acc += max_walking_speed * 0.001 * y ;
}
}
void walk_player(int x, int y) {
walk(&player.physics, player.max_walking_speed, x, y);
// walk(&player.physics, player.max_walking_speed, x, y);
//set_motor_ms(&player.physics, player.max_walking_speed * 0.01 * x, player.max_walking_speed * 0.01 * y);
set_motor_ms(&player.physics, 100 * y , 100 * x);
// player.physics.x_acc = player.max_walking_speed * 0.00001 * x ;
// player.physics.y_acc = player.max_walking_speed * 0.00001 * y ;
}
@ -205,22 +243,14 @@ void step(int interval) { @@ -205,22 +243,14 @@ void step(int interval) {
// player.physics.y_acc = 0;
keyboard = SDL_GetKeyboardState(NULL);
walk_player(0,0);
if (keyboard[SDL_SCANCODE_W]) {
walk_player(0, -1);
}
if (keyboard[SDL_SCANCODE_A]) {
} if (keyboard[SDL_SCANCODE_A]) {
walk_player(-1, 0);
}
if (keyboard[SDL_SCANCODE_S]) {
} if (keyboard[SDL_SCANCODE_S]) {
walk_player(0, 1);
}
if (keyboard[SDL_SCANCODE_D]) {
} if (keyboard[SDL_SCANCODE_D]) {
walk_player(1, 0);
}

3
game.h

@ -43,6 +43,9 @@ typedef struct { @@ -43,6 +43,9 @@ typedef struct {
float glob_friction;
bool glob_gravity; // t/f
float motor_x;
float motor_y;
} physics_thing;
typedef struct {

Loading…
Cancel
Save