diff --git a/src/forces.zig b/src/forces.zig index ca3ee49..86171ea 100644 --- a/src/forces.zig +++ b/src/forces.zig @@ -34,9 +34,9 @@ pub fn drag( velocity: vec3.Vec3F64, ) vec3.Vec3F64 { return .init( - -velocity.x() * drag_coefficient, - -velocity.y() * drag_coefficient, - -velocity.z() * drag_coefficient, + velocity.x() * drag_coefficient, + velocity.y() * drag_coefficient, + velocity.z() * drag_coefficient, ); } @@ -73,7 +73,7 @@ test "drag is correct" { const velocity = vec3.Vec3F64.init(0, 1, 1); const f_drag = drag(0.5, velocity); - try std.testing.expectApproxEqAbs(-0.0, f_drag.x(), 1e-11); - try std.testing.expectApproxEqAbs(-0.5, f_drag.y(), 1e-11); - try std.testing.expectApproxEqAbs(-0.5, f_drag.z(), 1e-11); + try std.testing.expectApproxEqAbs(0, f_drag.x(), 1e-11); + try std.testing.expectApproxEqAbs(0.5, f_drag.y(), 1e-11); + try std.testing.expectApproxEqAbs(0.5, f_drag.z(), 1e-11); } diff --git a/src/integration.zig b/src/integration.zig index b6d53cd..1e7578c 100644 --- a/src/integration.zig +++ b/src/integration.zig @@ -28,14 +28,14 @@ pub fn timestep(dt: f64, d: drone.Drone, props: []const drone.Propeller, s: *dro ); s.omega = vec3.vec3Add(s.omega, vec3.vec3Scale(omega_dot, dt)); - const half_omega_quat: vec3.QuatF64 = .init( + const half_omege_quat: vec3.QuatF64 = .init( s.omega.x() * 0.5, s.omega.y() * 0.5, s.omega.z() * 0.5, 0, ); - const qdot = vec3.quatMul(half_omega_quat, s.q); + const qdot = vec3.quatMul(half_omege_quat, s.q); s.q = vec3.quatAdd(s.q, vec3.quatScale(qdot, dt)).normalized(); return; } @@ -85,23 +85,4 @@ test "Validate timesteps" { try std.testing.expectApproxEqAbs(0.12403234937465502, state.q.y(), 1e-12); try std.testing.expectApproxEqAbs(0.006201617468732751, state.q.z(), 1e-11); try std.testing.expectApproxEqAbs(0.9922587949972401, state.q.w(), 1e-11); - - timestep(0.005, d, &props, &state); - - try std.testing.expectApproxEqAbs(-1.23072190e+01, state.v.x(), 1e-7); - try std.testing.expectApproxEqAbs(-7.69201185e-02, state.v.y(), 1e-8); - try std.testing.expectApproxEqAbs(-9.83635311e+01, state.v.z(), 1e-7); - - try std.testing.expectApproxEqAbs(-6.15360950e-02, state.x_cg.x(), 1e-9); - try std.testing.expectApproxEqAbs(-3.84600592e-04, state.x_cg.y(), 1e-9); - try std.testing.expectApproxEqAbs(-7.41572489e-01, state.x_cg.z(), 1e-9); - - try std.testing.expectApproxEqAbs(-0.46875, state.omega.x(), 1e-12); - try std.testing.expectApproxEqAbs(100, state.omega.y(), 1e-12); - try std.testing.expectApproxEqAbs(5, state.omega.z(), 1e-11); - - try std.testing.expectApproxEqAbs(-0.0011280012096230429, state.q.x(), 1e-12); - try std.testing.expectApproxEqAbs(0.36096743708693396, state.q.y(), 1e-12); - try std.testing.expectApproxEqAbs(0.01790701920276581, state.q.z(), 1e-12); - try std.testing.expectApproxEqAbs(0.9324057998744074, state.q.w(), 1e-12); } diff --git a/src/vec3.zig b/src/vec3.zig index 3079253..e9cdd25 100644 --- a/src/vec3.zig +++ b/src/vec3.zig @@ -90,7 +90,7 @@ pub fn mat3Mul(a: Mat3F64, b: Mat3F64) Mat3F64 { pub fn mat3Det(a: Mat3F64) f64 { return a.row1.x() * (a.row2.y() * a.row3.z() - a.row2.z() * a.row3.y()) - a.row1.y() * (a.row2.x() * a.row3.z() - a.row2.z() * a.row3.x()) + - a.row1.z() * (a.row2.x() * a.row3.y() - a.row2.y() * a.row3.x()); + a.row1.y() * (a.row2.x() * a.row3.y() - a.row2.y() * a.row3.x()); } pub fn mat3Inv(a: Mat3F64) Mat3F64 { @@ -105,7 +105,7 @@ pub fn mat3Inv(a: Mat3F64) Mat3F64 { ); const row2 = vec3Scale( .init( - a.row2.z() * a.row3.x() - a.row2.x() * a.row3.z(), + a.row2.z() * a.row3.x() + a.row2.x() * a.row3.z(), a.row1.x() * a.row3.z() - a.row1.z() * a.row3.x(), a.row1.z() * a.row2.x() - a.row1.x() * a.row2.z(), ), @@ -113,7 +113,7 @@ pub fn mat3Inv(a: Mat3F64) Mat3F64 { ); const row3 = vec3Scale( .init( - a.row2.x() * a.row3.y() - a.row2.y() * a.row3.x(), + a.row2.x() * a.row3.y() + a.row2.y() * a.row3.x(), a.row1.y() * a.row3.x() - a.row1.x() * a.row3.y(), a.row1.x() * a.row2.y() - a.row1.y() * a.row2.x(), ), @@ -321,30 +321,30 @@ test "Mat3 mul works" { test "mat determinant works" { const a: Mat3F64 = .{ .row1 = Vec3F64.init(1, 2, 3), - .row2 = Vec3F64.init(4, 1, 5), - .row3 = Vec3F64.init(3, 1, 9), + .row2 = Vec3F64.init(0, 1, 5), + .row3 = Vec3F64.init(0, 0, 9), }; - try std.testing.expectApproxEqAbs(-35, mat3Det(a), 1e-12); + try std.testing.expectApproxEqAbs(9, mat3Det(a), 1e-12); } test "mat inverse works" { const a: Mat3F64 = .{ .row1 = Vec3F64.init(1, 2, 3), - .row2 = Vec3F64.init(4, 1, 5), - .row3 = Vec3F64.init(3, 1, 9), + .row2 = Vec3F64.init(0, 1, 5), + .row3 = Vec3F64.init(0, 0, 9), }; const ainv = mat3Inv(a); - try std.testing.expectApproxEqAbs(-4.0 / 35.0, ainv.row1.x(), 1e-12); - try std.testing.expectApproxEqAbs(3.0 / 7.0, ainv.row1.y(), 1e-12); - try std.testing.expectApproxEqAbs(-1.0 / 5.0, ainv.row1.z(), 1e-12); - try std.testing.expectApproxEqAbs(3.0 / 5.0, ainv.row2.x(), 1e-12); - try std.testing.expectApproxEqAbs(0, ainv.row2.y(), 1e-12); - try std.testing.expectApproxEqAbs(-1.0 / 5.0, ainv.row2.z(), 1e-12); - try std.testing.expectApproxEqAbs(-1.0 / 35.0, ainv.row3.x(), 1e-12); - try std.testing.expectApproxEqAbs(-1.0 / 7.0, ainv.row3.y(), 1e-12); - try std.testing.expectApproxEqAbs(1.0 / 5.0, ainv.row3.z(), 1e-12); + try std.testing.expectApproxEqAbs(1, ainv.row1.x(), 1e-12); + try std.testing.expectApproxEqAbs(-2, ainv.row1.y(), 1e-12); + try std.testing.expectApproxEqAbs(7.0 / 9.0, ainv.row1.z(), 1e-12); + try std.testing.expectApproxEqAbs(0, ainv.row2.x(), 1e-12); + try std.testing.expectApproxEqAbs(1, ainv.row2.y(), 1e-12); + try std.testing.expectApproxEqAbs(-5.0 / 9.0, ainv.row2.z(), 1e-12); + try std.testing.expectApproxEqAbs(0, ainv.row3.x(), 1e-12); + try std.testing.expectApproxEqAbs(0, ainv.row3.y(), 1e-12); + try std.testing.expectApproxEqAbs(1.0 / 9.0, ainv.row3.z(), 1e-12); } test "vec3 mul mat3 works" { const b: Mat3F64 = .{