code: improve vehicle code

isolation_bkp/dynres
Dominik Madarász 2021-08-09 21:04:23 +02:00
parent e00b8d7e5f
commit ef2b6cd68e
1 changed files with 18 additions and 15 deletions

View File

@ -212,24 +212,11 @@ void VehicleHandling(ecs_iter_t *it) {
for (int i = 0; i < it->count; i++) { for (int i = 0; i < it->count; i++) {
Vehicle *car = &veh[i]; Vehicle *car = &veh[i];
// NOTE(zaklaus): Apply friction
car->speed *= 0.99f; car->speed *= 0.99f;
car->steer *= 0.97f; car->steer *= 0.97f;
float fr_x = p[i].x + (car->wheel_base/2.0f) * zpl_cos(car->heading);
float fr_y = p[i].y + (car->wheel_base/2.0f) * zpl_sin(car->heading);
float bk_x = p[i].x - (car->wheel_base/2.0f) * zpl_cos(car->heading);
float bk_y = p[i].y - (car->wheel_base/2.0f) * zpl_sin(car->heading);
bk_x += car->speed * zpl_cos(car->heading);
bk_y += car->speed * zpl_sin(car->heading);
fr_x += car->speed * zpl_cos(car->heading + zpl_to_radians(car->steer));
fr_y += car->speed * zpl_sin(car->heading + zpl_to_radians(car->steer));
v[i].x = (fr_x + bk_x) / 2.0f - p[i].x;
v[i].y = (fr_y + bk_y) / 2.0f - p[i].y;
car->heading = zpl_arctan2(fr_y - bk_y, fr_x - bk_x);
for (int j = 0; j < 4; j++) { for (int j = 0; j < 4; j++) {
// NOTE(zaklaus): Perform seat cleanup // NOTE(zaklaus): Perform seat cleanup
if (!ecs_is_alive(world_ecs(), veh[i].seats[j])) { if (!ecs_is_alive(world_ecs(), veh[i].seats[j])) {
@ -257,6 +244,22 @@ void VehicleHandling(ecs_iter_t *it) {
car->steer = zpl_clamp(car->steer, -40.0f, 40.0f); car->steer = zpl_clamp(car->steer, -40.0f, 40.0f);
} }
} }
// NOTE(zaklaus): Vehicle physics
float fr_x = p[i].x + (car->wheel_base/2.0f) * zpl_cos(car->heading);
float fr_y = p[i].y + (car->wheel_base/2.0f) * zpl_sin(car->heading);
float bk_x = p[i].x - (car->wheel_base/2.0f) * zpl_cos(car->heading);
float bk_y = p[i].y - (car->wheel_base/2.0f) * zpl_sin(car->heading);
bk_x += car->speed * zpl_cos(car->heading);
bk_y += car->speed * zpl_sin(car->heading);
fr_x += car->speed * zpl_cos(car->heading + zpl_to_radians(car->steer));
fr_y += car->speed * zpl_sin(car->heading + zpl_to_radians(car->steer));
v[i].x = (fr_x + bk_x) / 2.0f - p[i].x;
v[i].y = (fr_y + bk_y) / 2.0f - p[i].y;
car->heading = zpl_arctan2(fr_y - bk_y, fr_x - bk_x);
} }
} }