TDME2 1.9.121
Capsule.cpp
Go to the documentation of this file.
2
3#include <ext/reactphysics3d/src/collision/shapes/CapsuleShape.h>
4
5#include <tdme/tdme.h>
8#include <tdme/math/Math.h>
10#include <tdme/math/Vector3.h>
11
13
19
20Capsule::Capsule(const Vector3& a, const Vector3& b, float radius, const Vector3& scale)
21{
22 this->a.set(a);
23 this->b.set(b);
24 this->radius = radius;
25 center.set(a).add(b).scale(0.5f);
27}
28
29float Capsule::getRadius() const
30{
31 return radius;
32}
33
34const Vector3& Capsule::getA() const
35{
36 return a;
37}
38
39const Vector3& Capsule::getB() const
40{
41 return b;
42}
43
44void Capsule::setScale(const Vector3& scale) {
45 // store new scale
46 this->scale.set(scale);
47
48 // remove old collision shape
49 if (collisionShape != nullptr) delete collisionShape;
50
51 //
52 Vector3 aScaled;
53 Vector3 bScaled;
54 aScaled.set(a).scale(scale);
55 bScaled.set(b).scale(scale);
56
57 // determine local rotation around Y axis
58 Vector3 yAxis(0.0f, -1.0f, 0.0f);
59 Vector3 abNormalized = aScaled.clone().sub(bScaled).normalize();
60 auto& abNormalizedVectorXYZ = abNormalized.getArray();
61 Vector3 rotationAxis;
62 if (Math::abs(abNormalizedVectorXYZ[0]) < Math::EPSILON && Math::abs(abNormalizedVectorXYZ[2]) < Math::EPSILON) {
63 rotationAxis.set(abNormalizedVectorXYZ[1], 0.0f, 0.0f);
64 } else {
65 rotationAxis = Vector3::computeCrossProduct(yAxis, abNormalized).normalize();
66 }
67 auto angle = Vector3::computeAngle(yAxis, abNormalized, yAxis);
68 Quaternion rotationQuaternion;
69 rotationQuaternion.rotate(rotationAxis, angle);
70
71 // set shape orientation
72 collisionShapeLocalTransform.setOrientation(
73 reactphysics3d::Quaternion(
74 rotationQuaternion.getX(),
75 rotationQuaternion.getY(),
76 rotationQuaternion.getZ(),
77 rotationQuaternion.getW()
78 )
79 );
80
81 // determine local translation
84 reactphysics3d::Vector3(
88 )
89 );
90
91 // create capsule
92 collisionShape = new reactphysics3d::CapsuleShape(
93 Math::max(Math::EPSILON, radius * Math::max(Math::abs(scale.getZ()), Math::max(Math::abs(scale.getX()), Math::abs(scale.getY())))),
94 Math::max(Math::EPSILON, bScaled.clone().sub(aScaled).computeLength())
95 );
96
97 // compute bounding box
99}
100
102{
103 return new Capsule(a, b, radius, scale);
104}
Transformations which contain scale, rotations and translation.
reactphysics3d::Transform collisionShapeLocalTransform
reactphysics3d::CollisionShape * collisionShape
void computeBoundingBox()
Compute bounding box.
Capsule physics primitive.
Definition: Capsule.h:18
const Vector3 & getB() const
Definition: Capsule.cpp:39
void setScale(const Vector3 &scale) override
Set local scale.
Definition: Capsule.cpp:44
const Vector3 & getA() const
Definition: Capsule.cpp:34
BoundingVolume * clone() const override
Clones this bounding volume.
Definition: Capsule.cpp:101
Standard math functions.
Definition: Math.h:21
4x4 3D Matrix class
Definition: Matrix4x4.h:24
Quaternion class.
Definition: Quaternion.h:22
Quaternion & rotate(const Vector3 &axis, float angle)
Creates a rotation quaternion.
Definition: Quaternion.h:196
float getY() const
Definition: Quaternion.h:119
float getX() const
Definition: Quaternion.h:102
float getZ() const
Definition: Quaternion.h:136
float getW() const
Definition: Quaternion.h:153
3D vector 3 class
Definition: Vector3.h:22
float getY() const
Definition: Vector3.h:119
float getX() const
Definition: Vector3.h:103
float computeLength() const
Definition: Vector3.h:202
float getZ() const
Definition: Vector3.h:136
Vector3 & normalize()
Normalize the vector.
Definition: Vector3.h:288
Vector3 & set(float x, float y, float z)
Set up vector.
Definition: Vector3.h:73
Vector3 clone() const
Clones the vector.
Definition: Vector3.h:372
Vector3 & sub(const Vector3 &v)
Subtracts a vector.
Definition: Vector3.h:325
Vector3 & add(const Vector3 &v)
Adds a vector.
Definition: Vector3.h:301
Vector3 & scale(float scale)
Scale this vector.
Definition: Vector3.h:349
array< float, 3 > & getArray() const
Definition: Vector3.h:171