Permalink
Browse files

ADD support for physics debug drawing

  • Loading branch information...
1 parent a3ae810 commit 4ec334113aa673c64afe58df12d9b8345fb1fb0e @PowerKiKi committed Oct 25, 2010
@@ -29,12 +29,12 @@ namespace Polukili
b2PolygonShape bulletShape;
bulletShape.SetAsBox(((float)this->getImageWidth() * this->powerFactor / Constants::pixelsPerUnits) / 2.0f, ((float)this->getImageHeight() * this->powerFactor / Constants::pixelsPerUnits) / 2.0f);
- b2FixtureDef bulletDef;
- bulletDef.shape = &bulletShape;
+ b2FixtureDef bulletDef;
+ bulletDef.shape = &bulletShape;
bulletDef.density = Constants::defaultDensity;
bulletDef.friction = Constants::defaultFriction;
bulletDef.restitution = Constants::defaultRestitution;
- bulletDef.filter.categoryBits = bullets;
+ bulletDef.filter.categoryBits = bullets;
bulletDef.filter.maskBits = ground + enemies;
this->body->CreateFixture(&bulletDef);
@@ -84,7 +84,7 @@ namespace Polukili
/*************************************************/
void Console::render()
{
- unsigned int fontsize = 12;
+ unsigned int fontsize = 14;
for (int i = 0; i < maximumRows; i ++)
{
GRRLIB_PrintfTTF(16, 16 + i * fontsize,this->font, the_console[i], fontsize, RGBA(255,0,0,0));
@@ -0,0 +1,76 @@
+#include <DebugDraw.h>
+
+#include <grrlib.h>
+
+#include <Constants.h>
+#include <Console.h>
+
+namespace Polukili
+{
+ DebugDraw::DebugDraw()
+ {
+ // We want to draw everything by default
+ this->SetFlags(
+ e_shapeBit ///< draw shapes
+ | e_jointBit ///< draw joint connections
+ | e_aabbBit ///< draw axis aligned bounding boxes
+ | e_pairBit ///< draw broad-phase pairs
+ | e_centerOfMassBit
+ );
+ }
+
+ void DebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
+ {
+ guVector v[vertexCount];
+ u32 colors[vertexCount];
+ for (int32 i = 0; i < vertexCount; i++)
+ {
+ v[i].x = Constants::pixelsPerUnits * vertices[i].x;
+ v[i].y = Constants::pixelsPerUnits * vertices[i].y;
+ v[i].z = 0;
+ colors[i] = RGBA((int)(color.r * 255), (int)(color.g * 255), (int)(color.b * 255), 255);
+ Console::log(LOG_INFO, "point[%d] %f, %f, %f, %f", i, v[i].x, v[i].y, vertices[i].x, vertices[i].y);
+ }
+
+ GRRLIB_NGone(v, colors, vertexCount);
+ }
+
+ void DebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
+ {
+ guVector v[vertexCount];
+ u32 colors[vertexCount];
+ for (int32 i = 0; i < vertexCount; i++)
+ {
+ v[i].x = Constants::pixelsPerUnits * vertices[i].x;
+ v[i].y = Constants::pixelsPerUnits * vertices[i].y;
+ v[i].z = 0;
+ colors[i] = RGBA((int)(color.r * 255), (int)(color.g * 255), (int)(color.b * 255), 255);
+ Console::log(LOG_INFO, "point[%d] %f, %f", i, v[i].x, v[i].y);
+ }
+
+ GRRLIB_NGoneFilled(v, colors, vertexCount);
+ }
+
+ void DebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
+ {
+ u32 c = RGBA((int)(color.r * 255), (int)(color.g * 255), (int)(color.b * 255), 255);
+ GRRLIB_Circle(center.y, center.y, radius, c, 0);
+ }
+
+ void DebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
+ {
+ u32 c = RGBA((int)(color.r * 255), (int)(color.g * 255), (int)(color.b * 255), 255);
+ GRRLIB_Circle(center.y, center.y, radius, c, 1);
+ }
+
+ void DebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
+ {
+ u32 c = RGBA((int)(color.r * 255), (int)(color.g * 255), (int)(color.b * 255), 255);
+ GRRLIB_Line(p1.x, p1.y, p2.x, p2.y, c);
+ }
+
+ void DebugDraw::DrawTransform(const b2Transform& xf)
+ {
+
+ }
+}
@@ -0,0 +1,38 @@
+#ifndef Polukili_DebugDraw_h
+#define Polukili_DebugDraw_h
+
+#include <Box2D/Box2D.h>
+
+namespace Polukili
+{
+ /**
+ * This class implements debug drawing callbacks that are invoked via b2World
+ */
+ class DebugDraw : public b2DebugDraw
+ {
+ public:
+
+ DebugDraw();
+
+ /// Draw a closed polygon provided in CCW order.
+ virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
+
+ /// Draw a solid closed polygon provided in CCW order.
+ virtual void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
+
+ /// Draw a circle.
+ virtual void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);
+
+ /// Draw a solid circle.
+ virtual void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);
+
+ /// Draw a line segment.
+ virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);
+
+ /// Draw a transform. Choose your own length scale.
+ /// @param xf a transform.
+ virtual void DrawTransform(const b2Transform& xf);
+ };
+}
+
+#endif
@@ -33,7 +33,7 @@ namespace Polukili
}
/*************************************************/
- void Bee::initPhysic(const b2Vec2& position)
+ void Bee::initPhysic(const b2Vec2& position, float32 angle)
{
this->Actor::initPhysic(position);
b2CircleShape beeShape;
@@ -23,7 +23,7 @@ namespace Polukili
virtual ~Bee();
- virtual void initPhysic(const b2Vec2& position);
+ virtual void initPhysic(const b2Vec2& position, float32 angle = 0);
/**
@@ -85,16 +85,18 @@ namespace Polukili
WPAD_ScanPads();
Level* level = this->levels.top();
-
level->nextStep();
level->render();
-
+
+ if (this->debugDrawEnabled)
+ level->world->DrawDebugData();
+
if (this->console.isEnabled())
this->console.render();
// Render FPS
GRRLIB_PrintfTTF(570, 32, this->font, framePerSecond, 16, GRRLIB_RED);
-
+
GRRLIB_Render(); // Render the frame buffer to the screen
@@ -116,6 +118,7 @@ namespace Polukili
// We return to the launcher application via exit
if (pressed & WPAD_BUTTON_HOME) return;
if (pressed & WPAD_BUTTON_PLUS) this->console.enable(!this->console.isEnabled());
+ if (pressed & WPAD_BUTTON_MINUS) this->debugDrawEnabled = !this->debugDrawEnabled;
// Count FPS
frameCount++;
@@ -13,6 +13,7 @@
#include <Level.h>
#include <ImageLibrary.h>
#include <ContactListener.h>
+#include <DebugDraw.h>
#include <Console.h>
#include <PeriodicTimer.h>
@@ -55,12 +56,19 @@ namespace Polukili
ImageLibrary imageLibrary;
ContactListener contactListener;
+
+
+ /**
+ * The instance to draw debug physics.
+ */
+ DebugDraw debugDraw;
private:
stack< Level* > levels;
GRRLIB_ttfFont* font;
Console console;
PeriodicTimer fpsTimer;
+ bool debugDrawEnabled;
};
} /* End of namespace Polukili */
@@ -29,7 +29,7 @@ namespace Polukili
{
/*************************************************/
Level::Level(Game* game)
- : world(0), game(game), backgroundSprite(0), foregroundSprite(0)
+ : world(0), game(game), backgroundSprite(0), foregroundSprite(0), body(0)
{
// Nothing do to here
}
@@ -42,7 +42,7 @@ namespace Polukili
while (!this->actors.empty())
delete this->actors.front();
-
+
if (this->world != 0)
delete this->world;
}
@@ -72,6 +72,7 @@ namespace Polukili
bool doSleep = false;
this->world = new b2World(gravity, doSleep);
this->world->SetContactListener(&this->game->contactListener);
+ this->world->SetDebugDraw(&this->game->debugDraw);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, 0.0f);
@@ -145,8 +146,8 @@ namespace Polukili
Console::log(LOG_INFO, this->body ? "not null" : "null");
- b2FixtureDef polygonDef;
- polygonDef.shape = &polygonShape;
+ b2FixtureDef polygonDef;
+ polygonDef.shape = &polygonShape;
// polygonDef.filter.categoryBits = ground;
this->body->CreateFixture(&polygonDef);
@@ -293,6 +294,7 @@ namespace Polukili
if (this->foregroundSprite)
this->foregroundSprite->draw();
+
Console::log(LOG_INFO, "Level::render() - end");
}
@@ -323,8 +325,9 @@ namespace Polukili
Console::log(LOG_INFO, "nextstep physic with %d actors", this->actors.size());
- Console::log(LOG_INFO, "this->world->Step(%f, %d, %d);", Constants::timeStep, Constants::iterations, Constants::iterations);
+ Console::log(LOG_INFO, "this->world->Step(%f, %d, %d);", Constants::timeStep, Constants::iterations, Constants::iterations);
this->world->Step(Constants::timeStep, Constants::iterations, Constants::iterations);
+ this->world->ClearForces();
Console::log(LOG_INFO, "nextstep physic end");
}
@@ -31,15 +31,15 @@ namespace Polukili
}
/*************************************************/
- void Player::initPhysic(const b2Vec2& position)
+ void Player::initPhysic(const b2Vec2& position, float32 angle)
{
this->Actor::initPhysic(position);
this->body->SetFixedRotation(true);
b2PolygonShape playerShape;
playerShape.SetAsBox((((float)this->getImageWidth() / Constants::pixelsPerUnits) / 2.0f), (((float)this->getImageHeight() / Constants::pixelsPerUnits) / 2.0f));
-
- b2FixtureDef playerDef;
- playerDef.shape = &playerShape;
+
+ b2FixtureDef playerDef;
+ playerDef.shape = &playerShape;
playerDef.density = Constants::defaultDensity;
playerDef.friction = Constants::defaultFriction;
playerDef.restitution = Constants::defaultRestitution;
@@ -26,7 +26,7 @@ namespace Polukili
*/
virtual ~Player();
- virtual void initPhysic(const b2Vec2& position);
+ virtual void initPhysic(const b2Vec2& position, float32 angle = 0);
/**
* Define the behavior of the actor for the next step. Apply forces/actions/modifications based on current context (wiimote inputs, state changes based on time, etc.)

0 comments on commit 4ec3341

Please sign in to comment.