summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/vector.h8
-rw-r--r--src/atragmx.c79
-rw-r--r--src/vector.c26
3 files changed, 64 insertions, 49 deletions
diff --git a/include/vector.h b/include/vector.h
index fe6c563..72e6523 100644
--- a/include/vector.h
+++ b/include/vector.h
@@ -3,7 +3,7 @@
typedef struct { double x, y, z; } Vec3;
-static inline Vec3 vec3_add(Vec3 a, Vec3 b);
-static inline Vec3 vec3_sub(Vec3 a, Vec3 b);
-static inline Vec3 vec3_mul_scalar(Vec3 a, double s);
-static inline double
+static inline double vec3_magnitude(Vec3 v);
+static inline Vec3 vec3_diff(Vec3 a, Vec3 b);
+static inline Vec3 vec3_normalize(Vec3 v);
+static inline Vec3 vec3_mul(Vec3 v, double n);
diff --git a/src/atragmx.c b/src/atragmx.c
index c9cebae..07cd580 100644
--- a/src/atragmx.c
+++ b/src/atragmx.c
@@ -7,6 +7,7 @@
#include "constants.h"
#include "atragmx.h"
#include "ace.h"
+#include "vector.h"
ATragMXSolution *ATragMXCalcSolution(ATragMXContext *ctx, ace_config *ace) {
@@ -34,19 +35,19 @@ ATragMXSolution *ATragMXCalcSolution(ATragMXContext *ctx, ace_config *ace) {
struct {
double tx;
double tz;
- double last_pos[3];
- double pos[3];
- double velocity[3];
- double accel[3];
+ Vec3 last_pos;
+ Vec3 pos;
+ Vec3 velocity;
+ Vec3 accel;
double speed;
- double gravity[3];
+ Vec3 gravity;
double deltaT;
} bullet = {
.gravity = {
- [0] = 0.0,
- [1] = sin(ctx->gun.scope_angle + ctx->target.inclination_angle)
+ .x = 0.0,
+ .y = sin(ctx->gun.scope_angle + ctx->target.inclination_angle)
* -GRAVITY,
- [2] = cos(ctx->gun.scope_angle + ctx->target.inclination_angle)
+ .z = cos(ctx->gun.scope_angle + ctx->target.inclination_angle)
* -GRAVITY
},
.deltaT = 1.0 / ctx->sim_steps,
@@ -57,7 +58,7 @@ ATragMXSolution *ATragMXCalcSolution(ATragMXContext *ctx, ace_config *ace) {
Windage windage;
double lead;
double time_of_flight;
- double true_velocity[3];
+ Vec3 true_velocity;
double true_speed;
double v_coriolis;
double v_coriolis_deflection;
@@ -77,20 +78,20 @@ ATragMXSolution *ATragMXCalcSolution(ATragMXContext *ctx, ace_config *ace) {
.range_card = NULL,
};
- double wind1[3] = {
- [0] = cos(270 - ctx->atmosphere.wind_dir * 30)
+ Vec3 wind1 = {
+ .x = cos(270 - ctx->atmosphere.wind_dir * 30)
* ctx->atmosphere.wind_speed[0],
- [1] = sin(270 - ctx->atmosphere.wind_dir * 30)
+ .y = sin(270 - ctx->atmosphere.wind_dir * 30)
* ctx->atmosphere.wind_speed[0],
- [2] = 0
+ .z = 0
};
- double wind2[3] = {
- [0] = cos(270 - ctx->atmosphere.wind_dir * 30)
+ Vec3 wind2 = {
+ .x = cos(270 - ctx->atmosphere.wind_dir * 30)
* ctx->atmosphere.wind_speed[1],
- [1] = sin(270 - ctx->atmosphere.wind_dir * 30)
+ .y = sin(270 - ctx->atmosphere.wind_dir * 30)
* ctx->atmosphere.wind_speed[1],
- [2] = 0
+ .z = 0
};
double wind_drift = 0.0;
@@ -111,44 +112,32 @@ ATragMXSolution *ATragMXCalcSolution(ATragMXContext *ctx, ace_config *ace) {
* sin(ctx->target.direction);
}
- memcpy(bullet.pos,
- (double[3]){0.0,
- 0.0,
- -(ctx->gun.bore_height / 100.0)},
- sizeof(bullet.pos));
-
- memcpy(bullet.velocity,
- (double[3]){0.0,
- cos(ctx->gun.scope_angle) * ctx->gun.muzzle_velocity,
- sin(ctx->gun.scope_angle) * ctx->gun.muzzle_velocity},
- sizeof(bullet.pos));
+ bullet.pos.x = 0.0;
+ bullet.pos.y = 0.0;
+ bullet.pos.z = -(ctx->gun.bore_height / 100.0);
+
+ bullet.velocity.x = 0.0;
+ bullet.velocity.y = cos(ctx->gun.scope_angle) * ctx->gun.muzzle_velocity;
+ bullet.velocity.z = sin(ctx->gun.scope_angle) * ctx->gun.muzzle_velocity;
while (local_solution.time_of_flight < 15
- && bullet.pos[1] < ctx->target.range) {
+ && bullet.pos.y < ctx->target.range) {
// calculate magnitude of bullet.velocity
- bullet.speed = hypot(bullet.velocity[0],
- hypot(bullet.velocity[1], bullet.velocity[2]));
-
- memcpy(local_solution.true_velocity,
- (double[3]){bullet.velocity[0] - wind1[0],
- bullet.velocity[1] - wind1[1],
- bullet.velocity[2] - wind1[2]},
- sizeof(local_solution.true_velocity));
+ bullet.speed = vec3_magnitude(bullet.velocity);
+ local_solution.true_velocity = vec3_diff(bullet.velocity, wind1);
- local_solution.true_speed = hypot(local_solution.true_velocity[0],
- hypot(local_solution.true_velocity[1],
- local_solution.true_velocity[2]));
+ local_solution.true_speed = vec3_magnitude(local_solution.true_velocity);
if (ace->ballistics->enabled) {
double data = retard(ctx->gun.drag_model,
ctx->gun.ballistic_coef,
ctx->gun.muzzle_velocity,
ctx->atmosphere.temperature);
- memcpy(bullet.accel,
- (double[3]){bullet.accel[0] != 0 ? -data : 0,
- bullet.accel[1] != 0 ? -data : 0,
- bullet.accel[2] != 0 ? -data : 0},
-
+ /* memcpy(bullet.accel, */
+ /* (double[3]){bullet.accel[0] != 0 ? -data : 0, */
+ /* bullet.accel[1] != 0 ? -data : 0, */
+ /* bullet.accel[2] != 0 ? -data : 0}, */
+ /* ); */
}
}
diff --git a/src/vector.c b/src/vector.c
index 7da6cc5..7274cbb 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -1 +1,27 @@
+#include <math.h>
+
#include "vector.h"
+
+static inline double vec3_magnitude(Vec3 v) {
+ return hypot(v.x, hypot(v.y, v.z));
+}
+
+static inline Vec3 vec3_diff(Vec3 a, Vec3 b) {
+ return (Vec3){.x = a.x - b.x,
+ .y = a.y - b.y,
+ .z = a.z - b.z};
+}
+
+static inline Vec3 vec3_normalize(Vec3 v) {
+ double len = sqrt(v.x * v.x
+ + v.y * v.y
+ + v.z * v.z);
+ if (len != 0.0)
+ return (Vec3){.x = v.x / len,
+ .y = v.y / len,
+ .z = v.z / len};
+}
+
+static inline Vec3 vec3_mul(Vec3 v, double n) {
+
+}