1
1
Fork 0
Browse Source

fixed motor timeout

thread-physics
user 5 years ago
parent
commit
0dfeb3d000
  1. 2
      draw.c
  2. 22
      game.c
  3. 5
      game.h

2
draw.c

@ -126,7 +126,7 @@ void draw_forces(SDL_Renderer *ren, Body *body) { @@ -126,7 +126,7 @@ void draw_forces(SDL_Renderer *ren, Body *body) {
Vect F;
for (int i = 0; i < body->num_motors; i++) {
if (body->motors[i].TTL <= 0) {
if (body->motors[i].timeout < SDL_GetTicks()) {
continue;
}
F.x += body->motors[i].x;

22
game.c

@ -364,8 +364,8 @@ void set_motor_max_velocity(Body *thing, int motorID, double max) { @@ -364,8 +364,8 @@ void set_motor_max_velocity(Body *thing, int motorID, double max) {
thing->motors[motorID].max_velocity = max;
}
void set_motor_ttl(Body *thing, int motorID, double ttl) {
thing->motors[motorID].TTL = ttl;
void set_motor_timeout(Body *thing, int motorID, uint32_t timeout) {
thing->motors[motorID].timeout = timeout;
}
void set_motor_newtons(Body *thing, int motorID, double x, double y) {
@ -386,7 +386,7 @@ void add_motor(Body *thing, double x, double y) { @@ -386,7 +386,7 @@ void add_motor(Body *thing, double x, double y) {
motor.x = x;
motor.y = y;
motor.TTL = INFINITY;
motor.timeout = -1;
motor.max_velocity = 999899;
motor.update_motor = default_motor_curve;
@ -448,13 +448,15 @@ void advance_thing(Body * thing) { @@ -448,13 +448,15 @@ void advance_thing(Body * thing) {
return;
}
uint32_t time_delta = SDL_GetTicks() - thing->last_advance_time ;
uint32_t now = SDL_GetTicks();
uint32_t time_delta = now - thing->last_advance_time ;
thing->last_advance_time = SDL_GetTicks(); // in milliseconds
//time_delta = 17;
// motors
for (int i = 0; i < thing->num_motors; i++) {
if (thing->motors[i].TTL <= 0) {
if (thing->motors[i].timeout < now) {
continue;
}
@ -469,10 +471,6 @@ void advance_thing(Body * thing) { @@ -469,10 +471,6 @@ void advance_thing(Body * thing) {
double dirF = atan2(F.y, F.x);
double dirV = atan2(V.y, V.x);
if (thing->motors[i].TTL != INFINITY) {
thing->motors[i].TTL--;
}
double diff = dirV > dirF ? dirV - dirF: dirF - dirV;
if (thing->motors[i].max_velocity > vect_mag(vel_in_dir)
@ -625,8 +623,6 @@ void step(int interval) { @@ -625,8 +623,6 @@ void step(int interval) {
const uint8_t * keyboard;
keyboard = SDL_GetKeyboardState(NULL);
//set_motor_ms(&player.physics, 0 ,0);
//
set_motor_newtons(player.physics, M_PLAYER_WALK, 0, 0);
if (keyboard[SDL_SCANCODE_W]) {
@ -643,9 +639,9 @@ void step(int interval) { @@ -643,9 +639,9 @@ void step(int interval) {
&& !keyboard[SDL_SCANCODE_A]
&& !keyboard[SDL_SCANCODE_S]
&& !keyboard[SDL_SCANCODE_D]) {
set_motor_ttl(player.physics, M_PLAYER_WALK, 90);
set_motor_timeout(player.physics, M_PLAYER_WALK, SDL_GetTicks()+1000*2);
}
advance_things();
logwrite(DEBUG, "Update Physics \n");
logwrite(DEBUG, "Updated Physics \n");
}

5
game.h

@ -28,8 +28,9 @@ typedef struct motorstruct { @@ -28,8 +28,9 @@ typedef struct motorstruct {
// does not apply force if the velocity in the
// direction of the motor vector is greater than
// max_velocity
int TTL; // motor dies after this expires.
// units are simulation tick
uint32_t timeout;// absolute time (in ms, from when the program starts)
// for when the motor stops.
// set to -1 for infinity
void (*update_motor)(struct motorstruct *motor); // function pointer for generating
// the motor's output curve
} Motor;

Loading…
Cancel
Save