Collider.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_COLLIDER_HPP
12 #define CUBBYFLOW_COLLIDER_HPP
13 
15 
16 #include <functional>
17 
18 namespace CubbyFlow
19 {
29 template <size_t N>
30 class Collider
31 {
32  public:
39  using OnBeginUpdateCallback =
40  std::function<void(Collider*, double, double)>;
41 
43  Collider() = default;
44 
46  virtual ~Collider() = default;
47 
49  Collider(const Collider& other) = default;
50 
52  Collider(Collider&& other) noexcept = default;
53 
55  Collider& operator=(const Collider& other) = default;
56 
58  Collider& operator=(Collider&& other) noexcept = default;
59 
61  [[nodiscard]] virtual Vector<double, N> VelocityAt(
62  const Vector<double, N>& point) const = 0;
63 
72  void ResolveCollision(double radius, double restitutionCoefficient,
73  Vector<double, N>* position,
74  Vector<double, N>* velocity);
75 
77  [[nodiscard]] double GetFrictionCoefficient() const;
78 
85  void SetFrictionCoefficient(double newFrictionCoefficient);
86 
88  [[nodiscard]] const std::shared_ptr<Surface<N>>& GetSurface() const;
89 
91  void Update(double currentTimeInSeconds, double timeIntervalInSeconds);
92 
103  void SetOnBeginUpdateCallback(const OnBeginUpdateCallback& callback);
104 
105  protected:
107  struct ColliderQueryResult final
108  {
109  double distance = 0.0;
113  };
114 
116  void SetSurface(const std::shared_ptr<Surface<N>>& newSurface);
117 
119  void GetClosestPoint(const std::shared_ptr<Surface<N>>& surface,
120  const Vector<double, N>& queryPoint,
121  ColliderQueryResult* result) const;
122 
124  [[nodiscard]] bool IsPenetrating(const ColliderQueryResult& colliderPoint,
125  const Vector<double, N>& position,
126  double radius);
127 
128  private:
129  std::shared_ptr<Surface<N>> m_surface;
130  double m_frictionCoefficient = 0.0;
131  OnBeginUpdateCallback m_onUpdateCallback;
132 };
133 
136 
139 
141 using Collider2Ptr = std::shared_ptr<Collider2>;
142 
144 using Collider3Ptr = std::shared_ptr<Collider3>;
145 } // namespace CubbyFlow
146 
147 #endif
Collider & operator=(const Collider &other)=default
Default copy assignment operator.
std::shared_ptr< Collider3 > Collider3Ptr
Shared pointer type for the Collider3.
Definition: Collider.hpp:144
Collider()=default
Default constructor.
std::shared_ptr< Collider2 > Collider2Ptr
Shared pointer type for the Collider2.
Definition: Collider.hpp:141
std::function< void(Collider *, double, double)> OnBeginUpdateCallback
Callback function type for update calls.
Definition: Collider.hpp:40
Vector< double, N > velocity
Definition: Collider.hpp:112
void GetClosestPoint(const std::shared_ptr< Surface< N >> &surface, const Vector< double, N > &queryPoint, ColliderQueryResult *result) const
Outputs closest point&#39;s information.
bool IsPenetrating(const ColliderQueryResult &colliderPoint, const Vector< double, N > &position, double radius)
Returns true if given point is in the opposite side of the surface.
void ResolveCollision(double radius, double restitutionCoefficient, Vector< double, N > *position, Vector< double, N > *velocity)
void SetFrictionCoefficient(double newFrictionCoefficient)
Sets the friction coefficient.
Definition: Matrix.hpp:27
double GetFrictionCoefficient() const
Returns friction coefficient.
void Update(double currentTimeInSeconds, double timeIntervalInSeconds)
Updates the collider state.
Definition: pybind11Utils.hpp:20
const std::shared_ptr< Surface< N > > & GetSurface() const
Returns the surface instance.
Abstract base class for N-D surface.
Definition: Surface.hpp:38
virtual ~Collider()=default
Default virtual destructor.
double distance
Definition: Collider.hpp:109
virtual Vector< double, N > VelocityAt(const Vector< double, N > &point) const =0
Returns the velocity of the collider at given point.
Vector< double, N > point
Definition: Collider.hpp:110
Internal query result structure.
Definition: Collider.hpp:107
Abstract base class for generic collider object.
Definition: Collider.hpp:30
Vector< double, N > normal
Definition: Collider.hpp:111
void SetSurface(const std::shared_ptr< Surface< N >> &newSurface)
Assigns the surface instance from the subclass.
void SetOnBeginUpdateCallback(const OnBeginUpdateCallback &callback)
Sets the callback function to be called when Collider::update function is invoked.