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