Something went wrong. Try again.
Box2d lite but uses fixed points instead of floats for embedded devices, fork from https://github.com/erincatto/box2d-lite github.com/erincatto/box2d-lite
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713/** Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com** Permission to use, copy, modify, distribute and sell this software* and its documentation for any purpose is hereby granted without fee,* provided that the above copyright notice appear in all copies.* Erin Catto makes no representations about the suitability * of this software for any purpose. * It is provided "as is" without express or implied warranty.*/
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
#include "imgui/imgui.h"#include "imgui_impl_glfw.h"#include "imgui_impl_opengl2.h"
#define GLFW_INCLUDE_NONE#include "glad/glad.h"#include "GLFW/glfw3.h"
#include "box2d-lite/World.h"#include "box2d-lite/Body.h"#include "box2d-lite/Joint.h"
namespace{ GLFWwindow* mainWindow = NULL;
Body bodies[200]; Joint joints[100]; Body* bomb = NULL;
float timeStep = 1.0f / 60.0f; int iterations = 10; Vec2 gravity(0.0f, -10.0f);
int numBodies = 0; int numJoints = 0;
int demoIndex = 0;
int width = 1280; int height = 720; float zoom = 10.0f; float pan_y = 8.0f;
World world(gravity, iterations);}
static void glfwErrorCallback(int error, const char* description){ printf("GLFW error %d: %s\n", error, description);}
static void DrawText(int x, int y, const char* string){ ImVec2 p; p.x = float(x); p.y = float(y); ImGui::Begin("Overlay", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar); ImGui::SetCursorPos(p); ImGui::TextColored(ImColor(230, 153, 153, 255), "%s", string); ImGui::End();}
static void DrawBody(Body* body){ Mat22 R(body->rotation); Vec2 x = body->position; Vec2 h = 0.5f * body->width;
Vec2 v1 = x + R * Vec2(-h.x, -h.y); Vec2 v2 = x + R * Vec2( h.x, -h.y); Vec2 v3 = x + R * Vec2( h.x, h.y); Vec2 v4 = x + R * Vec2(-h.x, h.y);
if (body == bomb) glColor3f(0.4f, 0.9f, 0.4f); else glColor3f(0.8f, 0.8f, 0.9f);
glBegin(GL_LINE_LOOP); glVertex2f(v1.x, v1.y); glVertex2f(v2.x, v2.y); glVertex2f(v3.x, v3.y); glVertex2f(v4.x, v4.y); glEnd();}
static void DrawJoint(Joint* joint){ Body* b1 = joint->body1; Body* b2 = joint->body2;
Mat22 R1(b1->rotation); Mat22 R2(b2->rotation);
Vec2 x1 = b1->position; Vec2 p1 = x1 + R1 * joint->localAnchor1;
Vec2 x2 = b2->position; Vec2 p2 = x2 + R2 * joint->localAnchor2;
glColor3f(0.5f, 0.5f, 0.8f); glBegin(GL_LINES); glVertex2f(x1.x, x1.y); glVertex2f(p1.x, p1.y); glVertex2f(x2.x, x2.y); glVertex2f(p2.x, p2.y); glEnd();}
static void LaunchBomb(){ if (!bomb) { bomb = bodies + numBodies; bomb->Set(Vec2(1.0f, 1.0f), 50.0f); bomb->friction = 0.2f; world.Add(bomb); ++numBodies; }
bomb->position.Set(Random(-15.0f, 15.0f), 15.0f); bomb->rotation = Random(-1.5f, 1.5f); bomb->velocity = -1.5f * bomb->position; bomb->angularVelocity = Random(-20.0f, 20.0f);}
// Single boxstatic void Demo1(Body* b, Joint* j){ b->Set(Vec2(100.0f, 20.0f), FLT_MAX); b->position.Set(0.0f, -0.5f * b->width.y); world.Add(b); ++b; ++numBodies;
b->Set(Vec2(1.0f, 1.0f), 200.0f); b->position.Set(0.0f, 4.0f); world.Add(b); ++b; ++numBodies;}
// A simple pendulumstatic void Demo2(Body* b, Joint* j){ Body* b1 = b + 0; b1->Set(Vec2(100.0f, 20.0f), FLT_MAX); b1->friction = 0.2f; b1->position.Set(0.0f, -0.5f * b1->width.y); b1->rotation = 0.0f; world.Add(b1);
Body* b2 = b + 1; b2->Set(Vec2(1.0f, 1.0f), 100.0f); b2->friction = 0.2f; b2->position.Set(9.0f, 11.0f); b2->rotation = 0.0f; world.Add(b2);
numBodies += 2;
j->Set(b1, b2, Vec2(0.0f, 11.0f)); world.Add(j);
numJoints += 1;}
// Varying friction coefficientsstatic void Demo3(Body* b, Joint* j){ b->Set(Vec2(100.0f, 20.0f), FLT_MAX); b->position.Set(0.0f, -0.5f * b->width.y); world.Add(b); ++b; ++numBodies;
b->Set(Vec2(13.0f, 0.25f), FLT_MAX); b->position.Set(-2.0f, 11.0f); b->rotation = -0.25f; world.Add(b); ++b; ++numBodies;
b->Set(Vec2(0.25f, 1.0f), FLT_MAX); b->position.Set(5.25f, 9.5f); world.Add(b); ++b; ++numBodies;
b->Set(Vec2(13.0f, 0.25f), FLT_MAX); b->position.Set(2.0f, 7.0f); b->rotation = 0.25f; world.Add(b); ++b; ++numBodies;
b->Set(Vec2(0.25f, 1.0f), FLT_MAX); b->position.Set(-5.25f, 5.5f); world.Add(b); ++b; ++numBodies;
b->Set(Vec2(13.0f, 0.25f), FLT_MAX); b->position.Set(-2.0f, 3.0f); b->rotation = -0.25f; world.Add(b); ++b; ++numBodies;
float friction[5] = {0.75f, 0.5f, 0.35f, 0.1f, 0.0f}; for (int i = 0; i < 5; ++i) { b->Set(Vec2(0.5f, 0.5f), 25.0f); b->friction = friction[i]; b->position.Set(-7.5f + 2.0f * i, 14.0f); world.Add(b); ++b; ++numBodies; }}
// A vertical stackstatic void Demo4(Body* b, Joint* j){ b->Set(Vec2(100.0f, 20.0f), FLT_MAX); b->friction = 0.2f; b->position.Set(0.0f, -0.5f * b->width.y); b->rotation = 0.0f; world.Add(b); ++b; ++numBodies;
for (int i = 0; i < 10; ++i) { b->Set(Vec2(1.0f, 1.0f), 1.0f); b->friction = 0.2f; float x = Random(-0.1f, 0.1f); b->position.Set(x, 0.51f + 1.05f * i); world.Add(b); ++b; ++numBodies; }}
// A pyramidstatic void Demo5(Body* b, Joint* j){ b->Set(Vec2(100.0f, 20.0f), FLT_MAX); b->friction = 0.2f; b->position.Set(0.0f, -0.5f * b->width.y); b->rotation = 0.0f; world.Add(b); ++b; ++numBodies;
Vec2 x(-6.0f, 0.75f); Vec2 y;
for (int i = 0; i < 12; ++i) { y = x;
for (int j = i; j < 12; ++j) { b->Set(Vec2(1.0f, 1.0f), 10.0f); b->friction = 0.2f; b->position = y; world.Add(b); ++b; ++numBodies;
y += Vec2(1.125f, 0.0f); }
//x += Vec2(0.5625f, 1.125f); x += Vec2(0.5625f, 2.0f); }}
// A teeterstatic void Demo6(Body* b, Joint* j){ Body* b1 = b + 0; b1->Set(Vec2(100.0f, 20.0f), FLT_MAX); b1->position.Set(0.0f, -0.5f * b1->width.y); world.Add(b1);
Body* b2 = b + 1; b2->Set(Vec2(12.0f, 0.25f), 100.0f); b2->position.Set(0.0f, 1.0f); world.Add(b2);
Body* b3 = b + 2; b3->Set(Vec2(0.5f, 0.5f), 25.0f); b3->position.Set(-5.0f, 2.0f); world.Add(b3);
Body* b4 = b + 3; b4->Set(Vec2(0.5f, 0.5f), 25.0f); b4->position.Set(-5.5f, 2.0f); world.Add(b4);
Body* b5 = b + 4; b5->Set(Vec2(1.0f, 1.0f), 100.0f); b5->position.Set(5.5f, 15.0f); world.Add(b5);
numBodies += 5;
j->Set(b1, b2, Vec2(0.0f, 1.0f)); world.Add(j);
numJoints += 1;}
// A suspension bridgestatic void Demo7(Body* b, Joint* j){ b->Set(Vec2(100.0f, 20.0f), FLT_MAX); b->friction = 0.2f; b->position.Set(0.0f, -0.5f * b->width.y); b->rotation = 0.0f; world.Add(b); ++b; ++numBodies;
const int numPlanks = 15; float mass = 50.0f;
for (int i = 0; i < numPlanks; ++i) { b->Set(Vec2(1.0f, 0.25f), mass); b->friction = 0.2f; b->position.Set(-8.5f + 1.25f * i, 5.0f); world.Add(b); ++b; ++numBodies; }
// Tuning float frequencyHz = 2.0f; float dampingRatio = 0.7f;
// frequency in radians float omega = 2.0f * k_pi * frequencyHz;
// damping coefficient float d = 2.0f * mass * dampingRatio * omega;
// spring stifness float k = mass * omega * omega;
// magic formulas float softness = 1.0f / (d + timeStep * k); float biasFactor = timeStep * k / (d + timeStep * k);
for (int i = 0; i < numPlanks; ++i) { j->Set(bodies+i, bodies+i+1, Vec2(-9.125f + 1.25f * i, 5.0f)); j->softness = softness; j->biasFactor = biasFactor;
world.Add(j); ++j; ++numJoints; }
j->Set(bodies + numPlanks, bodies, Vec2(-9.125f + 1.25f * numPlanks, 5.0f)); j->softness = softness; j->biasFactor = biasFactor; world.Add(j); ++j; ++numJoints;}
// Dominosstatic void Demo8(Body* b, Joint* j){ Body* b1 = b; b->Set(Vec2(100.0f, 20.0f), FLT_MAX); b->position.Set(0.0f, -0.5f * b->width.y); world.Add(b); ++b; ++numBodies;
b->Set(Vec2(12.0f, 0.5f), FLT_MAX); b->position.Set(-1.5f, 10.0f); world.Add(b); ++b; ++numBodies;
for (int i = 0; i < 10; ++i) { b->Set(Vec2(0.2f, 2.0f), 10.0f); b->position.Set(-6.0f + 1.0f * i, 11.125f); b->friction = 0.1f; world.Add(b); ++b; ++numBodies; }
b->Set(Vec2(14.0f, 0.5f), FLT_MAX); b->position.Set(1.0f, 6.0f); b->rotation = 0.3f; world.Add(b); ++b; ++numBodies;
Body* b2 = b; b->Set(Vec2(0.5f, 3.0f), FLT_MAX); b->position.Set(-7.0f, 4.0f); world.Add(b); ++b; ++numBodies;
Body* b3 = b; b->Set(Vec2(12.0f, 0.25f), 20.0f); b->position.Set(-0.9f, 1.0f); world.Add(b); ++b; ++numBodies;
j->Set(b1, b3, Vec2(-2.0f, 1.0f)); world.Add(j); ++j; ++numJoints;
Body* b4 = b; b->Set(Vec2(0.5f, 0.5f), 10.0f); b->position.Set(-10.0f, 15.0f); world.Add(b); ++b; ++numBodies;
j->Set(b2, b4, Vec2(-7.0f, 15.0f)); world.Add(j); ++j; ++numJoints;
Body* b5 = b; b->Set(Vec2(2.0f, 2.0f), 20.0f); b->position.Set(6.0f, 2.5f); b->friction = 0.1f; world.Add(b); ++b; ++numBodies;
j->Set(b1, b5, Vec2(6.0f, 2.6f)); world.Add(j); ++j; ++numJoints;
Body* b6 = b; b->Set(Vec2(2.0f, 0.2f), 10.0f); b->position.Set(6.0f, 3.6f); world.Add(b); ++b; ++numBodies;
j->Set(b5, b6, Vec2(7.0f, 3.5f)); world.Add(j); ++j; ++numJoints;}
// A multi-pendulumstatic void Demo9(Body* b, Joint* j){ b->Set(Vec2(100.0f, 20.0f), FLT_MAX); b->friction = 0.2f; b->position.Set(0.0f, -0.5f * b->width.y); b->rotation = 0.0f; world.Add(b);
Body * b1 = b; ++b; ++numBodies;
float mass = 10.0f;
// Tuning float frequencyHz = 4.0f; float dampingRatio = 0.7f;
// frequency in radians float omega = 2.0f * k_pi * frequencyHz;
// damping coefficient float d = 2.0f * mass * dampingRatio * omega;
// spring stiffness float k = mass * omega * omega;
// magic formulas float softness = 1.0f / (d + timeStep * k); float biasFactor = timeStep * k / (d + timeStep * k);
const float y = 12.0f;
for (int i = 0; i < 15; ++i) { Vec2 x(0.5f + i, y); b->Set(Vec2(0.75f, 0.25f), mass); b->friction = 0.2f; b->position = x; b->rotation = 0.0f; world.Add(b);
j->Set(b1, b, Vec2(float(i), y)); j->softness = softness; j->biasFactor = biasFactor; world.Add(j);
b1 = b; ++b; ++numBodies; ++j; ++numJoints; }}
void (*demos[])(Body* b, Joint* j) = {Demo1, Demo2, Demo3, Demo4, Demo5, Demo6, Demo7, Demo8, Demo9};const char* demoStrings[] = { "Demo 1: A Single Box", "Demo 2: Simple Pendulum", "Demo 3: Varying Friction Coefficients", "Demo 4: Randomized Stacking", "Demo 5: Pyramid Stacking", "Demo 6: A Teeter", "Demo 7: A Suspension Bridge", "Demo 8: Dominos", "Demo 9: Multi-pendulum"};
static void InitDemo(int index){ world.Clear(); numBodies = 0; numJoints = 0; bomb = NULL;
demoIndex = index; demos[index](bodies, joints);}
static void Keyboard(GLFWwindow* window, int key, int scancode, int action, int mods){ if (action != GLFW_PRESS) { return; }
switch (key) { case GLFW_KEY_ESCAPE: // Quit glfwSetWindowShouldClose(mainWindow, GL_TRUE); break;
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': InitDemo(key - GLFW_KEY_1); break;
case GLFW_KEY_A: World::accumulateImpulses = !World::accumulateImpulses; break;
case GLFW_KEY_P: World::positionCorrection = !World::positionCorrection; break;
case GLFW_KEY_W: World::warmStarting = !World::warmStarting; break;
case GLFW_KEY_SPACE: LaunchBomb(); break; }}
static void Reshape(GLFWwindow*, int w, int h){ width = w; height = h > 0 ? h : 1;
glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity();
float aspect = float(width) / float(height); if (width >= height) { // aspect >= 1, set the height from -1 to 1, with larger width glOrtho(-zoom * aspect, zoom * aspect, -zoom + pan_y, zoom + pan_y, -1.0, 1.0); } else { // aspect < 1, set the width to -1 to 1, with larger height glOrtho(-zoom, zoom, -zoom / aspect + pan_y, zoom / aspect + pan_y, -1.0, 1.0); }}
int main(int, char**){ glfwSetErrorCallback(glfwErrorCallback);
if (glfwInit() == 0) { fprintf(stderr, "Failed to initialize GLFW\n"); return -1; }
mainWindow = glfwCreateWindow(width, height, "box2d-lite", NULL, NULL); if (mainWindow == NULL) { fprintf(stderr, "Failed to open GLFW mainWindow.\n"); glfwTerminate(); return -1; }
glfwMakeContextCurrent(mainWindow);
// Load OpenGL functions using glad int gladStatus = gladLoadGL(); if (gladStatus == 0) { fprintf(stderr, "Failed to load OpenGL.\n"); glfwTerminate(); return -1; }
glfwSwapInterval(1); glfwSetWindowSizeCallback(mainWindow, Reshape); glfwSetKeyCallback(mainWindow, Keyboard);
float xscale, yscale; glfwGetWindowContentScale(mainWindow, &xscale, &yscale); float uiScale = xscale;
IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGui::StyleColorsClassic(); ImGui_ImplGlfw_InitForOpenGL(mainWindow, true); ImGui_ImplOpenGL2_Init(); ImGuiIO& io = ImGui::GetIO(); io.FontGlobalScale = uiScale;
glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); float aspect = float(width) / float(height); if (width >= height) { // aspect >= 1, set the height from -1 to 1, with larger width glOrtho(-zoom * aspect, zoom * aspect, -zoom + pan_y, zoom + pan_y, -1.0, 1.0); } else { // aspect < 1, set the width to -1 to 1, with larger height glOrtho(-zoom, zoom, -zoom / aspect + pan_y, zoom / aspect + pan_y, -1.0, 1.0); }
InitDemo(0);
while (!glfwWindowShouldClose(mainWindow)) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ImGui_ImplOpenGL2_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame();
// Globally position text ImGui::SetNextWindowPos(ImVec2(10.0f, 10.0f)); ImGui::Begin("Overlay", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar); ImGui::End();
DrawText(5, 5, demoStrings[demoIndex]); DrawText(5, 35, "Keys: 1-9 Demos, Space to Launch the Bomb");
char buffer[64]; sprintf(buffer, "(A)ccumulation %s", World::accumulateImpulses ? "ON" : "OFF"); DrawText(5, 65, buffer);
sprintf(buffer, "(P)osition Correction %s", World::positionCorrection ? "ON" : "OFF"); DrawText(5, 95, buffer);
sprintf(buffer, "(W)arm Starting %s", World::warmStarting ? "ON" : "OFF"); DrawText(5, 125, buffer);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
world.Step(timeStep);
for (int i = 0; i < numBodies; ++i) DrawBody(bodies + i);
for (int i = 0; i < numJoints; ++i) DrawJoint(joints + i);
glPointSize(4.0f); glColor3f(1.0f, 0.0f, 0.0f); glBegin(GL_POINTS); std::map<ArbiterKey, Arbiter>::const_iterator iter; for (iter = world.arbiters.begin(); iter != world.arbiters.end(); ++iter) { const Arbiter& arbiter = iter->second; for (int i = 0; i < arbiter.numContacts; ++i) { Vec2 p = arbiter.contacts[i].position; glVertex2f(p.x, p.y); } } glEnd(); glPointSize(1.0f);
ImGui::Render(); ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
glfwPollEvents(); glfwSwapBuffers(mainWindow); }
glfwTerminate(); return 0;}