| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <AnKi/Collision/Common.h>
- namespace anki {
- /// @addtogroup collision
- /// @{
- /// Sphere collision shape.
- class Sphere
- {
- public:
- static constexpr CollisionShapeType kClassType = CollisionShapeType::kSphere;
- /// Will not initialize any memory, nothing.
- Sphere()
- {
- }
- /// Copy constructor
- Sphere(const Sphere& b)
- {
- operator=(b);
- }
- /// Constructor
- Sphere(const Vec4& center, F32 radius)
- : m_center(center)
- , m_radius(radius)
- {
- check();
- }
- /// Constructor
- Sphere(const Vec3& center, F32 radius)
- : m_center(center.xyz0)
- , m_radius(radius)
- {
- check();
- }
- /// Set from point cloud.
- Sphere(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize)
- {
- setFromPointCloud(pointBuffer, pointCount, pointStride, buffSize);
- }
- Sphere& operator=(const Sphere& b)
- {
- b.check();
- m_center = b.m_center;
- m_radius = b.m_radius;
- return *this;
- }
- const Vec4& getCenter() const
- {
- check();
- return m_center;
- }
- void setCenter(const Vec4& x)
- {
- ANKI_ASSERT(x.w == 0.0f);
- m_center = x;
- }
- void setCenter(const Vec3& x)
- {
- m_center = x.xyz0;
- }
- F32 getRadius() const
- {
- check();
- return m_radius;
- }
- void setRadius(const F32 x)
- {
- ANKI_ASSERT(x > 0.0f);
- m_radius = x;
- }
- /// Calculate from a set of points
- void setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, PtrSize buffSize);
- Sphere getTransformed(const Transform& transform) const
- {
- check();
- Sphere out;
- out.m_center = transform.transform(m_center);
- ANKI_ASSERT(transform.hasUniformScale());
- out.m_radius = m_radius * transform.getScale().x;
- return out;
- }
- /// Get the sphere that includes this sphere and the given. See a drawing in the docs dir for more info about the
- /// algorithm
- Sphere getCompoundShape(const Sphere& b) const;
- /// Compute the GJK support.
- Vec4 computeSupport(const Vec4& dir) const
- {
- return m_center + dir.normalize() * m_radius;
- }
- private:
- Vec4 m_center
- #if ANKI_ASSERTIONS_ENABLED
- = Vec4(kMaxF32)
- #endif
- ;
- F32 m_radius
- #if ANKI_ASSERTIONS_ENABLED
- = -1.0f
- #endif
- ;
- void check() const
- {
- ANKI_ASSERT(m_center.w == 0.0f);
- ANKI_ASSERT(m_radius > 0.0f);
- }
- };
- /// @}
- } // end namespace anki
|