1 /*************************************************************************/
2 /*  godot_collision_configuration.cpp                                    */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "godot_collision_configuration.h"
32 
33 #include "godot_ray_world_algorithm.h"
34 
35 #include <BulletCollision/BroadphaseCollision/btBroadphaseProxy.h>
36 #include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
37 
38 /**
39 	@author AndreaCatania
40 */
41 
GodotCollisionConfiguration(const btDiscreteDynamicsWorld * world,const btDefaultCollisionConstructionInfo & constructionInfo)42 GodotCollisionConfiguration::GodotCollisionConfiguration(const btDiscreteDynamicsWorld *world, const btDefaultCollisionConstructionInfo &constructionInfo) :
43 		btDefaultCollisionConfiguration(constructionInfo) {
44 
45 	void *mem = NULL;
46 
47 	mem = btAlignedAlloc(sizeof(GodotRayWorldAlgorithm::CreateFunc), 16);
48 	m_rayWorldCF = new (mem) GodotRayWorldAlgorithm::CreateFunc(world);
49 
50 	mem = btAlignedAlloc(sizeof(GodotRayWorldAlgorithm::SwappedCreateFunc), 16);
51 	m_swappedRayWorldCF = new (mem) GodotRayWorldAlgorithm::SwappedCreateFunc(world);
52 }
53 
~GodotCollisionConfiguration()54 GodotCollisionConfiguration::~GodotCollisionConfiguration() {
55 	m_rayWorldCF->~btCollisionAlgorithmCreateFunc();
56 	btAlignedFree(m_rayWorldCF);
57 
58 	m_swappedRayWorldCF->~btCollisionAlgorithmCreateFunc();
59 	btAlignedFree(m_swappedRayWorldCF);
60 }
61 
getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1)62 btCollisionAlgorithmCreateFunc *GodotCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0, int proxyType1) {
63 
64 	if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0 && CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
65 
66 		// This collision is not supported
67 		return m_emptyCreateFunc;
68 	} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0) {
69 
70 		return m_rayWorldCF;
71 	} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
72 
73 		return m_swappedRayWorldCF;
74 	} else {
75 
76 		return btDefaultCollisionConfiguration::getCollisionAlgorithmCreateFunc(proxyType0, proxyType1);
77 	}
78 }
79 
getClosestPointsAlgorithmCreateFunc(int proxyType0,int proxyType1)80 btCollisionAlgorithmCreateFunc *GodotCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(int proxyType0, int proxyType1) {
81 
82 	if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0 && CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
83 
84 		// This collision is not supported
85 		return m_emptyCreateFunc;
86 	} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0) {
87 
88 		return m_rayWorldCF;
89 	} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
90 
91 		return m_swappedRayWorldCF;
92 	} else {
93 
94 		return btDefaultCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(proxyType0, proxyType1);
95 	}
96 }
97 
GodotSoftCollisionConfiguration(const btDiscreteDynamicsWorld * world,const btDefaultCollisionConstructionInfo & constructionInfo)98 GodotSoftCollisionConfiguration::GodotSoftCollisionConfiguration(const btDiscreteDynamicsWorld *world, const btDefaultCollisionConstructionInfo &constructionInfo) :
99 		btSoftBodyRigidBodyCollisionConfiguration(constructionInfo) {
100 
101 	void *mem = NULL;
102 
103 	mem = btAlignedAlloc(sizeof(GodotRayWorldAlgorithm::CreateFunc), 16);
104 	m_rayWorldCF = new (mem) GodotRayWorldAlgorithm::CreateFunc(world);
105 
106 	mem = btAlignedAlloc(sizeof(GodotRayWorldAlgorithm::SwappedCreateFunc), 16);
107 	m_swappedRayWorldCF = new (mem) GodotRayWorldAlgorithm::SwappedCreateFunc(world);
108 }
109 
~GodotSoftCollisionConfiguration()110 GodotSoftCollisionConfiguration::~GodotSoftCollisionConfiguration() {
111 	m_rayWorldCF->~btCollisionAlgorithmCreateFunc();
112 	btAlignedFree(m_rayWorldCF);
113 
114 	m_swappedRayWorldCF->~btCollisionAlgorithmCreateFunc();
115 	btAlignedFree(m_swappedRayWorldCF);
116 }
117 
getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1)118 btCollisionAlgorithmCreateFunc *GodotSoftCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0, int proxyType1) {
119 
120 	if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0 && CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
121 
122 		// This collision is not supported
123 		return m_emptyCreateFunc;
124 	} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0) {
125 
126 		return m_rayWorldCF;
127 	} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
128 
129 		return m_swappedRayWorldCF;
130 	} else {
131 
132 		return btSoftBodyRigidBodyCollisionConfiguration::getCollisionAlgorithmCreateFunc(proxyType0, proxyType1);
133 	}
134 }
135 
getClosestPointsAlgorithmCreateFunc(int proxyType0,int proxyType1)136 btCollisionAlgorithmCreateFunc *GodotSoftCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(int proxyType0, int proxyType1) {
137 
138 	if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0 && CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
139 
140 		// This collision is not supported
141 		return m_emptyCreateFunc;
142 	} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType0) {
143 
144 		return m_rayWorldCF;
145 	} else if (CUSTOM_CONVEX_SHAPE_TYPE == proxyType1) {
146 
147 		return m_swappedRayWorldCF;
148 	} else {
149 
150 		return btSoftBodyRigidBodyCollisionConfiguration::getClosestPointsAlgorithmCreateFunc(proxyType0, proxyType1);
151 	}
152 }
153