21 float t = Vector3::computeDotProduct(pSubP1, lineDirection);
22 if (t < 0.0f) t = 0.0f;
23 if (t > lineLength) t = lineLength;
48 auto a = Vector3::computeDotProduct(d1, d1);
49 auto e = Vector3::computeDotProduct(d2, d2);
50 auto f = Vector3::computeDotProduct(d2, r);
52 if (a <= Math::EPSILON && e <= Math::EPSILON) {
60 if (a <= Math::EPSILON) {
63 t = Math::clamp(t, 0.0f, 1.0f);
65 auto c = Vector3::computeDotProduct(d1, r);
67 if (e <= Math::EPSILON) {
69 s = Math::clamp(-c / a, 0.0f, 1.0f);
72 auto b = Vector3::computeDotProduct(d1, d2);
73 auto denom = a * e - b * b;
75 s = Math::clamp((b * f - c * e) / denom, 0.0f, 1.0f);
82 s = Math::clamp(-c / a, 0.0f, 1.0f);
83 }
else if (t > 1.0f) {
85 s = Math::clamp((b - c) / a, 0.0f, 1.0f);
103 for (
auto i = 0; i < 3; i++) {
104 if (Math::abs(d[i]) < Math::EPSILON &&
105 (p[i] <= minXYZ[i] || p[i] >= maxXYZ[i])) {
108 auto odd = 1.0f / d[i];
109 auto t1 = (minXYZ[i] - p[i]) * odd;
110 auto t2 = (maxXYZ[i] - p[i]) * odd;
116 tmin = Math::max(tmin, t1);
117 tmax = Math::min(tmax, t2);
123 if (tmin > 1.0)
return false;
138 const auto& obbAxes = orientedBoundingBox->
getAxes();
139 const auto& obbCenter = orientedBoundingBox->
getCenter();
142 for (
auto i = 0; i < 3; i++) {
143 auto directionLengthOnAxis = Vector3::computeDotProduct(d, obbAxes[i]);
144 auto obbExtensionLengthOnAxis = obbHalfExtension[i];
145 auto obbCenterLengthOnAxis = Vector3::computeDotProduct(obbCenter, obbAxes[i]);
146 auto pointLengthOnAxis = Vector3::computeDotProduct(p, obbAxes[i]);
147 if (Math::abs(directionLengthOnAxis) < Math::EPSILON &&
148 (pointLengthOnAxis <= obbCenterLengthOnAxis - obbExtensionLengthOnAxis ||
149 pointLengthOnAxis >= obbCenterLengthOnAxis + obbExtensionLengthOnAxis)) {
152 auto odd = 1.0f / directionLengthOnAxis;
153 auto t1 = (obbCenterLengthOnAxis - obbExtensionLengthOnAxis - pointLengthOnAxis) * odd;
154 auto t2 = (obbCenterLengthOnAxis + obbExtensionLengthOnAxis - pointLengthOnAxis) * odd;
160 tmin = Math::max(tmin, t1);
161 tmax = Math::min(tmax, t2);
167 if (tmin > 1.0)
return false;
182 auto h = Vector3::computeCrossProduct(rayVector, edge2);
183 a = Vector3::computeDotProduct(edge1, h);
184 if (a > -Math::EPSILON && a < Math::EPSILON) {
190 u = f * Vector3::computeDotProduct(s, h);
191 if (u < 0.0 || u > 1.0) {
194 auto q = Vector3::computeCrossProduct(s, edge1);
195 v = f * Vector3::computeDotProduct(rayVector, q);
196 if (v < 0.0 || u + v > 1.0) {
200 float t = f * Vector3::computeDotProduct(edge2, q);
201 if (t > Math::EPSILON) {
203 contact = r1 + rayVector * t;
213 auto lineDirection = p2.
clone().
sub(p1);
215 lineDirection.normalize();
216 float nDotP1 = Vector3::computeDotProduct(n, p1);
217 float nDotLineDirection = Vector3::computeDotProduct(n, lineDirection);
218 auto t = ((d - nDotP1) / nDotLineDirection);
219 if (t < 0.0 || t > lineLength)
return false;
220 contact.
set(p1 + (lineDirection * t));
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
const Vector3 & getCenter() const
Line segment helper functions.
static bool doesLineSegmentCollideWithPlane(const Vector3 &n, float d, const Vector3 &p1, const Vector3 &p2, Vector3 &contact)
Does line segment collide with plane.
static void computeClosestPointsOnLineSegments(const Vector3 &p1, const Vector3 &q1, const Vector3 &p2, const Vector3 &q2, Vector3 &c1, Vector3 &c2)
Computes closest points c1, c2 on line segment p1->q1, p2->q2 based on an algorithm from "Real-Time C...
static bool doesBoundingBoxCollideWithLineSegment(BoundingBox *boundingBox, const Vector3 &p, const Vector3 &q, Vector3 &contactMin, Vector3 &contactMax)
Check if segment collides with bounding box based on an algorithm from "Real-Time Collision Detection...
static bool doesOrientedBoundingBoxCollideWithLineSegment(OrientedBoundingBox *orientedBoundingBox, const Vector3 &p, const Vector3 &q, Vector3 &contactMin, Vector3 &contactMax)
Check if segment collides with oriented bounding box based on an algorithm from "Real-Time Collision ...
static bool doesLineSegmentCollideWithTriangle(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3, const Vector3 &r1, const Vector3 &r2, Vector3 &contact)
Does line segment collides with triangle.
static bool doesLineSegmentsCollide(const Vector3 &p1, const Vector3 &q1, const Vector3 &p2, const Vector3 &q2, Vector3 &p)
Does line segments collide.
Oriented bounding box physics primitive.
const Vector3 & getHalfExtension() const
const array< Vector3, 3 > & getAxes() const
Vector3 class representing vector3 mathematical structure and operations with x, y,...
float computeLength() const
Vector3 & add(float scalar)
Adds a scalar.
const array< float, 3 > & getArray() const
Vector3 clone() const
Clones this vector3.
Vector3 & sub(float scalar)
Subtracts a scalar.
Vector3 & scale(float scalar)
Scales by scalar.
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Vector3 & normalize()
Normalizes this vector3.