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.shape; 30 31 import dlib.core.memory; 32 import dlib.math.vector; 33 import dlib.math.matrix; 34 import dlib.math.transformation; 35 import dlib.geometry.aabb; 36 import dlib.geometry.sphere; 37 38 import dmech.geometry; 39 40 /* 41 * ShapeComponent is a proxy object between RigidBody and Geometry. 42 * It stores non-geometric information such as mass contribution, 43 * position in body space and a unique identifier for indexing in 44 * contact cache. 45 * One Geometry can be shared between multiple ShapeComponents. 46 */ 47 48 class ShapeComponent: Freeable 49 { 50 Geometry geometry; // geometry 51 Vector3f centroid; // position in body space 52 float mass; // mass contribution 53 uint id = 0; // global identifier 54 55 Matrix4x4f _transformation; 56 57 bool locked = false; 58 59 bool active = true; 60 bool solve = true; 61 bool raycastable = true; 62 int numCollisions = 0; 63 64 @property 65 { 66 Matrix4x4f transformation() 67 { 68 while(locked) {} 69 return _transformation; 70 } 71 72 void transformation(Matrix4x4f m) 73 { 74 locked = true; 75 _transformation = m; 76 locked = false; 77 } 78 } 79 80 this(Geometry g, Vector3f c, float m) 81 { 82 geometry = g; 83 centroid = c; 84 mass = m; 85 86 _transformation = Matrix4x4f.identity; 87 } 88 89 // position in world space 90 @property Vector3f position() 91 { 92 return _transformation.translation; 93 } 94 95 @property AABB boundingBox() 96 { 97 return geometry.boundingBox( 98 _transformation.translation); 99 } 100 101 @property Sphere boundingSphere() 102 { 103 AABB aabb = geometry.boundingBox( 104 _transformation.translation); 105 return Sphere(aabb.center, aabb.size.length); 106 } 107 108 Vector3f supportPointGlobal(Vector3f dir) 109 { 110 Vector3f result; 111 Matrix4x4f* m = &_transformation; 112 113 result.x = ((dir.x * m.a11) + (dir.y * m.a21)) + (dir.z * m.a31); 114 result.y = ((dir.x * m.a12) + (dir.y * m.a22)) + (dir.z * m.a32); 115 result.z = ((dir.x * m.a13) + (dir.y * m.a23)) + (dir.z * m.a33); 116 117 result = geometry.supportPoint(result); 118 119 float x = ((result.x * m.a11) + (result.y * m.a12)) + (result.z * m.a13); 120 float y = ((result.x * m.a21) + (result.y * m.a22)) + (result.z * m.a23); 121 float z = ((result.x * m.a31) + (result.y * m.a32)) + (result.z * m.a33); 122 123 result.x = m.a14 + x; 124 result.y = m.a24 + y; 125 result.z = m.a34 + z; 126 127 return result; 128 } 129 130 void free() 131 { 132 Delete(this); 133 } 134 }