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