1 /*
2 Copyright (c) 2013-2014 Timur Gafarov 
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dmech.solver;
30 
31 import std.math;
32 import std.algorithm;
33 
34 import dlib.math.vector;
35 import dlib.math.utils;
36 
37 import dmech.rigidbody;
38 import dmech.contact;
39 
40 void prepareContact(Contact* c)
41 {
42     RigidBody body1 = c.body1;
43     RigidBody body2 = c.body2;
44     
45     Vector3f r1 = c.body1RelPoint;
46     Vector3f r2 = c.body2RelPoint;
47     
48     Vector3f relativeVelocity = Vector3f(0.0f, 0.0f, 0.0f);
49     relativeVelocity += body1.linearVelocity + cross(body1.angularVelocity, r1);
50     relativeVelocity -= body2.linearVelocity + cross(body2.angularVelocity, r2);
51     c.initialVelocityProjection = dot(relativeVelocity, c.normal);
52 
53     c.n1 = c.normal;
54     c.w1 = c.normal.cross(r1);
55     c.n2 = -c.normal;
56     c.w2 = -c.normal.cross(r2);
57 
58     c.effectiveMass = 
59         body1.invMass + 
60         body2.invMass +
61         dot(c.w1 * body1.invInertiaTensor, c.w1) +
62         dot(c.w2 * body2.invInertiaTensor, c.w2);
63 }
64 
65 void solveContact(Contact* c, double dt)
66 {
67     RigidBody body1 = c.body1;
68     RigidBody body2 = c.body2;
69     
70     Vector3f r1 = c.body1RelPoint;
71     Vector3f r2 = c.body2RelPoint;
72 
73     Vector3f relativeVelocity = Vector3f(0.0f, 0.0f, 0.0f);
74     relativeVelocity += body1.linearVelocity + cross(body1.angularVelocity, r1);
75     relativeVelocity -= body2.linearVelocity + cross(body2.angularVelocity, r2);
76     float velocityProjection = dot(relativeVelocity, c.normal);
77 
78     // Check if the bodies are already moving apart
79     if (velocityProjection > 0.0f)
80         return;
81 
82     float bounce = (body1.bounce + body2.bounce) * 0.5f;
83     float damping = 0.9f;
84     float C = max(0, -bounce * c.initialVelocityProjection - damping);
85 
86     float bias = 0.0f;
87 
88     // Velocity-based position correction
89   /*
90     float allowedPenetration = 0.01f;
91     float biasFactor = 0.3f; // 0.1 to 0.3
92     bias = biasFactor * (1.0f / dt) * max(0.0f, c.penetration - allowedPenetration);
93   */
94     float a = velocityProjection;
95     float b = c.effectiveMass;
96 
97     float normalImpulse = (C - a + bias) / b;
98 
99     //if (normalImpulse < 0.0f)
100     //    normalImpulse = 0.0f;
101 
102     // Friction
103     float mu = (body1.friction + body2.friction) * 0.5f;
104     Vector3f fVec = Vector3f(0.0f, 0.0f, 0.0f);
105 
106     Vector3f tn1 = c.fdir1;
107     Vector3f tw1 = c.fdir1.cross(r1);
108     Vector3f tn2 = -c.fdir1;
109     Vector3f tw2 = -c.fdir1.cross(r2);
110     float ta = dot(relativeVelocity, c.fdir1);
111     float tb = dot(tn1, tn1 * body1.invMass)
112              + dot(tw1, tw1 * body1.invInertiaTensor)
113              + dot(tn2, tn2 * body2.invMass)
114              + dot(tw2, tw2 * body2.invInertiaTensor);
115     float fImpulse1 = -ta / tb;
116     fImpulse1 = clamp(fImpulse1, -normalImpulse * mu, normalImpulse * mu);
117 
118     tn1 = c.fdir2;
119     tw1 = c.fdir2.cross(r1);
120     tn2 = -c.fdir2;
121     tw2 = -c.fdir2.cross(r2);
122     ta = dot(relativeVelocity, c.fdir2);
123     tb = dot(tn1, tn1 * body1.invMass)
124        + dot(tw1, tw1 * body1.invInertiaTensor)
125        + dot(tn2, tn2 * body2.invMass)
126        + dot(tw2, tw2 * body2.invInertiaTensor);
127     float fImpulse2 = -ta / tb;
128     fImpulse2 = clamp(fImpulse2, -normalImpulse * mu, normalImpulse * mu);
129 
130     c.accumulatedfImpulse1 += fImpulse1;
131     c.accumulatedfImpulse2 += fImpulse2;
132 
133     fVec = c.fdir1 * fImpulse1 + c.fdir2 * fImpulse2;
134 
135     Vector3f impulseVec = c.normal * normalImpulse;
136 
137     impulseVec += fVec;
138 
139     body1.applyImpulse(+impulseVec, c.point);
140     body2.applyImpulse(-impulseVec, c.point);
141 }
142 
143 void solvePositionError(Contact* c, uint numContacts)
144 {
145     RigidBody body1 = c.body1;
146     RigidBody body2 = c.body2;
147     
148     Vector3f r1 = c.body1RelPoint;
149     Vector3f r2 = c.body2RelPoint;
150        
151     Vector3f prv = Vector3f(0.0f, 0.0f, 0.0f);
152     prv += body1.pseudoLinearVelocity + cross(body1.pseudoAngularVelocity, r1);
153     prv -= body2.pseudoLinearVelocity + cross(body2.pseudoAngularVelocity, r2);
154     float pvp = dot(prv, c.normal);
155 
156     if (c.penetration <= 0.0f)
157         return;
158     
159     float ERP = (1.0f / numContacts) * 0.99f;
160     float pc = c.penetration * ERP;
161     c.penetration -= pc;
162     
163     if (pvp >= pc)
164         return;
165 
166     float a = pvp;
167     float impulse = (pc - a) / c.effectiveMass;
168 
169     Vector3f impulseVec = c.normal * impulse;
170    
171     body1.applyPseudoImpulse(+impulseVec, c.point);
172     body2.applyPseudoImpulse(-impulseVec, c.point);
173 }
174