1 /*
2 Copyright (c) 2014-2020 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 dagon.ext.physics.pcm;
30 
31 import dlib.math.vector;
32 
33 import dagon.ext.physics.contact;
34 import dagon.ext.physics.rigidbody;
35 
36 /*
37  * Persistent contact manifold.
38  * Stores information about two bodies' collision.
39  * Contacts are collected incrementally
40  * (for all-at-once solution see cm.d).
41  *
42  * TODO: use squared distance in methods
43  * TODO: overflow handling
44  */
45 struct PersistentContactManifold
46 {
47     Contact[4] contacts;
48     uint numContacts = 0;
49 
50     void addContact(Contact c)
51     {
52         bool farEnough = true;
53 
54         for (uint i = 0; i < numContacts; i++)
55         {
56             float d = distance(c.point, contacts[i].point);
57             farEnough = farEnough && (d > 0.1f); //0.1f
58         }
59 
60         if (farEnough)
61             append(c);
62     }
63 
64     void update()
65     {
66         for (uint i = 0; i < numContacts; )
67         {
68             auto c = &contacts[i];
69 
70             Vector3f p1, p2;
71 
72             if (c.body1.dynamic)
73                 p1 = c.shape1RelPoint + c.shape1.position;
74             else
75                 p1 = c.shape1RelPoint + c.shape1pos;
76                 
77             if (c.body2.dynamic)
78                 p2 = c.shape2RelPoint + c.shape2.position;
79             else
80                 p2 = c.shape2RelPoint + c.shape2pos;
81 
82             float d = distance(p1, p2);
83 
84             if (d > 0.15f) //0.14f
85             {
86                 this.removeContact(i);
87             }
88             else
89             {
90                 c.point = (p1 + p2) * 0.5f;
91                 i++;
92             }
93         }
94     }
95 
96     void append(Contact c)
97     {
98         if (numContacts < contacts.length)
99         {
100             contacts[numContacts] = c;
101             numContacts++;
102         }
103     }
104 
105     void removeContact(uint n)
106     {
107         uint i = 0;
108         for (uint j = 0; j < n; j++)
109         {
110             if (j != n)
111                 contacts[i++] = contacts[j];
112         }
113         numContacts--;
114     }
115 }
116