Replace π with τ; τ = 2π (http://tauday.com/)

This commit is contained in:
Neale Pickett 2010-07-22 15:23:45 -06:00
parent fb78cd6561
commit 7be64ca1fc
2 changed files with 13 additions and 12 deletions

View File

@ -14,7 +14,7 @@
#define DUMP_f(v) DUMPf("%s = %f", #v, v) #define DUMP_f(v) DUMPf("%s = %f", #v, v)
#define DUMP_p(v) DUMPf("%s = %p", #v, v) #define DUMP_p(v) DUMPf("%s = %p", #v, v)
#define DUMP_xy(v) DUMPf("%s = (%f, %f)", #v, v[0], v[1]); #define DUMP_xy(v) DUMPf("%s = (%f, %f)", #v, v[0], v[1]);
#define DUMP_angle(v) DUMPf("%s = %.3fπ", #v, (v/PI)); #define DUMP_angle(v) DUMPf("%s = %.3fτ", #v, (v/TAU));
#define sq(x) ((x) * (x)) #define sq(x) ((x) * (x))
@ -55,7 +55,7 @@ tank_get_turret(struct tank *tank)
void void
tank_set_turret(struct tank *tank, float angle) tank_set_turret(struct tank *tank, float angle)
{ {
tank->turret.desired = fmodf(angle, 2*PI); tank->turret.desired = fmodf(angle, TAU);
} }
int int
@ -253,13 +253,13 @@ tanks_move_tank(struct tanks_game *game,
/* Constrain rot_angle to between -PI and PI */ /* Constrain rot_angle to between -PI and PI */
rot_angle = tank->turret.desired - tank->turret.current; rot_angle = tank->turret.desired - tank->turret.current;
while (rot_angle < 0) { while (rot_angle < 0) {
rot_angle += 2*PI; rot_angle += TAU;
} }
rot_angle = fmodf(PI + rot_angle, 2*PI) - PI; rot_angle = fmodf(PI + rot_angle, TAU) - PI;
rot_angle = min(TANK_MAX_TURRET_ROT, rot_angle); rot_angle = min(TANK_MAX_TURRET_ROT, rot_angle);
rot_angle = max(-TANK_MAX_TURRET_ROT, rot_angle); rot_angle = max(-TANK_MAX_TURRET_ROT, rot_angle);
tank->turret.current = fmodf(tank->turret.current + rot_angle, 2*PI); tank->turret.current = fmodf(tank->turret.current + rot_angle, TAU);
} }
/* Fakey acceleration */ /* Fakey acceleration */
@ -322,9 +322,9 @@ tanks_move_tank(struct tanks_game *game,
The fraction of the circle traveled is equal to the speed The fraction of the circle traveled is equal to the speed
of the outer tread over the circumference of the circle: of the outer tread over the circumference of the circle:
Ft = So/(2*pi*r) Ft = So/(tau*r)
The angle traveled is: The angle traveled is:
theta = Ft * 2*pi theta = Ft * tau
This reduces to a simple This reduces to a simple
theta = So/r theta = So/r
We multiply it by dir to adjust for the direction of rotation We multiply it by dir to adjust for the direction of rotation
@ -336,7 +336,7 @@ tanks_move_tank(struct tanks_game *game,
} }
/* Now move the tank */ /* Now move the tank */
tank->angle = fmodf(tank->angle + angle + 2*PI, 2*PI); tank->angle = fmodf(tank->angle + angle + TAU, TAU);
{ {
float m[2]; float m[2];

View File

@ -2,6 +2,7 @@
#define __CTANKS_H__ #define __CTANKS_H__
/* Some useful constants */ /* Some useful constants */
#define TAU 6.28318530717958647692
#define PI 3.14159265358979323846 #define PI 3.14159265358979323846
#define TANK_MAX_SENSORS 10 #define TANK_MAX_SENSORS 10
@ -10,7 +11,7 @@
#define TANK_CANNON_RECHARGE 20 /* Turns to recharge cannon */ #define TANK_CANNON_RECHARGE 20 /* Turns to recharge cannon */
#define TANK_CANNON_RANGE (TANK_SENSOR_RANGE / 2) #define TANK_CANNON_RANGE (TANK_SENSOR_RANGE / 2)
#define TANK_MAX_ACCEL 35 #define TANK_MAX_ACCEL 35
#define TANK_MAX_TURRET_ROT (PI/10) #define TANK_MAX_TURRET_ROT (TAU/20)
#define TANK_TOP_SPEED 7 #define TANK_TOP_SPEED 7
/* (tank radius + tank radius)^2 */ /* (tank radius + tank radius)^2 */
@ -27,8 +28,8 @@
((TANK_CANNON_RANGE + TANK_RADIUS) * (TANK_CANNON_RANGE + TANK_RADIUS)) ((TANK_CANNON_RANGE + TANK_RADIUS) * (TANK_CANNON_RANGE + TANK_RADIUS))
#ifndef rad2deg #ifndef rad2deg
#define rad2deg(r) ((int)(180*(r)/PI)) #define rad2deg(r) ((int)(360*(r)/TAU))
#define deg2rad(r) ((r*PI)/180) #define deg2rad(r) ((r*TAU)/360)
#endif #endif
#ifndef max #ifndef max