Path: blob/master/thirdparty/jolt_physics/Jolt/Geometry/Sphere.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Geometry/AABox.h>78JPH_NAMESPACE_BEGIN910class [[nodiscard]] Sphere11{12public:13JPH_OVERRIDE_NEW_DELETE1415/// Constructor16inline Sphere() = default;17inline Sphere(const Float3 &inCenter, float inRadius) : mCenter(inCenter), mRadius(inRadius) { }18inline Sphere(Vec3Arg inCenter, float inRadius) : mRadius(inRadius) { inCenter.StoreFloat3(&mCenter); }1920/// Calculate the support vector for this convex shape.21inline Vec3 GetSupport(Vec3Arg inDirection) const22{23float length = inDirection.Length();24return length > 0.0f ? Vec3::sLoadFloat3Unsafe(mCenter) + (mRadius/ length) * inDirection : Vec3::sLoadFloat3Unsafe(mCenter);25}2627// Properties28inline Vec3 GetCenter() const { return Vec3::sLoadFloat3Unsafe(mCenter); }29inline float GetRadius() const { return mRadius; }3031/// Test if two spheres overlap32inline bool Overlaps(const Sphere &inB) const33{34return (Vec3::sLoadFloat3Unsafe(mCenter) - Vec3::sLoadFloat3Unsafe(inB.mCenter)).LengthSq() <= Square(mRadius + inB.mRadius);35}3637/// Check if this sphere overlaps with a box38inline bool Overlaps(const AABox &inOther) const39{40return inOther.GetSqDistanceTo(GetCenter()) <= Square(mRadius);41}4243/// Create the minimal sphere that encapsulates this sphere and inPoint44inline void EncapsulatePoint(Vec3Arg inPoint)45{46// Calculate distance between point and center47Vec3 center = GetCenter();48Vec3 d_vec = inPoint - center;49float d_sq = d_vec.LengthSq();50if (d_sq > Square(mRadius))51{52// It is further away than radius, we need to widen the sphere53// The diameter of the new sphere is radius + d, so the new radius is half of that54float d = sqrt(d_sq);55float radius = 0.5f * (mRadius + d);5657// The center needs to shift by new radius - old radius in the direction of d58center += (radius - mRadius) / d * d_vec;5960// Store new sphere61center.StoreFloat3(&mCenter);62mRadius = radius;63}64}6566private:67Float3 mCenter;68float mRadius;69};7071JPH_NAMESPACE_END727374