Fireworks Engine  v2.0
Lightweight Sandbox Game Engine using OpenGL for additional Customisation and Quick Prototyping
rigidbody2d.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <box2d/box2d.h>
4 #include <glm/glm.hpp>
5 #include <glm/gtc/matrix_transform.hpp>
6 #include "../components/component.h"
7 
9 static const b2Vec2 gravity(0.0f, -10.0f);
10 static b2World World(gravity);
11 
12 namespace fireworks { namespace physics {
13 
15  const float M2PX = 250.0f, M2PY = 150.0f;
16  const float P2MX = 1.0f / M2PX, P2MY = 1.0f / M2PY;
17 
20  {
21  Static = b2_staticBody,
22  Dynamic = b2_dynamicBody,
23  Kinematic = b2_kinematicBody
24  };
25 
28  {
29  public:
33  float density;
35  float friction;
37  float resitution;
39  float gravityScale;
42  private:
43  b2World& m_World;
44  b2BodyDef m_BodyDef;
45  b2Body* m_Body;
46  b2PolygonShape m_Shape;
47  b2FixtureDef m_Fixture;
48 
49  bool m_DidGenerateRB;
50  public:
57  RigidBody2D(float density, float friction, RigidBodyType bodytype, b2World& world = World);
58  ~RigidBody2D();
59 
61  glm::vec3 GetPositionInMeters();
63  glm::vec3 GetPositionInPixels();
67  float GetRotation();
68 
73  void AddForce(glm::vec2 direction, float force);
77  void SetVelocity(glm::vec2 velocity);
81  void SetPosition(const glm::vec3& position);
85  void SetSize(glm::vec2& size);
87  void GenerateRigidBody(glm::vec3 pos, glm::vec2 dim);
88 
90  inline b2Body* GetBody() { return m_Body; }
91  };
92 
93 } }
~RigidBody2D()
Definition: rigidbody2d.cpp:8
bool fixedRotation
Whether or not to clamp the rigid bodys rotation.
Definition: rigidbody2d.h:41
float resitution
The bounciness of the rigid body.
Definition: rigidbody2d.h:37
float density
The density of the rigid body.
Definition: rigidbody2d.h:33
Definition: rigidbody2d.h:23
RigidBody 2D Physics component to simulate real time physics.
Definition: rigidbody2d.h:27
glm::vec3 GetPositionInPixels()
Gets the position of the rigid body in pixels.
Definition: rigidbody2d.cpp:19
void SetPosition(const glm::vec3 &position)
Sets the position of the rigid body.
Definition: rigidbody2d.cpp:41
const float P2MX
Definition: rigidbody2d.h:16
float GetRotation()
Gets the rotation of the rigid body in radians.
Definition: rigidbody2d.cpp:25
Class for creating components.
Definition: component.h:22
void SetVelocity(glm::vec2 velocity)
Sets the velocity of the rigid body.
Definition: rigidbody2d.cpp:35
float gravityScale
The effect of gravity on the rigid body.
Definition: rigidbody2d.h:39
const float P2MY
Definition: rigidbody2d.h:16
Definition: audioclip.cpp:3
Definition: rigidbody2d.h:22
RigidBodyType bodyType
Denotes whether the RigidBody2D is dynamic, static or kinematic.
Definition: rigidbody2d.h:31
const float M2PX
Box2D scaling factors (set these up dynamically using the projections matrix&#39;s Aspect Ratio and Scree...
Definition: rigidbody2d.h:15
glm::vec3 GetPositionInMeters()
Gets the position of the rigid body in meters (M.K.S system)
Definition: rigidbody2d.cpp:13
RigidBodyType
The type of the rigid body.
Definition: rigidbody2d.h:19
const float M2PY
Definition: rigidbody2d.h:15
float friction
The friction of the rigid body surface.
Definition: rigidbody2d.h:35
void AddForce(glm::vec2 direction, float force)
Applies force to the rigid body.
Definition: rigidbody2d.cpp:30
Definition: rigidbody2d.h:21
void GenerateRigidBody(glm::vec3 pos, glm::vec2 dim)
Generates a plane rigid body using the position and dimensions.
Definition: rigidbody2d.cpp:53
RigidBody2D(float density, float friction, RigidBodyType bodytype, b2World &world=World)
Creates the rigid body.
Definition: rigidbody2d.cpp:3
b2Body * GetBody()
Gets the box2D body of the rigid body.
Definition: rigidbody2d.h:90
void SetSize(glm::vec2 &size)
Sets the size of the rigid body.
Definition: rigidbody2d.cpp:47