Compare commits

..

2 Commits

Author SHA1 Message Date
6cab255d92 fix: Corrected trigonometrry 2026-04-23 18:58:52 +01:00
2920749387 feat: Very broken trigonometry but planes turn 2026-04-23 17:51:22 +01:00
6 changed files with 55 additions and 7 deletions

View File

@@ -102,8 +102,30 @@ static vec2 max2(vec2 const &a, vec2 const &b)
static vec2 polar_to_vec2(const float angle, const float len)
{
// 0 (north) up, PI/2 (east) right
return {
.x = len * sinf(angle),
.y = len * cosf(angle)
};
}
static float vec2_angle(const vec2 v)
{
return atan2f(v.x, v.y);
}
static float normalize_angle(const float angle)
{
float a = angle;
while (a > 2*M_PIf) a -= 2*M_PIf;
while (a < 0) a += 2*M_PIf;
return a;
}
static float normalize_angle_diff(const float diff)
{
float d = diff;
while (d > M_PIf) d -= 2*M_PIf;
while (d < -M_PIf) d += 2*M_PIf;
return d;
}

View File

@@ -1,5 +1,7 @@
#include "af_spawner.hpp"
#include <math.h>
void dxd::sim::AFSpawner::tick(float timestep, World *world)
{
(void)timestep;
@@ -11,7 +13,7 @@ void dxd::sim::AFSpawner::tick(float timestep, World *world)
//Pos should spawn in a grid, check if free, and offset random from grid coord to look natural
vec2 pos = {(SDL_randf()-.5f)*2*60.0f, (SDL_randf()-.5f)*2*60.0f};
Airfield *af = new Airfield(pos, _MIN_AF_TTL + SDL_rand(_MAX_AF_TTL));
Airfield *af = new Airfield(pos, _MIN_AF_TTL + SDL_rand(_MAX_AF_TTL), SDL_randf()*2*M_PIf);
world->add_obj(af);
_airfields.push_back(af);
}

View File

@@ -5,6 +5,13 @@ void dxd::sim::Aircraft::tick(float timestep, World *world)
{
(void)world;
float target_angle = vec2_angle(_target - _position);
float target_deviation = target_angle - _direction;
target_deviation = normalize_angle_diff(target_deviation);
_direction += SDL_clamp(target_deviation, -_MAX_TURN_RATE, _MAX_TURN_RATE) * timestep;
_direction = normalize_angle(_direction);
_position = _position + polar_to_vec2(_direction, _speed) * timestep;
if (--_trail_cooldown == 0)
@@ -19,4 +26,7 @@ void dxd::sim::Aircraft::draw(Renderer *rend)
rend->color(255, 255, 255, 255);
rend->box(_position, 1);
rend->line(_position, _position + polar_to_vec2(_direction, 3));
rend->color(255, 64, 64, 255);
rend->diamond(_target, 1);
}

View File

@@ -12,14 +12,16 @@ private:
vec2 _position;
float _direction;
float _speed;
vec2 _target;
int _trail_cooldown;
const int _TRAIL_DELAY = 60;
const int _TRAIL_DURATION = 420;
const float _MAX_TURN_RATE = 0.5f;
public:
Aircraft(vec2 position, float direction, float speed)
: _position(position), _direction(direction), _speed(speed)
Aircraft(vec2 position, float direction, float speed, vec2 target)
: _position(position), _direction(direction), _speed(speed), _target(target)
{
_trail_cooldown = _TRAIL_DELAY;
}

View File

@@ -13,9 +13,13 @@ void dxd::sim::Airfield::tick(float timestep, World *world)
if (--_next_spawn == 0)
{
Aircraft *af = new Aircraft(_position, SDL_randf() * 2 * M_PIf, 5.0f);
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(0, 30);
Aircraft *af = new Aircraft(pos, _rw_heading, 5.0f, tgt);
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);
_next_spawn = 99999999;
}
}
@@ -24,4 +28,9 @@ void dxd::sim::Airfield::draw(Renderer *rend)
rend->color(127, 127, 127, 255);
rend->rect(_position, 5);
rend->diamond(_position, 5);
rend->color(64, 64, 255, 255);
vec2 a = _position + polar_to_vec2(_rw_heading, +_WR_LENGTH);
vec2 b = _position + polar_to_vec2(_rw_heading, -_WR_LENGTH);
rend->line(a, b);
}

View File

@@ -14,14 +14,17 @@ private:
vec2 _position;
int _ttl;
int _next_spawn;
float _rw_heading;
const int _MIN_TAKEOFF_DELAY = 240;
const int _MAX_TAKEOFF_DELAY = 600;
const int _WR_LENGTH = 6;
public:
Airfield(vec2 position, int ttl) : _position(position), _ttl(ttl)
Airfield(vec2 position, int ttl, float rw_heading)
: _position(position), _ttl(ttl), _rw_heading(rw_heading)
{
_next_spawn = 30;
_next_spawn = _MIN_TAKEOFF_DELAY;
}
void tick(float timestep, World *world) override;