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