-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.c
63 lines (49 loc) · 1.71 KB
/
sim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include "macros.h"
#include "renderer.h"
#include "controller.h"
#include "util.h"
#include "types.h"
SimState simState;
const float TARGET_UPDATE_FREQUENCY = 20.f;
const float LOG_FREQUENCY = 1.f;
const float MAX_VELOCITY = 2.f;
float timer = 0;
float logTimer = LOG_FREQUENCY;
void update(float dt) {
timer -= dt;
logTimer -= dt;
if (logTimer < 0) {
logTimer = LOG_FREQUENCY;
printf("Vehicle accel %.2f,%.2f vel %.2f,%.2f pos %.2f,%.2f\n",
simState.vehicle.acceleration.x, simState.vehicle.acceleration.y,
simState.vehicle.velocity.x, simState.vehicle.velocity.y,
simState.vehicle.position.x, simState.vehicle.position.y);
}
if (timer < 0) {
timer = TARGET_UPDATE_FREQUENCY;
simState.target.x = (rand1() * 60.f) - 30.f;
simState.target.y = (rand1() * 60.f) - 30.f;
printf("Target at %.1f, %.1f\n", simState.target.x, simState.target.y);
}
Vec2 accel;
if (ERROR == getAccelerationPropIntegDeriv(&simState, dt, &accel)) {
exit(1);
} else {
simState.vehicle.acceleration.x = accel.x;
simState.vehicle.acceleration.y = accel.y;
}
if (!c_assert_vec2(simState.vehicle.acceleration, -100.f, 100.f)) {
exit(1);
}
simState.vehicle.velocity.x = simState.vehicle.velocity.x + simState.vehicle.acceleration.x * dt;
simState.vehicle.velocity.y = simState.vehicle.velocity.y + simState.vehicle.acceleration.y * dt;
simState.vehicle.position.x = simState.vehicle.position.x + simState.vehicle.velocity.x * dt;
simState.vehicle.position.y = simState.vehicle.position.y + simState.vehicle.velocity.y * dt;
}
int main (int argc, char **argv) {
seedRand();
initRenderer(argc, argv, update, &simState);
}