1 /*************************************************************************/
2 /*  btRayShape.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 "btRayShape.h"
32 
33 #include "core/math/math_funcs.h"
34 
35 #include <LinearMath/btAabbUtil2.h>
36 
37 /**
38 	@author AndreaCatania
39 */
40 
btRayShape(btScalar length)41 btRayShape::btRayShape(btScalar length) :
42 		btConvexInternalShape(),
43 		m_shapeAxis(0, 0, 1) {
44 	m_shapeType = CUSTOM_CONVEX_SHAPE_TYPE;
45 	setLength(length);
46 }
47 
~btRayShape()48 btRayShape::~btRayShape() {
49 }
50 
setLength(btScalar p_length)51 void btRayShape::setLength(btScalar p_length) {
52 
53 	m_length = p_length;
54 	reload_cache();
55 }
56 
setMargin(btScalar margin)57 void btRayShape::setMargin(btScalar margin) {
58 	btConvexInternalShape::setMargin(margin);
59 	reload_cache();
60 }
61 
setSlipsOnSlope(bool p_slipsOnSlope)62 void btRayShape::setSlipsOnSlope(bool p_slipsOnSlope) {
63 
64 	slipsOnSlope = p_slipsOnSlope;
65 }
66 
localGetSupportingVertex(const btVector3 & vec) const67 btVector3 btRayShape::localGetSupportingVertex(const btVector3 &vec) const {
68 	return localGetSupportingVertexWithoutMargin(vec) + (m_shapeAxis * m_collisionMargin);
69 }
70 
localGetSupportingVertexWithoutMargin(const btVector3 & vec) const71 btVector3 btRayShape::localGetSupportingVertexWithoutMargin(const btVector3 &vec) const {
72 	if (vec.z() > 0)
73 		return m_shapeAxis * m_cacheScaledLength;
74 	else
75 		return btVector3(0, 0, 0);
76 }
77 
batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 * vectors,btVector3 * supportVerticesOut,int numVectors) const78 void btRayShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const {
79 	for (int i = 0; i < numVectors; ++i) {
80 		supportVerticesOut[i] = localGetSupportingVertexWithoutMargin(vectors[i]);
81 	}
82 }
83 
getAabb(const btTransform & t,btVector3 & aabbMin,btVector3 & aabbMax) const84 void btRayShape::getAabb(const btTransform &t, btVector3 &aabbMin, btVector3 &aabbMax) const {
85 	btVector3 localAabbMin(0, 0, 0);
86 	btVector3 localAabbMax(m_shapeAxis * m_cacheScaledLength);
87 	btTransformAabb(localAabbMin, localAabbMax, m_collisionMargin, t, aabbMin, aabbMax);
88 }
89 
calculateLocalInertia(btScalar mass,btVector3 & inertia) const90 void btRayShape::calculateLocalInertia(btScalar mass, btVector3 &inertia) const {
91 	inertia.setZero();
92 }
93 
getNumPreferredPenetrationDirections() const94 int btRayShape::getNumPreferredPenetrationDirections() const {
95 	return 0;
96 }
97 
getPreferredPenetrationDirection(int index,btVector3 & penetrationVector) const98 void btRayShape::getPreferredPenetrationDirection(int index, btVector3 &penetrationVector) const {
99 	penetrationVector.setZero();
100 }
101 
reload_cache()102 void btRayShape::reload_cache() {
103 
104 	m_cacheScaledLength = m_length * m_localScaling[2];
105 
106 	m_cacheSupportPoint.setIdentity();
107 	m_cacheSupportPoint.setOrigin(m_shapeAxis * m_cacheScaledLength);
108 }
109