summaryrefslogtreecommitdiff
path: root/physics
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2024-12-16 22:22:09 +0100
committereug-vs <eugene@eug-vs.xyz>2024-12-16 22:22:09 +0100
commit9a19677c53dd8d3735027d03c6bc923b3fd9c3e6 (patch)
tree0fdc2b6c65b5d9a5f9ecf767351c55cd709d1659 /physics
parent4b977589d11427d1c88b85e1c7ed896849573d76 (diff)
downloadparticle-physics-9a19677c53dd8d3735027d03c6bc923b3fd9c3e6.tar.gz
fix: compute Jacobian by probing in both +- directions
Diffstat (limited to 'physics')
-rw-r--r--physics/src/constraint/mod.rs11
1 files changed, 5 insertions, 6 deletions
diff --git a/physics/src/constraint/mod.rs b/physics/src/constraint/mod.rs
index e9997f5..5c5034c 100644
--- a/physics/src/constraint/mod.rs
+++ b/physics/src/constraint/mod.rs
@@ -21,19 +21,18 @@ pub trait Constraint {
fn partial_derivative(&self, q: &DVector<Scalar>) -> RowDVector<Scalar> {
let dq = 0.00001;
- let c_original = self.c(&q);
let mut result = RowDVector::zeros(q.len());
// The only non-zero components of derivative vector are
// the particles that this constraint affects
for particle_id in self.get_particles() {
for i in 0..N {
let index = particle_id * N + i;
- let mut q_partial = q.clone();
- q_partial[index] += dq;
+ let mut a = q.clone();
+ let mut b = q.clone();
+ a[index] += dq;
+ b[index] -= dq;
- let c = self.c(&q_partial);
-
- result[index] = (c - c_original) / dq
+ result[index] = (self.c(&a) - self.c(&b)) / (2.0 * dq)
}
}