PROTO::KLUDGE  0.1
Quick prototyping library for games using SDL and OpenGL.
CharacterBody.hpp
Go to the documentation of this file.
1 #ifndef CHARACTERBODY_HPP
2 #define CHARACTERBODY_HPP
3 
4 #include <Box2D/Box2D.h>
5 
8 
9 namespace pk
10 {
11 
12 //forward declaration
13 class SensorData;
14 
26 {
27  private:
33  public:
34  CharacterBody(b2World* world, GLRect rect, int id);
35  virtual ~CharacterBody();
36 
42  b2World* getWorld(){ return m_world; };
43 
49  void setDensity(float density){ m_body->GetFixtureList()[0].SetDensity(density); };
50 
56  float getDensity(){ return m_body->GetFixtureList()[0].GetDensity(); m_body->ResetMassData(); };
57 
63  void setFriction(float friction){ m_body->GetFixtureList()[0].SetFriction(friction); };
64 
70  float getFriction(){ return m_body->GetFixtureList()[0].GetFriction(); };
71 
77  void setRestitution(float restitution){ m_body->GetFixtureList()[0].SetRestitution(restitution); };
78 
84  float getRestitution(){ m_body->GetFixtureList()[0].GetRestitution(); };
85 
91  void setFixedRotation(bool r){ m_bodyDef.fixedRotation = r; };
92 
101  void allowSleep(bool s){ m_bodyDef.allowSleep = s; };
102 
109  b2Vec2 getPosition(){ return { m_body->GetPosition().x, m_body->GetPosition().y}; };
110 
117  void setPosition(glm::vec2 position){ m_body->SetTransform({position.x, position.y}, m_body->GetAngle()); };
118 
124  float getRotation(){ return m_body->GetAngle(); };
125 
135  void setTransform(b2Vec2 position, float angle){ m_body->SetTransform(position, angle); };
136 
143  glm::vec2 getPositionInPixels(){ return physics::metersToPixels(m_body->GetPosition()); };
144 
151  void setLinearVelocity(glm::vec2 v){ m_body->SetLinearVelocity({v.x, v.y}); };
152 
159  glm::vec2 getLinearVelocity(){ return {m_body->GetLinearVelocity().x, m_body->GetLinearVelocity().y}; };
160 
167 
175 
183 
191 
199 
205  void destroyBody();
206 
212  bool canJump();
213 
220  bool touchingWall();
221 
222  private:
223  b2World* m_world;
224  b2BodyDef m_bodyDef;
225  b2Body* m_body;
226  b2PolygonShape m_shape;
227  b2FixtureDef m_fixtureDef;
234 };
235 
236 }
237 
238 #endif // CHARACTERBODY_HPP
glm::vec2 getPositionInPixels()
Get the position of the body in screen units (pixels).
Definition: CharacterBody.hpp:143
SensorData * m_footSensorData
Foot sensor data.
Definition: CharacterBody.hpp:230
void setLinearVelocity(glm::vec2 v)
Set the linear velocity of the body.
Definition: CharacterBody.hpp:151
float getFriction()
Get the friction of the body.
Definition: CharacterBody.hpp:70
b2PolygonShape m_shape
A Box2D polygon shape.
Definition: CharacterBody.hpp:226
SensorData * m_leftSensorData
Left sensor data.
Definition: CharacterBody.hpp:231
SensorData * m_topSensorData
Top sensor data.
Definition: CharacterBody.hpp:233
SensorData * m_rightSensorData
Right sensor data.
Definition: CharacterBody.hpp:232
Definition: Game.hpp:7
b2Vec2 getPosition()
Get the position of the body in Box2D units (meters).
Definition: CharacterBody.hpp:109
b2BodyDef m_bodyDef
A Box2D body definition structure.
Definition: CharacterBody.hpp:224
A class wrapping some user data for Box2D fixtures. Includes data for number of contacts being made t...
Definition: SensorData.hpp:30
b2World * getWorld()
Get a pointer to the Box2D world.
Definition: CharacterBody.hpp:42
glm::vec2 getLinearVelocity()
Get the linear velocity of the body.
Definition: CharacterBody.hpp:159
void setFixedRotation(bool r)
Set the body&#39;s rotation as fixed (no rotation).
Definition: CharacterBody.hpp:91
void setFriction(float friction)
Set the friction for the body.
Definition: CharacterBody.hpp:63
SensorData * rightSensor()
Get the right sensor data.
Definition: CharacterBody.hpp:190
b2FixtureDef m_fixtureDef
A Box2D fixture definition structure.
Definition: CharacterBody.hpp:227
virtual ~CharacterBody()
void setPosition(glm::vec2 position)
Set the position of the CharacterBody.
Definition: CharacterBody.hpp:117
SensorData * leftSensor()
Get the left sensor data.
Definition: CharacterBody.hpp:182
A class representing a Box2D dynamic body with special fixtures and data used to track collisions and...
Definition: CharacterBody.hpp:25
void setTransform(b2Vec2 position, float angle)
Set the transform data (position and angle) for the CharacterBody.
Definition: CharacterBody.hpp:135
void destroyBody()
Destroy the CharacterBody. Safe destruction using the internals of Box2D.
Definition: CharacterBody.hpp:198
CharacterBody()
Default constructor - Inaccessible.
Definition: CharacterBody.hpp:32
void setDensity(float density)
Set the density of the body.
Definition: CharacterBody.hpp:49
bool canJump()
Check if the CharacterBody can jump. Based on SensorData from foot sensor.
int m_characterID
The unique id of the character body.
Definition: CharacterBody.hpp:229
float getDensity()
Get the density of the body.
Definition: CharacterBody.hpp:56
int getCharacterBodyID()
Get the CharacterBody ID.
Definition: CharacterBody.hpp:166
GLRect m_rect
A GLRect structure.
Definition: CharacterBody.hpp:228
Useful for defining size and origin of an OpenGL object.
Definition: GLTransformable.hpp:15
b2World * m_world
A pointer to a Box2D world.
Definition: CharacterBody.hpp:223
float getRestitution()
Get the restitution of the body.
Definition: CharacterBody.hpp:84
void setRestitution(float restitution)
Set the restitution (bounciness) for the body.
Definition: CharacterBody.hpp:77
SensorData * topSensor()
Get the top sensor data.
Definition: CharacterBody.hpp:198
bool touchingWall()
Check if the CharacterBody is touching a wall. Based on SensorData from left, right, and top.
SensorData * footSensor()
Get the foot sensor data.
Definition: CharacterBody.hpp:174
float getRotation()
Get the rotation for the CharacterBody.
Definition: CharacterBody.hpp:124
b2Body * m_body
A Box2D body.
Definition: CharacterBody.hpp:225
void allowSleep(bool s)
Set the body to allow sleeping. This increases efficiency and performance of Box2D while also prevent...
Definition: CharacterBody.hpp:101