PROTO::KLUDGE  0.1
Quick prototyping library for games using SDL and OpenGL.
DynamicBody.hpp
Go to the documentation of this file.
1 #ifndef DYNAMICBODY_HPP
2 #define DYNAMICBODY_HPP
3 
4 #include <Box2D/Box2D.h>
5 
8 
9 namespace pk
10 {
11 
21 {
22  private:
28  public:
34  DynamicBody(b2World* world, GLRect rect);
35 
36  virtual ~DynamicBody();
37 
43  b2World* getWorld(){ return m_world; };
44 
50  void setDensity(float density){ m_body->GetFixtureList()[0].SetDensity(density); m_body->ResetMassData(); };
51 
57  float getDensity(){ return m_body->GetFixtureList()[0].GetDensity(); };
58 
64  void setFriction(float friction){ m_body->GetFixtureList()[0].SetFriction(friction); };
65 
71  float getFriction(){ return m_body->GetFixtureList()[0].GetFriction(); };
72 
78  void setRestitution(float restitution){ m_body->GetFixtureList()[0].SetRestitution(restitution); };
79 
85  float getRestitution(){ m_body->GetFixtureList()[0].GetRestitution(); };
86 
92  void setFixedRotation(bool r){ m_bodyDef.fixedRotation = r; };
93 
102  void allowSleep(bool s){ m_bodyDef.allowSleep = s; };
103 
110  b2Vec2 getPosition(){ return { m_body->GetPosition().x, m_body->GetPosition().y}; };
111 
118  void setPosition(glm::vec2 position){ m_body->SetTransform({position.x, position.y}, m_body->GetAngle()); };
119 
126  float getRotation(){ return m_body->GetAngle(); };
127 
137  void setTransform(b2Vec2 position, float angle){ m_body->SetTransform(position, angle); };
138 
145  glm::vec2 getPositionInPixels(){ return physics::metersToPixels(m_body->GetPosition()); };
146 
153  void setLinearVelocity(glm::vec2 v){ m_body->SetLinearVelocity({v.x, v.y}); };
154 
161  glm::vec2 getLinearVelocity(){ return {m_body->GetLinearVelocity().x, m_body->GetLinearVelocity().y}; };
162 
168  void destroyBody(){ m_world->DestroyBody(m_body); m_body = NULL; };
169 
170  private:
171  b2World* m_world;
172  b2BodyDef m_bodyDef;
173  b2Body* m_body;
174  b2PolygonShape m_shape;
175  b2FixtureDef m_fixtureDef;
177 };
178 
179 }
180 
181 #endif // DYNAMICBODY_HPP
void allowSleep(bool s)
Set the body to allow sleeping. This increases efficiency and performance of Box2D while also prevent...
Definition: DynamicBody.hpp:102
Definition: Game.hpp:7
DynamicBody()
Default constructor - Inaccessible.
Definition: DynamicBody.hpp:27
float getDensity()
Get the density of the body.
Definition: DynamicBody.hpp:57
void setTransform(b2Vec2 position, float angle)
Set the transform (position and angle) of the DynamicBody.
Definition: DynamicBody.hpp:137
void setFixedRotation(bool r)
Set the body&#39;s rotation as fixed (no rotation).
Definition: DynamicBody.hpp:92
A class wrapper to ease the instantiation and use of Box2D dynamic bodies. It also performs conversio...
Definition: DynamicBody.hpp:20
b2PolygonShape m_shape
A Box2D polygon shape.
Definition: DynamicBody.hpp:174
void setLinearVelocity(glm::vec2 v)
Set the linear velocity of the body.
Definition: DynamicBody.hpp:153
float getRotation()
Get the rotation of the DynamicBody.
Definition: DynamicBody.hpp:126
float getRestitution()
Get the restitution of the body.
Definition: DynamicBody.hpp:85
b2FixtureDef m_fixtureDef
A Box2D fixture definition structure.
Definition: DynamicBody.hpp:175
void destroyBody()
Destroy the DynamicBody. Safely removes the body using Box2D internals.
Definition: DynamicBody.hpp:168
glm::vec2 getPositionInPixels()
Get the position of the body in screen units (pixels).
Definition: DynamicBody.hpp:145
void setRestitution(float restitution)
Set the restitution (bounciness) for the body.
Definition: DynamicBody.hpp:78
b2Vec2 getPosition()
Get the position of the body in Box2D units (meters).
Definition: DynamicBody.hpp:110
void setDensity(float density)
Set the density of the body.
Definition: DynamicBody.hpp:50
float getFriction()
Get the friction of the body.
Definition: DynamicBody.hpp:71
void setPosition(glm::vec2 position)
Set the position of the DynamicBody.
Definition: DynamicBody.hpp:118
GLRect m_rect
A GLRect structure.
Definition: DynamicBody.hpp:176
Useful for defining size and origin of an OpenGL object.
Definition: GLTransformable.hpp:15
b2BodyDef m_bodyDef
A Box2D body definition structure.
Definition: DynamicBody.hpp:172
glm::vec2 getLinearVelocity()
Get the linear velocity of the body.
Definition: DynamicBody.hpp:161
virtual ~DynamicBody()
void setFriction(float friction)
Set the friction for the body.
Definition: DynamicBody.hpp:64
b2World * getWorld()
Get a pointer to the Box2D world.
Definition: DynamicBody.hpp:43
b2Body * m_body
A Box2D body.
Definition: DynamicBody.hpp:173
b2World * m_world
A pointer to a Box2D world.
Definition: DynamicBody.hpp:168