Collider2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Collider2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Abstract base class for generic collider object.
6 > Created Time: 2017/05/09
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_COLLIDER2_H
10 #define CUBBYFLOW_COLLIDER2_H
11 
12 #include <Core/Surface/Surface2.h>
13 
14 #include <functional>
15 
16 namespace CubbyFlow
17 {
27  class Collider2
28  {
29  public:
36  using OnBeginUpdateCallback = std::function<void(Collider2*, double, double)>;
37 
39  Collider2();
40 
42  virtual ~Collider2();
43 
45  virtual Vector2D VelocityAt(const Vector2D& point) const = 0;
46 
55  void ResolveCollision(double radius, double restitutionCoefficient, Vector2D* position, Vector2D* velocity);
56 
58  double GetFrictionCoefficient() const;
59 
66  void SetFrictionCoefficient(double newFrictionCoeffient);
67 
69  const Surface2Ptr& GetSurface() const;
70 
72  void Update(double currentTimeInSeconds, double timeIntervalInSeconds);
73 
84  void SetOnBeginUpdateCallback(const OnBeginUpdateCallback& callback);
85 
86  protected:
88  struct ColliderQueryResult final
89  {
90  double distance;
94  };
95 
97  void SetSurface(const Surface2Ptr& newSurface);
98 
100  void GetClosestPoint(const Surface2Ptr& surface, const Vector2D& queryPoint, ColliderQueryResult* result) const;
101 
103  bool IsPenetrating(const ColliderQueryResult& colliderPoint, const Vector2D& position, double radius);
104 
105  private:
106  Surface2Ptr m_surface;
107  double m_frictionCoeffient = 0.0;
108  OnBeginUpdateCallback m_onUpdateCallback;
109  };
110 
112  using Collider2Ptr = std::shared_ptr<Collider2>;
113 }
114 
115 #endif
void SetSurface(const Surface2Ptr &newSurface)
Assigns the surface instance from the subclass.
virtual Vector2D VelocityAt(const Vector2D &point) const =0
Returns the velocity of the collider at given point.
virtual ~Collider2()
Default destructor.
std::shared_ptr< Collider2 > Collider2Ptr
Shared pointer type for the Collider2.
Definition: Collider2.h:112
Vector2D point
Definition: Collider2.h:91
double distance
Definition: Collider2.h:90
Vector2D velocity
Definition: Collider2.h:93
bool IsPenetrating(const ColliderQueryResult &colliderPoint, const Vector2D &position, double radius)
Returns true if given point is in the opposite side of the surface.
double GetFrictionCoefficient() const
Returns friction coefficient.
Internal query result structure.
Definition: Collider2.h:88
Vector2D normal
Definition: Collider2.h:92
const Surface2Ptr & GetSurface() const
Returns the surface instance.
void GetClosestPoint(const Surface2Ptr &surface, const Vector2D &queryPoint, ColliderQueryResult *result) const
Outputs closest point&#39;s information.
void SetOnBeginUpdateCallback(const OnBeginUpdateCallback &callback)
Sets the callback function to be called when Collider2::update function is invoked.
void ResolveCollision(double radius, double restitutionCoefficient, Vector2D *position, Vector2D *velocity)
void Update(double currentTimeInSeconds, double timeIntervalInSeconds)
Updates the collider state.
Definition: pybind11Utils.h:24
2-D vector class.
Definition: Vector2.h:26
std::function< void(Collider2 *, double, double)> OnBeginUpdateCallback
Callback function type for update calls.
Definition: Collider2.h:36
Abstract base class for generic collider object.
Definition: Collider2.h:27
std::shared_ptr< Surface2 > Surface2Ptr
Shared pointer for the Surface2 type.
Definition: Surface2.h:100
void SetFrictionCoefficient(double newFrictionCoeffient)
Sets the friction coefficient.
Collider2()
Default constructor.