Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Sphere.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
10
11class [[nodiscard]] Sphere
12{
13public:
15
17 inline Sphere() = default;
18 inline Sphere(const Float3 &inCenter, float inRadius) : mCenter(inCenter), mRadius(inRadius) { }
19 inline Sphere(Vec3Arg inCenter, float inRadius) : mRadius(inRadius) { inCenter.StoreFloat3(&mCenter); }
20
22 inline Vec3 GetSupport(Vec3Arg inDirection) const
23 {
24 float length = inDirection.Length();
25 return length > 0.0f ? Vec3::sLoadFloat3Unsafe(mCenter) + (mRadius/ length) * inDirection : Vec3::sLoadFloat3Unsafe(mCenter);
26 }
27
28 // Properties
29 inline Vec3 GetCenter() const { return Vec3::sLoadFloat3Unsafe(mCenter); }
30 inline float GetRadius() const { return mRadius; }
31
33 inline bool Overlaps(const Sphere &inB) const
34 {
35 return (Vec3::sLoadFloat3Unsafe(mCenter) - Vec3::sLoadFloat3Unsafe(inB.mCenter)).LengthSq() <= Square(mRadius + inB.mRadius);
36 }
37
39 inline bool Overlaps(const AABox &inOther) const
40 {
41 return inOther.GetSqDistanceTo(GetCenter()) <= Square(mRadius);
42 }
43
45 inline void EncapsulatePoint(Vec3Arg inPoint)
46 {
47 // Calculate distance between point and center
48 Vec3 center = GetCenter();
49 Vec3 d_vec = inPoint - center;
50 float d_sq = d_vec.LengthSq();
51 if (d_sq > Square(mRadius))
52 {
53 // It is further away than radius, we need to widen the sphere
54 // The diameter of the new sphere is radius + d, so the new radius is half of that
55 float d = sqrt(d_sq);
56 float radius = 0.5f * (mRadius + d);
57
58 // The center needs to shift by new radius - old radius in the direction of d
59 center += (radius - mRadius) / d * d_vec;
60
61 // Store new sphere
62 center.StoreFloat3(&mCenter);
63 mRadius = radius;
64 }
65 }
66
67private:
68 Float3 mCenter;
69 float mRadius;
70};
71
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
constexpr T Square(T inV)
Square a value.
Definition: Math.h:52
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
Axis aligned box.
Definition: AABox.h:16
float GetSqDistanceTo(Vec3Arg inPoint) const
Get the squared distance between inPoint and this box (will be 0 if in Point is inside the box)
Definition: AABox.h:294
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition: Float3.h:13
Definition: Sphere.h:12
bool Overlaps(const AABox &inOther) const
Check if this sphere overlaps with a box.
Definition: Sphere.h:39
float GetRadius() const
Definition: Sphere.h:30
JPH_OVERRIDE_NEW_DELETE Sphere()=default
Constructor.
Vec3 GetSupport(Vec3Arg inDirection) const
Calculate the support vector for this convex shape.
Definition: Sphere.h:22
Vec3 GetCenter() const
Definition: Sphere.h:29
bool Overlaps(const Sphere &inB) const
Test if two spheres overlap.
Definition: Sphere.h:33
Sphere(Vec3Arg inCenter, float inRadius)
Definition: Sphere.h:19
Sphere(const Float3 &inCenter, float inRadius)
Definition: Sphere.h:18
void EncapsulatePoint(Vec3Arg inPoint)
Create the minimal sphere that encapsulates this sphere and inPoint.
Definition: Sphere.h:45
Definition: Vec3.h:16
JPH_INLINE float Length() const
Length of vector.
Definition: Vec3.inl:669
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store 3 floats to memory.
Definition: Vec3.inl:757
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition: Vec3.inl:653
static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (reads 32 bits extra which it doesn't use)
Definition: Vec3.inl:134