fix: Corrected trigonometrry
This commit is contained in:
38
src/math.hpp
38
src/math.hpp
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
typedef struct vec2 { float x, y; } vec2;
|
typedef struct vec2 { float x, y; } vec2;
|
||||||
typedef struct vec2i { int x, y; } vec2i;
|
typedef struct vec2i { int x, y; } vec2i;
|
||||||
typedef struct polar_t { float angle, length; } polar_t;
|
|
||||||
|
|
||||||
static const vec2 vec2_zero = { 0, 0 };
|
static const vec2 vec2_zero = { 0, 0 };
|
||||||
static const vec2 vec2_one = { 1, 1 };
|
static const vec2 vec2_one = { 1, 1 };
|
||||||
@@ -103,29 +102,30 @@ static vec2 max2(vec2 const &a, vec2 const &b)
|
|||||||
|
|
||||||
static vec2 polar_to_vec2(const float angle, const float len)
|
static vec2 polar_to_vec2(const float angle, const float len)
|
||||||
{
|
{
|
||||||
// 0 (north) up
|
// 0 (north) up, PI/2 (east) right
|
||||||
return {
|
return {
|
||||||
.x = len * sinf(angle),
|
.x = len * sinf(angle),
|
||||||
.y = len * cosf(angle)
|
.y = len * cosf(angle)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static polar_t vec2_to_polar(const vec2 v)
|
static float vec2_angle(const vec2 v)
|
||||||
{
|
{
|
||||||
// 0 (north) up
|
return atan2f(v.x, v.y);
|
||||||
if (v.x == 0)
|
}
|
||||||
return {
|
|
||||||
.angle = (v.y > 0) ? 0 : M_PIf,
|
static float normalize_angle(const float angle)
|
||||||
.length = abs(v.y)
|
{
|
||||||
};
|
float a = angle;
|
||||||
|
while (a > 2*M_PIf) a -= 2*M_PIf;
|
||||||
polar_t ret = {
|
while (a < 0) a += 2*M_PIf;
|
||||||
.angle = atanf(v.x / v.y) + (v.y > 0 ? 0 : M_PIf),
|
return a;
|
||||||
.length = norm(v)
|
}
|
||||||
};
|
|
||||||
|
static float normalize_angle_diff(const float diff)
|
||||||
if (ret.angle < 0)
|
{
|
||||||
ret.angle += 2 * M_PIf;
|
float d = diff;
|
||||||
|
while (d > M_PIf) d -= 2*M_PIf;
|
||||||
return ret;
|
while (d < -M_PIf) d += 2*M_PIf;
|
||||||
|
return d;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,16 +5,13 @@ void dxd::sim::Aircraft::tick(float timestep, World *world)
|
|||||||
{
|
{
|
||||||
(void)world;
|
(void)world;
|
||||||
|
|
||||||
float target_direction = vec2_to_polar(_target - _position).angle;
|
float target_angle = vec2_angle(_target - _position);
|
||||||
float target_deviation = target_direction - _direction;
|
float target_deviation = target_angle - _direction;
|
||||||
//TODO: Fix this normalization mess
|
|
||||||
if (target_deviation > M_PIf)
|
target_deviation = normalize_angle_diff(target_deviation);
|
||||||
{
|
|
||||||
target_deviation -= 2 * M_PIf;
|
|
||||||
}
|
|
||||||
printf("%0.2f %.2f %0.2f\n", target_direction, target_deviation, target_direction - _direction);
|
|
||||||
|
|
||||||
_direction += SDL_clamp(target_deviation, -_MAX_TURN_RATE, _MAX_TURN_RATE) * timestep;
|
_direction += SDL_clamp(target_deviation, -_MAX_TURN_RATE, _MAX_TURN_RATE) * timestep;
|
||||||
|
_direction = normalize_angle(_direction);
|
||||||
_position = _position + polar_to_vec2(_direction, _speed) * timestep;
|
_position = _position + polar_to_vec2(_direction, _speed) * timestep;
|
||||||
|
|
||||||
if (--_trail_cooldown == 0)
|
if (--_trail_cooldown == 0)
|
||||||
@@ -29,4 +26,7 @@ void dxd::sim::Aircraft::draw(Renderer *rend)
|
|||||||
rend->color(255, 255, 255, 255);
|
rend->color(255, 255, 255, 255);
|
||||||
rend->box(_position, 1);
|
rend->box(_position, 1);
|
||||||
rend->line(_position, _position + polar_to_vec2(_direction, 3));
|
rend->line(_position, _position + polar_to_vec2(_direction, 3));
|
||||||
|
|
||||||
|
rend->color(255, 64, 64, 255);
|
||||||
|
rend->diamond(_target, 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ void dxd::sim::Airfield::tick(float timestep, World *world)
|
|||||||
{
|
{
|
||||||
vec2 pos = _position + polar_to_vec2(_rw_heading, _WR_LENGTH+0.5);
|
vec2 pos = _position + polar_to_vec2(_rw_heading, _WR_LENGTH+0.5);
|
||||||
//vec2 tgt = _position + polar_to_vec2(SDL_randf()*2*M_PIf, 30);
|
//vec2 tgt = _position + polar_to_vec2(SDL_randf()*2*M_PIf, 30);
|
||||||
vec2 tgt = _position + polar_to_vec2(0, 80);
|
vec2 tgt = _position + polar_to_vec2(0, 30);
|
||||||
Aircraft *af = new Aircraft(pos, _rw_heading, 5.0f, tgt);
|
Aircraft *af = new Aircraft(pos, _rw_heading, 5.0f, tgt);
|
||||||
world->add_obj(af);
|
world->add_obj(af);
|
||||||
//_next_spawn = _MIN_TAKEOFF_DELAY + SDL_rand(_MAX_TAKEOFF_DELAY - _MIN_TAKEOFF_DELAY);
|
//_next_spawn = _MIN_TAKEOFF_DELAY + SDL_rand(_MAX_TAKEOFF_DELAY - _MIN_TAKEOFF_DELAY);
|
||||||
|
|||||||
Reference in New Issue
Block a user