16 _aabb.setCenter(0.0f, 0.0f, 0.0f);
17 _aabb.setHalfExtents(_halfExtents);
23 _aabb.setCenter(0.0f, 0.0f, 0.0f);
24 _aabb.setHalfExtents(_halfExtents);
29 _worldTransform = value;
30 _modelTransform = value.
inverse();
36 _aabb.setHalfExtents(_halfExtents);
39 bool OrientedBox::intersectsLocalAabbRay(
const Ray& localRay,
Vector3* localPoint)
const
46 float txMin = min.
getX() - origin.
getX();
47 float txMax = max.getX() - origin.
getX();
48 float tyMin = min.getY() - origin.
getY();
49 float tyMax = max.getY() - origin.
getY();
50 float tzMin = min.getZ() - origin.
getZ();
51 float tzMax = max.getZ() - origin.
getZ();
53 if (dir.
getX() == 0.0f) {
54 txMin = txMin < 0.0f ? -std::numeric_limits<float>::max() : std::numeric_limits<float>::max();
55 txMax = txMax < 0.0f ? -std::numeric_limits<float>::max() : std::numeric_limits<float>::max();
60 if (dir.
getY() == 0.0f) {
61 tyMin = tyMin < 0.0f ? -std::numeric_limits<float>::max() : std::numeric_limits<float>::max();
62 tyMax = tyMax < 0.0f ? -std::numeric_limits<float>::max() : std::numeric_limits<float>::max();
67 if (dir.
getZ() == 0.0f) {
68 tzMin = tzMin < 0.0f ? -std::numeric_limits<float>::max() : std::numeric_limits<float>::max();
69 tzMax = tzMax < 0.0f ? -std::numeric_limits<float>::max() : std::numeric_limits<float>::max();
75 const Vector3 realMin(std::min(txMin, txMax), std::min(tyMin, tyMax), std::min(tzMin, tzMax));
76 const Vector3 realMax(std::max(txMin, txMax), std::max(tyMin, tyMax), std::max(tzMin, tzMax));
78 const float minMax = std::min(std::min(realMax.getX(), realMax.getY()), realMax.getZ());
79 const float maxMin = std::max(std::max(realMin.getX(), realMin.getY()), realMin.getZ());
80 const bool intersects = minMax >= maxMin && maxMin >= 0.0f;
81 if (intersects && localPoint) {
87 bool OrientedBox::fastIntersectsLocalAabbRay(
const Ray& localRay)
const
89 const Vector3 diff = localRay.origin() - Vector3(0.0f);
90 const Vector3 absDiff(std::abs(diff.getX()), std::abs(diff.getY()), std::abs(diff.getZ()));
91 const Vector3 prod(diff.getX() * localRay.direction().getX(), diff.getY() * localRay.direction().getY(),
92 diff.getZ() * localRay.direction().getZ());
94 if (absDiff.getX() > _halfExtents.getX() && prod.getX() >= 0.0f) {
97 if (absDiff.getY() > _halfExtents.getY() && prod.getY() >= 0.0f) {
100 if (absDiff.getZ() > _halfExtents.getZ() && prod.getZ() >= 0.0f) {
104 const Vector3 absDir(std::abs(localRay.direction().getX()), std::abs(localRay.direction().getY()),
105 std::abs(localRay.direction().getZ()));
106 Vector3 cross = localRay.direction().cross(diff);
107 cross = Vector3(std::abs(cross.getX()), std::abs(cross.getY()), std::abs(cross.getZ()));
109 if (cross.getX() > _halfExtents.getY() * absDir.getZ() + _halfExtents.getZ() * absDir.getY()) {
112 if (cross.getY() > _halfExtents.getX() * absDir.getZ() + _halfExtents.getZ() * absDir.getX()) {
115 if (cross.getZ() > _halfExtents.getX() * absDir.getY() + _halfExtents.getY() * absDir.getX()) {
122 Vector3 OrientedBox::closestPointOnLocalAabb(
const Vector3& localPoint)
const
125 std::max(-_halfExtents.getX(), std::min(localPoint.getX(), _halfExtents.getX())),
126 std::max(-_halfExtents.getY(), std::min(localPoint.getY(), _halfExtents.getY())),
127 std::max(-_halfExtents.getZ(), std::min(localPoint.getZ(), _halfExtents.getZ()))
138 const bool intersects = intersectsLocalAabbRay(localRay, &localPoint);
140 *point = _worldTransform.transformPoint(localPoint);
145 return fastIntersectsLocalAabbRay(localRay);
150 const Vector3 localPoint = _modelTransform.transformPoint(point);
151 return std::abs(localPoint.
getX()) <= _halfExtents.
getX() &&
152 std::abs(localPoint.
getY()) <= _halfExtents.getY() &&
153 std::abs(localPoint.
getZ()) <= _halfExtents.getZ();
158 const Vector3 localCenter = _modelTransform.transformPoint(sphere.
center());
159 const Vector3 closest = closestPointOnLocalAabb(localCenter);
160 const Vector3 delta = localCenter - closest;
Bounding sphere defined by center and radius for intersection and containment tests.
const Vector3 & center() const
bool intersectsBoundingSphere(const BoundingSphere &sphere) const
bool containsPoint(const Vector3 &point) const
const Vector3 & halfExtents() const
const Matrix4 & worldTransform() const
void setHalfExtents(const Vector3 &halfExtents)
bool intersectsRay(const Ray &ray, Vector3 *point=nullptr) const
void setWorldTransform(const Matrix4 &value)
Infinite ray defined by origin and direction for raycasting and picking.
Ray & set(const Vector3 &origin, const Vector3 &direction)
const Vector3 & origin() const
const Vector3 & direction() const
4x4 column-major transformation matrix with SIMD acceleration.
3D vector for positions, directions, and normals with multi-backend SIMD acceleration.
Vector3 transformNormal(const Matrix4 &mat) const
float lengthSquared() const