Aspen
2D C++ Class-Based Object Oriented Game Engine
Physics.hpp
1 #ifndef __PHYSICS_HPP
2 #define __PHYSICS_HPP
3 #include "Object.hpp"
4 #include <string>
5 #define _USE_MATH_DEFINES
6 #include <cmath>
7 
9 namespace Aspen
10 {
12 namespace Physics
13 {
15 namespace GRAV_DIR
16 {
18 const double LEFT = 0;
20 const double DOWN = M_PI * 0.5;
22 const double RIGHT = M_PI;
24 const double UP = M_PI * 1.5;
25 }; // namespace GRAV_DIR
26 
28 class Physics : public Object::Object
29 {
31  double _gravStrength;
35  double _drag;
36 
37 public:
42  Physics(Object *parent = nullptr, std::string name = "Physics");
50  Physics(double strength, double direction = GRAV_DIR::DOWN, Object *parent = nullptr, std::string name = "Physics");
59  Physics(double strength, double direction, double drag, Object *parent = nullptr, std::string name = "Physics");
61  ~Physics();
62 
66  void operator()();
67 
70  double GetGravityStrength();
73  double GetGravityDirection();
76  double GetGravityX();
79  double GetGravityY();
82  void SetGravityStrength(double strength);
85  void SetGravityDirection(double direction);
89  void SetGravity(double x, double y);
90 
93  double GetDrag();
96  void SetDrag(double drag);
97 
100  void PopulateDebugger();
101 };
102 
104 class Rigidbody : public Object::Object
105 {
107  double _mass;
118 
119 public:
124  Rigidbody(Object *parent = nullptr, std::string name = "Rigidbody");
131  Rigidbody(double mass, Object *parent = nullptr, std::string name = "Rigidbody");
133  ~Rigidbody();
134 
138  void operator()();
139 
142  double GetMass();
145  void SetMass(double mass);
146 
149  double GetVelocityStrength();
152  double GetVelocityDirection();
155  double GetVelocityX();
158  double GetVelocityY();
161  void SetVelocityStrength(double strength);
164  void SetVelocityDirection(double direction);
168  void SetVelocity(double strength, double direction);
172  void SetCartesianVelocity(double x, double y);
173 
176  double GetAccelerationStrength();
179  double GetAccelerationDirection();
182  double GetAccelerationX();
185  double GetAccelerationY();
188  void SetAccelerationStrength(double strength);
191  void SetAccelerationDirection(double direction);
195  void SetAcceleration(double strength, double direction);
199  void SetCartesianAcceleration(double x, double y);
200 
204  void ApplyForce(double force, double angle);
208  void ApplyCartesianForce(double x, double y);
209 
212  double GetGravityScale();
215  void SetGravityScale(double scale);
216 
219  void PopulateDebugger();
220 };
221 
223 
225 class Collider : public Object::Object
226 {
227 protected:
229  bool _trigger;
232 
233 public:
239  Collider(Object *parent = nullptr, std::string name = "Collider");
240 
244  void operator()();
245 
250  virtual std::pair<Collision, Collision> TestCollision(Collider *other);
253  virtual void ResolveCollision(Collision collision);
259  virtual bool InCollider(int x, int y);
260 
263  bool IsTrigger();
266  void SetTrigger(bool trigger);
267 
270  void PopulateDebugger();
271 };
272 
274 class CircleCollider : public Collider
275 {
277  double _radius;
278 
279 public:
284  CircleCollider(Object *parent = nullptr, std::string name = "CircleCollider");
291  CircleCollider(double radius, Object *parent = nullptr, std::string name = "CircleCollider");
292 
297  std::pair<Collision, Collision> TestCollision(Collider *other);
300  void ResolveCollision(Collision collision);
306  bool InCollider(int x, int y);
307 
310  double GetRadius();
313  void SetRadius(double radius);
314 
317  void PopulateDebugger();
318 };
319 
321 class AABBCollider : public Collider
322 {
324  double _width;
326  double _height;
327 
328 public:
333  AABBCollider(Object *parent = nullptr, std::string name = "AABBCollider");
341  AABBCollider(double width, double height, Object *parent = nullptr, std::string name = "AABBCollider");
342 
347  std::pair<Collision, Collision> TestCollision(Collider *other);
350  void ResolveCollision(Collision collision);
356  bool InCollider(int x, int y);
357 
360  double GetWidth();
363  void SetWidth(double width);
366  double GetHeight();
369  void SetHeight(double height);
373  void SetSize(double width, double height);
374 
377  void PopulateDebugger();
378 };
381 } // namespace Physics
382 } // namespace Aspen
383 
384 #endif
double _accelerationDirection
Direction of acceleration.
Definition: Physics.hpp:115
std::pair< Collision, Collision > TestCollision(Collider *other)
Finds if there is a collision between this Collider and another Collider.
double GetVelocityDirection()
Gets the current velocity direction.
void SetHeight(double height)
Sets the height.
Physics class.
Definition: Physics.hpp:28
const double UP
Up.
Definition: Physics.hpp:24
AABBCollider BoxCollider
BoxCollider is a synonym to AABBCollider.
Definition: Physics.hpp:380
double _gravityScale
Amount gravity affects this object.
Definition: Physics.hpp:117
void SetVelocity(double strength, double direction)
Sets the velocity.
void SetGravityStrength(double strength)
Sets the gravity strength.
void SetAccelerationDirection(double direction)
Sets the acceleration direction.
double _width
Width of the box.
Definition: Physics.hpp:324
double _mass
Mass of the object.
Definition: Physics.hpp:107
Object(Object *parent=nullptr, std::string name="Object")
Constructor Derived classes should call this in their constructors' initialization list.
const double DOWN
Down.
Definition: Physics.hpp:20
Rigidbody class.
Definition: Physics.hpp:104
double _radius
Radius of the collider.
Definition: Physics.hpp:277
General base class Allows for parent/child relationship trees.
Definition: Object.hpp:33
double GetAccelerationY()
Gets the current vertical cartesian acceleration strength.
double GetGravityScale()
Sets the object's gravity scale.
void SetCartesianVelocity(double x, double y)
Sets the velocity strength and direction via cartesian coordinates.
void ApplyCartesianForce(double x, double y)
Applies a cartesian force to the Rigidbody.
bool _trigger
Determines if the collider is a trigger or solid object.
Definition: Physics.hpp:229
CircleCollider(Object *parent=nullptr, std::string name="CircleCollider")
Constructor.
double _height
Height of the box.
Definition: Physics.hpp:326
double _velocityDirection
Direction of velocity.
Definition: Physics.hpp:111
Collider(Object *parent=nullptr, std::string name="Collider")
Constructor Derived classes should call this in their constructors' initialization list.
double GetGravityStrength()
Gets the current gravity strength.
double GetAccelerationStrength()
Gets the current acceleration strength.
void SetGravityDirection(double direction)
Sets the gravity direction.
void SetGravityScale(double scale)
Sets the object's gravity scale.
double GetMass()
Gets the object's mass.
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
void SetDrag(double drag)
Sets the drag factor.
bool InCollider(int x, int y)
Determines if a point is within the collider.
double GetVelocityY()
Gets the current vertical cartesian velocity strength.
void operator()()
Updates this object and all of its children Derived classes should call or reimplement this at some p...
Collider class.
Definition: Physics.hpp:225
std::pair< Collision, Collision > TestCollision(Collider *other)
Finds if there is a collision between this Collider and another Collider.
Axis Aligned Bounding Box Collider class.
Definition: Physics.hpp:321
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
double GetGravityX()
Gets the current horizontal cartesian gravity strength.
void SetMass(double mass)
Sets the object's mass.
void operator()()
Updates this object and all of its children Derived classes should call or reimplement this at some p...
void ApplyForce(double force, double angle)
Applies a force to the Rigidbody.
Rigidbody(Object *parent=nullptr, std::string name="Rigidbody")
Constructor.
double _accelerationStrength
Strength of acceleration.
Definition: Physics.hpp:113
void SetVelocityStrength(double strength)
Sets the velocity strength.
double _gravDirection
Gravity direction.
Definition: Physics.hpp:33
void ResolveCollision(Collision collision)
Resolves a collision and passes the collision to the parent via OnCollision.
void SetRadius(double radius)
Sets the radius.
virtual bool InCollider(int x, int y)
Determines if a point is within the collider.
virtual std::pair< Collision, Collision > TestCollision(Collider *other)
Finds if there is a collision between this Collider and another Collider.
AABBCollider(Object *parent=nullptr, std::string name="AABBCollider")
Constructor.
const double RIGHT
Right.
Definition: Physics.hpp:22
double GetGravityDirection()
Gets the current gravity direction.
void ResolveCollision(Collision collision)
Resolves a collision and passes the collision to the parent via OnCollision.
void SetSize(double width, double height)
Sets the box's size.
void SetGravity(double x, double y)
Sets the gravity strength and direction via cartesian coordinates.
void SetTrigger(bool trigger)
Determines if the collider is a trigger or solid object.
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
const double LEFT
Left.
Definition: Physics.hpp:18
double GetVelocityStrength()
Gets the current velocity strength.
Collision class.
Definition: Collision.hpp:25
CircleCollider class.
Definition: Physics.hpp:274
double GetRadius()
Gets the radius.
virtual void ResolveCollision(Collision collision)
Resolves a collision and passes the collision to the parent via OnCollision.
void SetAccelerationStrength(double strength)
Sets the acceleration strength.
void SetWidth(double width)
Sets the width.
double GetGravityY()
Gets the current vertical cartesian gravity strength.
void operator()()
Updates this object and all of its children Derived classes should call or reimplement this at some p...
double _drag
Drag factor.
Definition: Physics.hpp:35
bool _mouseOver
Determines if the collider is being moused over.
Definition: Physics.hpp:231
double GetHeight()
Gets the height.
Physics(Object *parent=nullptr, std::string name="Physics")
Constructor.
double GetAccelerationX()
Gets the current horizontal cartesian acceleration strength.
Aspen engine namespace.
Definition: Audio.hpp:8
double GetDrag()
Gets the current drag factor.
double GetAccelerationDirection()
Gets the current acceleration direction.
double GetWidth()
Gets the width.
double _gravStrength
Gravity strength.
Definition: Physics.hpp:31
void SetCartesianAcceleration(double x, double y)
Sets the acceleration strength and direction via cartesian coordinates.
bool IsTrigger()
Determines if the collider is a trigger or solid object.
void SetAcceleration(double strength, double direction)
Sets the acceleration.
double GetVelocityX()
Gets the current horizontal cartesian velocity strength.
void SetVelocityDirection(double direction)
Sets the velocity direction.
bool InCollider(int x, int y)
Determines if a point is within the collider.
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...
double _velocityStrength
Strength of velocity.
Definition: Physics.hpp:109
void PopulateDebugger()
Fills out the Debugger if it exists with this Object's information Derived classes should call their ...