Box2D_TestBed_improvement

This commit is contained in:
aismann 2021-08-12 05:54:06 +02:00
parent 1452e698e3
commit c94562d417
69 changed files with 819 additions and 806 deletions

View File

@ -27,8 +27,6 @@
USING_NS_CC;
// 'Interfaces' to adxe
cocos2d::DrawNode* drawBox2D;
#if defined(CC_PLATFORM_PC)
extern cocos2d::Label* labelDebugDraw;
@ -37,22 +35,9 @@ extern cocos2d::Label* labelDebugDraw;
#define BUFFER_OFFSET(x) ((const void*) (x))
//
//// halx99: since adxe init scene default camera at 'initWithXXX' function, only change design size at scene
//// construct is ok see also: https://github.com/adxeproject/adxe/commit/581a7921554c09746616759d5a5ca6ce9d3eaa22
//auto director = Director::getInstance();
//auto glview = director->getOpenGLView();
//Size designSize(960 * 0.85, 640 * 0.85);
//glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);
cocos2d::Vec2 debugNodeOffset = { 260, 70 };
//float mRatio = 10.0f;
DebugDraw::DebugDraw()
{
mRatio = 10;
drawBP = DrawNode::create();
drawBox2D = drawBP;
}
DebugDraw::~DebugDraw()
@ -61,10 +46,21 @@ DebugDraw::~DebugDraw()
void DebugDraw::initShader( void )
{
//mShaderProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
//mColorLocation = glGetUniformLocation( mShaderProgram->getProgram(), "u_color");
// initShader is unsupported
}
cocos2d::DrawNode* DebugDraw::GetDrawNode()
{
return drawBP;
}
void DebugDraw::SetDrawNode(cocos2d::DrawNode* drawNode)
{
CCASSERT(!drawBP, "drawBP is not NULL");
drawBP = drawNode;
}
void DebugDraw::DrawPolygon(const b2Vec2* verts, int vertexCount, const b2Color& color)
{
Vec2* vec = new (std::nothrow) Vec2[vertexCount];
@ -117,9 +113,7 @@ void DebugDraw::DrawPoint(const b2Vec2& p, float size, const b2Color& color)
{
drawBP->drawPoint(Vec2(p.x * mRatio, p.y * mRatio) + debugNodeOffset, size, Color4F(color.r, color.g, color.b, color.a));
}
static char PrintStringBuffer[1024 * 8];
static char* PrintStringCursor;
char const* MessageString = NULL;
void DebugDraw::DrawString(int x, int y, const char* string, ...)
{
#if defined(CC_PLATFORM_PC)
@ -129,7 +123,6 @@ void DebugDraw::DrawString(int x, int y, const char* string, ...)
#endif
}
void DebugDraw::DrawString(const b2Vec2& pw, const char* string, ...)
{
#if defined(CC_PLATFORM_PC)
@ -159,4 +152,5 @@ void DebugDraw::DrawAABB(b2AABB* aabb, const b2Color& color)
void DebugDraw::Flush()
{
// Flush is unsupported
}

View File

@ -31,90 +31,8 @@
#include "cocos2d.h"
struct b2AABB;
//struct b2AABB;
extern cocos2d::DrawNode* drawBox2D;
/// Test settings. Some can be controlled in the GUI.
struct Settings
{
Settings()
{
viewCenter.Set(0.0f, 0.0f);
hz = 60.0f;
velocityIterations = 8;
positionIterations = 3;
drawShapes = 1;
drawJoints = 1;
drawAABBs = 0;
drawContactPoints = 0;
drawContactNormals = 0;
drawContactImpulse = 0;
drawFrictionImpulse = 0;
drawCOMs = 0;
drawStats = 0;
drawProfile = 0;
enableWarmStarting = 1;
enableContinuous = 1;
enableSubStepping = 0;
enableSleep = 1;
pause = 0;
singleStep = 0;
}
b2Vec2 viewCenter;
float hz;
int32 velocityIterations;
int32 positionIterations;
int32 drawShapes;
int32 drawJoints;
int32 drawAABBs;
int32 drawContactPoints;
int32 drawContactNormals;
int32 drawContactImpulse;
int32 drawFrictionImpulse;
int32 drawCOMs;
int32 drawStats;
int32 drawProfile;
int32 enableWarmStarting;
int32 enableContinuous;
int32 enableSubStepping;
int32 enableSleep;
int32 pause;
int32 singleStep;
};
struct ContactPoint
{
b2Fixture* fixtureA;
b2Fixture* fixtureB;
b2Vec2 normal;
b2Vec2 position;
b2PointState state;
float normalImpulse;
float tangentImpulse;
float separation;
};
//struct Camera
//{
// Camera()
// {
// m_center.Set(0.0f, 20.0f);
// m_zoom = 1.0f;
// m_width = 1280;
// m_height = 800;
// }
//
// b2Vec2 ConvertScreenToWorld(const b2Vec2& screenPoint);
// b2Vec2 ConvertWorldToScreen(const b2Vec2& worldPoint);
// void BuildProjectionMatrix(float* m, float zBias);
//
// b2Vec2 m_center;
// float m_zoom;
// int32 m_width;
// int32 m_height;
//};
// This class implements debug drawing callbacks that are invoked
// inside b2World::Step.
@ -122,14 +40,9 @@ struct ContactPoint
//PhysicsDebugNode : public DrawNode
class CC_EX_DLL DebugDraw : public b2Draw
{
// cocos2d::g* mShaderProgram;
// GLint mColorLocation;
cocos2d::DrawNode* drawBP = NULL;
void initShader( void );
public:
void initShader( void );
DebugDraw();
~DebugDraw();
@ -158,7 +71,13 @@ public:
void Flush();
cocos2d::DrawNode* GetDrawNode();
void SetDrawNode(cocos2d::DrawNode* drawNode);
float mRatio;
cocos2d::DrawNode* drawBP = NULL;
cocos2d::Vec2 debugNodeOffset = { 250, 70 };
};
#endif //__PHYSICSNODES_DEBUGNODE_BOX2D_H__

View File

@ -41,11 +41,6 @@ enum {
};
//DebugDraw stuff
extern cocos2d::DrawNode* drawBox2D;
extern cocos2d::Vec2 debugNodeOffset;
Box2DTests::Box2DTests()
{
ADD_TEST_CASE(Box2DTest);
@ -103,6 +98,7 @@ bool Box2DTest::init()
this->addChild(menu);
menu->setPosition(VisibleRect::right().x - 100, VisibleRect::top().y - 60);
drawBox2D = g_debugDraw.GetDrawNode();
addChild(drawBox2D, 100);
drawBox2D->setOpacity(150);
@ -253,7 +249,7 @@ void Box2DTest::initPhysics()
g_debugDraw.SetFlags(flags);
g_debugDraw.mRatio = PTM_RATIO;
debugNodeOffset = { 0, 0 };
g_debugDraw.debugNodeOffset = { 0, 0 };
world->SetDebugDraw(&g_debugDraw);
}
@ -279,6 +275,8 @@ void Box2DTest::addNewSpriteAtPosition(Vec2 p)
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x / PTM_RATIO, p.y / PTM_RATIO);
CCLOG("Add PTM_RATIO sprite %0.2f x %0.2f", p.x / PTM_RATIO, p.y / PTM_RATIO);
b2Body* body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.

View File

@ -58,7 +58,7 @@ private:
b2World* world;
cocos2d::Texture2D* _spriteTexture;
cocos2d::DrawNode* drawBox2D;
DebugDraw g_debugDraw;
bool showDebugDraw = true;
} ;

View File

@ -25,28 +25,27 @@
#include "platform/CCPlatformConfig.h"
#include "Box2DTestBed.h"
#include "extensions/cocos-ext.h"
#include "test.h"
#include "tests/test.h"
#include "tests/settings.h"
#include "ImGuiEXT/CCImGuiEXT.h"
//#include "renderer/CCRenderer.h"
USING_NS_CC;
USING_NS_CC_EXT;
static bool show_test_window = true;
static bool show_another_window = true;
static ImVec4 clear_color = ImColor(114, 144, 154);
enum {
kTagParentNode = 1,
};
#define kAccelerometerFrequency 30
#define FRAMES_BETWEEN_PRESSES_FOR_DOUBLE_CLICK 10
extern int g_testCount;
#define PTM_RATIO 32
Settings settings;
extern cocos2d::Vec2 debugNodeOffset;
extern cocos2d::Vec2 physicsDebugNodeOffset;
cocos2d::Label* labelDebugDraw;
enum
@ -54,8 +53,22 @@ enum
kTagBox2DNode,
};
extern cocos2d::DrawNode* drawBox2D;
TestEntry g_testEntries[MAX_TESTS] = { {nullptr} };
int g_testCount = 0;
int RegisterTest(const char* category, const char* name, TestCreateFcn* fcn)
{
int index = g_testCount;
if (index < MAX_TESTS)
{
g_testEntries[index] = { category, name, fcn };
++g_testCount;
return index;
}
return -1;
}
Box2DTestBedTests::Box2DTestBedTests()
{
@ -75,19 +88,18 @@ Box2DTestBedTests::Box2DTestBedTests()
Box2DTestBed::Box2DTestBed()
{
debugNodeOffset = { 260, 70 };
}
Box2DTestBed::~Box2DTestBed()
{
_eventDispatcher->removeEventListener(_touchListener);
Layer::_eventDispatcher->removeEventListener(_touchListener);
}
Box2DTestBed* Box2DTestBed::createWithEntryID(int entryId)
{
auto layer = new (std::nothrow) Box2DTestBed();
layer->initWithEntryID(entryId);
layer->autorelease();
// layer->autorelease();
return layer;
}
@ -104,49 +116,86 @@ bool Box2DTestBed::initWithEntryID(int entryId)
m_entryID = entryId;
Box2DView* view = Box2DView::viewWithEntryID(entryId);
addChild(view, 0, kTagBox2DNode);
view->setScale(15);
view->setAnchorPoint(Vec2(0, 0));
view->setPosition(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 3);
auto label = Label::createWithTTF(view->title().c_str(), "fonts/arial.ttf", 28);
addChild(label, 1);
m_entry = g_testEntries + entryId;
m_test = m_entry->createFcn();
debugDrawNode = g_debugDraw.GetDrawNode();
m_test->debugDrawNode = g_debugDraw.GetDrawNode();
m_test->g_debugDrawTestBed = g_debugDraw;
TestCase::addChild(debugDrawNode, 100);
// drawBox2D->setOpacity(150);
// init physics
this->initPhysics();
auto label = Label::createWithTTF(m_entry->name, "fonts/arial.ttf", 28);
TestCase::addChild(label, 1);
label->setPosition(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height - 50);
// Adds touch event listener
_touchListener = EventListenerTouchOneByOne::create();
_touchListener->setSwallowTouches(true);
_touchListener->onTouchBegan = CC_CALLBACK_2(Box2DTestBed::onTouchBegan, this);
_touchListener->onTouchMoved = CC_CALLBACK_2(Box2DTestBed::onTouchMoved, this);
_touchListener->onTouchEnded = CC_CALLBACK_2(Box2DTestBed::onTouchEnded, this);
TestCase::_eventDispatcher->addEventListenerWithFixedPriority(_touchListener, -10);
_eventDispatcher->addEventListenerWithFixedPriority(_touchListener, 1);
// Adds Keyboard event listener
_keyboardListener = EventListenerKeyboard::create();
_keyboardListener->onKeyPressed = CC_CALLBACK_2(Box2DTestBed::onKeyPressed, this);
_keyboardListener->onKeyReleased = CC_CALLBACK_2(Box2DTestBed::onKeyReleased, this);
TestCase::_eventDispatcher->addEventListenerWithFixedPriority(_keyboardListener, -11);
addChild(drawBox2D, 100);
// Demo messageString
labelDebugDraw = Label::createWithTTF("TEST", "fonts/Marker Felt.ttf", 8.0f);
labelDebugDraw->setAnchorPoint(Vec2(0, 0));
labelDebugDraw->setPosition(VisibleRect::left().x, VisibleRect::top().y - 20);
labelDebugDraw->setColor(Color3B::WHITE);
addChild(labelDebugDraw, 100);
TestCase::addChild(labelDebugDraw, 100);
TestCase::scheduleUpdate();
return true;
}
bool Box2DTestBed::onTouchBegan(Touch* touch, Event* event)
{
return true;
auto location = touch->getLocation() - g_debugDraw.debugNodeOffset;
b2Vec2 pos = { location.x / g_debugDraw.mRatio, location.y / g_debugDraw.mRatio };
return m_test->MouseDown(pos);
}
void Box2DTestBed::onTouchMoved(Touch* touch, Event* event)
{
auto diff = touch->getDelta();
auto node = getChildByTag(kTagBox2DNode);
auto currentPos = node->getPosition();
node->setPosition(currentPos + diff);
auto location = touch->getLocation() - g_debugDraw.debugNodeOffset;
b2Vec2 pos = { location.x / g_debugDraw.mRatio, location.y / g_debugDraw.mRatio };
m_test->MouseMove(pos);
}
void Box2DTestBed::onTouchEnded(Touch* touch, Event* event)
{
auto location = touch->getLocation() - g_debugDraw.debugNodeOffset;
b2Vec2 pos = { location.x / g_debugDraw.mRatio, location.y / g_debugDraw.mRatio };
m_test->MouseUp(pos);
}
void Box2DTestBed::onKeyPressed(EventKeyboard::KeyCode code, Event* event)
{
log("Box2dView:onKeyPressed, keycode: %d", static_cast<int>(code));
m_test->Keyboard((static_cast<int>(code) - 59));
}
void Box2DTestBed::onKeyReleased(EventKeyboard::KeyCode code, Event* event)
{
log("onKeyReleased, keycode: %d", static_cast<int>(code));
m_test->Keyboard((static_cast<int>(code) - 59));
}
void Box2DTestBed::onEnter()
@ -161,222 +210,30 @@ void Box2DTestBed::onExit()
ImGuiEXT::getInstance()->removeRenderLoop("#im01");
}
void Box2DTestBed::update(float dt)
{
// Debug draw
debugDrawNode->clear();
m_test->Step(settings);
m_test->m_world->DebugDraw();
}
void Box2DTestBed::initPhysics()
{
uint32 flags = 0;
flags += 1 * b2Draw::e_shapeBit;
flags += 1 * b2Draw::e_jointBit;
flags += 0 * b2Draw::e_aabbBit;
flags += 0 * b2Draw::e_centerOfMassBit;
g_debugDraw.SetFlags(flags);
g_debugDraw.mRatio = PTM_RATIO / 4;
m_test->m_world->SetDebugDraw(&g_debugDraw);
m_test->g_debugDraw = g_debugDraw;
g_debugDraw.debugNodeOffset = { 250, 70 };
settings.m_hertz = 60;
}
void Box2DTestBed::onDrawImGui()
{
//// 1. Show a simple window
//// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
//{
// static float f = 0.0f;
// ImGui::Text("Hello, world!");
// ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
// ImGui::ColorEdit3("clear color", (float*)&clear_color);
// if (ImGui::Button("Test Window")) show_test_window ^= 1;
// if (ImGui::Button("Another Window")) show_another_window ^= 1;
// ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
//}
//// 2. Show another simple window, this time using an explicit Begin/End pair
//if (show_another_window)
//{
// ImGui::SetNextWindowSize(ImVec2(170, 80), ImGuiCond_FirstUseEver);
// ImGui::Begin("Another Window", &show_another_window);
// ImGui::Text("Hello");
// ImGui::End();
//}
//// 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
//if (show_test_window)
//{
// ImGui::ShowDemoWindow();
//}
}
//------------------------------------------------------------------
//
// Box2DView
//
//------------------------------------------------------------------
Box2DView::Box2DView(void)
{
}
Box2DView* Box2DView::viewWithEntryID(int entryId)
{
Box2DView* pView = new (std::nothrow) Box2DView();
pView->initWithEntryID(entryId);
pView->autorelease();
return pView;
}
bool Box2DView::initWithEntryID(int entryId)
{
m_entry = g_testEntries + entryId;
m_test = m_entry->createFcn();
// Adds Touch Event Listener
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(Box2DView::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(Box2DView::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(Box2DView::onTouchEnded, this);
_eventDispatcher->addEventListenerWithFixedPriority(listener, -10);
_touchListener = listener;
auto keyboardListener = EventListenerKeyboard::create();
keyboardListener->onKeyPressed = CC_CALLBACK_2(Box2DView::onKeyPressed, this);
keyboardListener->onKeyReleased = CC_CALLBACK_2(Box2DView::onKeyReleased, this);
_eventDispatcher->addEventListenerWithFixedPriority(keyboardListener, -11);
_keyboardListener = keyboardListener;
return true;
}
std::string Box2DView::title() const
{
std::string title = std::string(m_entry->category) + std::string(":") + std::string(m_entry->name);
return title;
}
void Box2DView::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
{
Layer::draw(renderer, transform, flags);
_customCmd.init(_globalZOrder, transform, flags);
_customCmd.func = CC_CALLBACK_0(Box2DView::onDraw, this, transform, flags);
renderer->addCommand(&_customCmd);
}
void Box2DView::onDraw(const Mat4& transform, uint32_t flags)
{
//Director* director = Director::getInstance();
//CCASSERT(nullptr != director, "Director is null when setting matrix stack");
//director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
//director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
//drawBox2D->clear();
//m_test->Step(&settings);
//m_test->m_world->DebugDraw();
//director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when setting matrix stack");
auto oldMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewMV);
// world->DebugDraw();
drawBox2D->clear();
m_test->Step(&settings);
//m_test->m_world->DebugDraw();
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, oldMV);
}
Box2DView::~Box2DView()
{
// Removes Touch Event Listener
_eventDispatcher->removeEventListener(_touchListener);
_eventDispatcher->removeEventListener(_keyboardListener);
delete m_test;
}
bool Box2DView::onTouchBegan(Touch* touch, Event* event)
{
auto touchLocation = touch->getLocation();
auto nodePosition = convertToNodeSpace(touchLocation);
log("Box2DView::onTouchBegan, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);
m_test->MouseDown(b2Vec2(nodePosition.x, nodePosition.y));
return false;
}
void Box2DView::onTouchMoved(Touch* touch, Event* event)
{
auto touchLocation = touch->getLocation();
auto nodePosition = convertToNodeSpace(touchLocation);
log("Box2DView::onTouchMoved, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);
m_test->MouseMove(b2Vec2(nodePosition.x, nodePosition.y));
}
void Box2DView::onTouchEnded(Touch* touch, Event* event)
{
auto touchLocation = touch->getLocation();
auto nodePosition = convertToNodeSpace(touchLocation);
log("Box2DView::onTouchEnded, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);
m_test->MouseUp(b2Vec2(nodePosition.x, nodePosition.y));
}
void Box2DView::onKeyPressed(EventKeyboard::KeyCode code, Event* event)
{
log("Box2dView:onKeyPressed, keycode: %d", static_cast<int>(code));
// m_test->Keyboard(static_cast<unsigned char>(code));
m_test->Keyboard((static_cast<int>(code) - 59));
}
void Box2DView::onKeyReleased(EventKeyboard::KeyCode code, Event* event)
{
log("onKeyReleased, keycode: %d", static_cast<int>(code));
// m_test->KeyboardUp(static_cast<unsigned char>(code));
m_test->Keyboard((static_cast<int>(code) - 59));
}
//void Box2DView::onEnter()
//{
// onEnter();
// ImGuiEXT::getInstance()->addFont(FileUtils::getInstance()->fullPathForFilename("fonts/arial.ttf"));
// ImGuiEXT::getInstance()->addRenderLoop("#im01", CC_CALLBACK_0(Box2DView::onDrawImGui, this), this->getScene());
//
//}
//void Box2DView::onExit()
//{
// ImGuiEXT::getInstance()->removeRenderLoop("#im01");
// onExit();
//}
void Box2DView::onDrawImGui()
{
// 1. Show a simple window
// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
{
static float f = 0.0f;
ImGui::Text("Hello, world!");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::ColorEdit3("clear color", (float*)&clear_color);
if (ImGui::Button("Test Window")) show_test_window ^= 1;
if (ImGui::Button("Another Window")) show_another_window ^= 1;
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
}
// 2. Show another simple window, this time using an explicit Begin/End pair
if (show_another_window)
{
ImGui::SetNextWindowSize(ImVec2(170, 80), ImGuiCond_FirstUseEver);
ImGui::Begin("Another Window", &show_another_window);
ImGui::Text("Hello");
ImGui::End();
}
// 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
if (show_test_window)
{
ImGui::ShowDemoWindow();
}
m_test->UpdateUI();
}

View File

@ -31,12 +31,25 @@
#include "renderer/CCCustomCommand.h"
extern cocos2d::Vec2 physicsDebugNodeOffset;
DEFINE_TEST_SUITE(Box2DTestBedTests);
class Box2DTestBed : public TestCase
class Test;
typedef Test* TestCreateFcn();
struct TestEntry
{
const char* category;
const char* name;
TestCreateFcn* createFcn;
};
#define MAX_TESTS 256
extern TestEntry g_testEntries[MAX_TESTS];
int RegisterTest(const char* category, const char* name, TestCreateFcn* fcn);
class Box2DTestBed : public TestCase, cocos2d::Layer
{
public:
static Box2DTestBed* createWithEntryID(int entryId);
@ -46,65 +59,38 @@ public:
void onEnter() override;
void onExit() override;
void onDrawImGui();
//void createResetButton();
//void reset(cocos2d::Ref* sender);
void initPhysics();
void update(float dt) override;
void createResetButton();
bool initWithEntryID(int entryId);
bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
cocos2d::DrawNode* draw = NULL;
private:
int m_entryID;
cocos2d::EventListenerTouchOneByOne* _touchListener;
};
void onKeyPressed(cocos2d::EventKeyboard::KeyCode code, cocos2d::Event* event) override;
void onKeyReleased(cocos2d::EventKeyboard::KeyCode code, cocos2d::Event* event) override;
struct TestEntry;
class Test;
class Box2DView : public cocos2d::Layer
{
public:
cocos2d::EventListenerTouchOneByOne* _touchListener;
cocos2d::EventListenerKeyboard* _keyboardListener;
TestEntry* m_entry;
Test* m_test;
int m_entryID;
void onDrawImGui();
Box2DView(void);
virtual ~Box2DView(void);
bool initWithEntryID(int entryId);
std::string title() const;
virtual void draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t flags) override;
// virtual void registerWithTouchDispatcher();
bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)override;
void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event)override;
void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event)override;
void onKeyPressed(cocos2d::EventKeyboard::KeyCode code, cocos2d::Event* event)override;
void onKeyReleased(cocos2d::EventKeyboard::KeyCode code, cocos2d::Event* event)override;
//virtual void accelerometer(UIAccelerometer* accelerometer, cocos2d::Acceleration* acceleration);
static Box2DView* viewWithEntryID(int entryId);
protected:
void onDraw(const cocos2d::Mat4& transform, uint32_t flags);
cocos2d::CallbackCommand _customCmd;
cocos2d::Mat4 _modelViewMV;
//cocos2d::extension::PhysicsDebugNode* _debugLayer; // weak ref
private:
b2World* world;
cocos2d::Texture2D* _spriteTexture;
// Debug stuff
cocos2d::DrawNode* debugDrawNode;
DebugDraw g_debugDraw;
};
#endif

View File

@ -16,18 +16,18 @@
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "test.h"
#include "tests/test.h"
#include "tests/settings.h"
#include "extensions/cocos-ext.h"
#include <stdio.h>
int m_textIncrement = 10;
void DestructionListener::SayGoodbye(b2Joint * joint)
{
if (test->m_mouseJoint == joint)
{
test->m_mouseJoint = nullptr;
test->m_mouseJoint = NULL;
}
else
{
@ -40,9 +40,10 @@ Test::Test()
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
m_world = new b2World(gravity);
m_bomb = nullptr;
m_bomb = NULL;
m_textLine = 30;
m_mouseJoint = nullptr;
m_textIncrement = 13;
m_mouseJoint = NULL;
m_pointCount = 0;
m_destructionListener.test = this;
@ -65,7 +66,7 @@ Test::~Test()
{
// By deleting the world, we delete the bomb, mouse joint, etc.
delete m_world;
m_world = nullptr;
m_world = NULL;
}
void Test::PreSolve(b2Contact * contact, const b2Manifold * oldManifold)
@ -103,8 +104,8 @@ void Test::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
void Test::DrawTitle(const char* string)
{
g_debugDraw.DrawString(5, DRAW_STRING_NEW_LINE, string);
m_textLine = 2 * DRAW_STRING_NEW_LINE;
g_debugDraw.DrawString(5, 5, string);
m_textLine = int32(26.0f);
}
class QueryCallback : public b2QueryCallback
@ -113,10 +114,10 @@ public:
QueryCallback(const b2Vec2& point)
{
m_point = point;
m_fixture = nullptr;
m_fixture = NULL;
}
bool ReportFixture(b2Fixture* fixture)
bool ReportFixture(b2Fixture* fixture) override
{
b2Body* body = fixture->GetBody();
if (body->GetType() == b2_dynamicBody)
@ -139,13 +140,13 @@ public:
b2Fixture* m_fixture;
};
void Test::MouseDown(const b2Vec2& p)
bool Test::MouseDown(const b2Vec2 & p)
{
m_mouseWorld = p;
if (m_mouseJoint != nullptr)
if (m_mouseJoint != NULL)
{
return;
return false;
}
// Make a small box.
@ -161,15 +162,22 @@ void Test::MouseDown(const b2Vec2& p)
if (callback.m_fixture)
{
float frequencyHz = 5.0f;
float dampingRatio = 0.7f;
b2Body* body = callback.m_fixture->GetBody();
b2MouseJointDef md;
md.bodyA = m_groundBody;
md.bodyB = body;
md.target = p;
md.maxForce = 1000.0f * body->GetMass();
m_mouseJoint = (b2MouseJoint*)m_world->CreateJoint(&md);
b2MouseJointDef jd;
jd.bodyA = m_groundBody;
jd.bodyB = body;
jd.target = p;
jd.maxForce = 1000.0f * body->GetMass();
b2LinearStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, jd.bodyA, jd.bodyB);
m_mouseJoint = (b2MouseJoint*)m_world->CreateJoint(&jd);
body->SetAwake(true);
return true;
}
return false;
}
void Test::SpawnBomb(const b2Vec2 & worldPt)
@ -196,7 +204,7 @@ void Test::ShiftMouseDown(const b2Vec2& p)
{
m_mouseWorld = p;
if (m_mouseJoint != nullptr)
if (m_mouseJoint != NULL)
{
return;
}
@ -206,11 +214,10 @@ void Test::ShiftMouseDown(const b2Vec2& p)
void Test::MouseUp(const b2Vec2 & p)
{
CCLOG("MouseUp p: x:%i y:%i", p.x, p.y);
if (m_mouseJoint)
{
m_world->DestroyJoint(m_mouseJoint);
m_mouseJoint = nullptr;
m_mouseJoint = NULL;
}
if (m_bombSpawning)
@ -241,7 +248,7 @@ void Test::LaunchBomb(const b2Vec2& position, const b2Vec2& velocity)
if (m_bomb)
{
m_world->DestroyBody(m_bomb);
m_bomb = nullptr;
m_bomb = NULL;
}
b2BodyDef bd;
@ -269,15 +276,15 @@ void Test::LaunchBomb(const b2Vec2& position, const b2Vec2& velocity)
m_bomb->CreateFixture(&fd);
}
void Test::Step(Settings* settings)
void Test::Step(Settings& settings)
{
float timeStep = settings->hz > 0.0f ? 1.0f / settings->hz : float(0.0f);
float timeStep = settings.m_hertz > 0.0f ? 1.0f / settings.m_hertz : float(0.0f);
if (settings->pause)
if (settings.m_pause)
{
if (settings->singleStep)
if (settings.m_singleStep)
{
settings->singleStep = 0;
settings.m_singleStep = 0;
}
else
{
@ -285,24 +292,24 @@ void Test::Step(Settings* settings)
}
g_debugDraw.DrawString(5, m_textLine, "****PAUSED****");
m_textLine += DRAW_STRING_NEW_LINE;
m_textLine += m_textIncrement;
}
uint32 flags = 0;
flags += settings->drawShapes * b2Draw::e_shapeBit;
flags += settings->drawJoints * b2Draw::e_jointBit;
flags += settings->drawAABBs * b2Draw::e_aabbBit;
flags += settings->drawCOMs * b2Draw::e_centerOfMassBit;
flags += settings.m_drawShapes * b2Draw::e_shapeBit;
flags += settings.m_drawJoints * b2Draw::e_jointBit;
flags += settings.m_drawAABBs * b2Draw::e_aabbBit;
flags += settings.m_drawCOMs * b2Draw::e_centerOfMassBit;
g_debugDraw.SetFlags(flags);
m_world->SetAllowSleeping(settings->enableSleep > 0);
m_world->SetWarmStarting(settings->enableWarmStarting > 0);
m_world->SetContinuousPhysics(settings->enableContinuous > 0);
m_world->SetSubStepping(settings->enableSubStepping > 0);
m_world->SetAllowSleeping(settings.m_enableSleep);
m_world->SetWarmStarting(settings.m_enableWarmStarting);
m_world->SetContinuousPhysics(settings.m_enableContinuous);
m_world->SetSubStepping(settings.m_enableSubStepping);
m_pointCount = 0;
m_world->Step(timeStep, settings->velocityIterations, settings->positionIterations);
m_world->Step(timeStep, settings.m_velocityIterations, settings.m_positionIterations);
m_world->DebugDraw();
@ -311,20 +318,20 @@ void Test::Step(Settings* settings)
++m_stepCount;
}
if (settings->drawStats)
if (settings.m_drawStats)
{
int32 bodyCount = m_world->GetBodyCount();
int32 contactCount = m_world->GetContactCount();
int32 jointCount = m_world->GetJointCount();
g_debugDraw.DrawString(5, m_textLine, "bodies/contacts/joints = %d/%d/%d", bodyCount, contactCount, jointCount);
m_textLine += DRAW_STRING_NEW_LINE;
m_textLine += m_textIncrement;
int32 proxyCount = m_world->GetProxyCount();
int32 height = m_world->GetTreeHeight();
int32 balance = m_world->GetTreeBalance();
float quality = m_world->GetTreeQuality();
g_debugDraw.DrawString(5, m_textLine, "proxies/height/balance/quality = %d/%d/%d/%g", proxyCount, height, balance, quality);
m_textLine += DRAW_STRING_NEW_LINE;
m_textLine += m_textIncrement;
}
// Track maximum profile times
@ -349,7 +356,7 @@ void Test::Step(Settings* settings)
m_totalProfile.broadphase += p.broadphase;
}
if (settings->drawProfile)
if (settings.m_drawProfile)
{
const b2Profile& p = m_world->GetProfile();
@ -369,35 +376,21 @@ void Test::Step(Settings* settings)
}
g_debugDraw.DrawString(5, m_textLine, "step [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.step, aveProfile.step, m_maxProfile.step);
m_textLine += DRAW_STRING_NEW_LINE;
m_textLine += m_textIncrement;
g_debugDraw.DrawString(5, m_textLine, "collide [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.collide, aveProfile.collide, m_maxProfile.collide);
m_textLine += DRAW_STRING_NEW_LINE;
m_textLine += m_textIncrement;
g_debugDraw.DrawString(5, m_textLine, "solve [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.solve, aveProfile.solve, m_maxProfile.solve);
m_textLine += DRAW_STRING_NEW_LINE;
m_textLine += m_textIncrement;
g_debugDraw.DrawString(5, m_textLine, "solve init [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.solveInit, aveProfile.solveInit, m_maxProfile.solveInit);
m_textLine += DRAW_STRING_NEW_LINE;
m_textLine += m_textIncrement;
g_debugDraw.DrawString(5, m_textLine, "solve velocity [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.solveVelocity, aveProfile.solveVelocity, m_maxProfile.solveVelocity);
m_textLine += DRAW_STRING_NEW_LINE;
m_textLine += m_textIncrement;
g_debugDraw.DrawString(5, m_textLine, "solve position [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.solvePosition, aveProfile.solvePosition, m_maxProfile.solvePosition);
m_textLine += DRAW_STRING_NEW_LINE;
m_textLine += m_textIncrement;
g_debugDraw.DrawString(5, m_textLine, "solveTOI [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.solveTOI, aveProfile.solveTOI, m_maxProfile.solveTOI);
m_textLine += DRAW_STRING_NEW_LINE;
m_textLine += m_textIncrement;
g_debugDraw.DrawString(5, m_textLine, "broad-phase [ave] (max) = %5.2f [%6.2f] (%6.2f)", p.broadphase, aveProfile.broadphase, m_maxProfile.broadphase);
m_textLine += DRAW_STRING_NEW_LINE;
}
if (m_mouseJoint)
{
b2Vec2 p1 = m_mouseJoint->GetAnchorB();
b2Vec2 p2 = m_mouseJoint->GetTarget();
b2Color c;
c.Set(0.0f, 1.0f, 0.0f);
g_debugDraw.DrawPoint(p1, 4.0f, c);
g_debugDraw.DrawPoint(p2, 4.0f, c);
c.Set(0.8f, 0.8f, 0.8f);
g_debugDraw.DrawSegment(p1, p2, c);
m_textLine += m_textIncrement;
}
if (m_bombSpawning)
@ -410,7 +403,7 @@ void Test::Step(Settings* settings)
g_debugDraw.DrawSegment(m_mouseWorld, m_bombSpawnPoint, c);
}
if (settings->drawContactPoints)
if (settings.m_drawContactPoints)
{
const float k_impulseScale = 0.1f;
const float k_axisScale = 0.3f;
@ -430,20 +423,20 @@ void Test::Step(Settings* settings)
g_debugDraw.DrawPoint(point->position, 5.0f, b2Color(0.3f, 0.3f, 0.95f));
}
if (settings->drawContactNormals == 1)
if (settings.m_drawContactNormals == 1)
{
b2Vec2 p1 = point->position;
b2Vec2 p2 = p1 + k_axisScale * point->normal;
g_debugDraw.DrawSegment(p1, p2, b2Color(0.9f, 0.9f, 0.9f));
}
else if (settings->drawContactImpulse == 1)
else if (settings.m_drawContactImpulse == 1)
{
b2Vec2 p1 = point->position;
b2Vec2 p2 = p1 + k_impulseScale * point->normalImpulse * point->normal;
g_debugDraw.DrawSegment(p1, p2, b2Color(0.9f, 0.9f, 0.3f));
}
if (settings->drawFrictionImpulse == 1)
if (settings.m_drawFrictionImpulse == 1)
{
b2Vec2 tangent = b2Cross(point->normal, 1.0f);
b2Vec2 p1 = point->position;
@ -458,21 +451,3 @@ void Test::ShiftOrigin(const b2Vec2& newOrigin)
{
m_world->ShiftOrigin(newOrigin);
}
TestEntry g_testEntries[MAX_TESTS] = { {nullptr} };
int g_testCount = 0;
int RegisterTest(const char* category, const char* name, TestCreateFcn* fcn)
{
int index = g_testCount;
if (index < MAX_TESTS)
{
g_testEntries[index] = { category, name, fcn };
++g_testCount;
return index;
}
return -1;
}

View File

@ -0,0 +1,83 @@
// MIT License
// Copyright (c) 2019 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
struct Settings
{
Settings()
{
Reset();
}
void Reset()
{
m_testIndex = 0;
m_windowWidth = 1600;
m_windowHeight = 900;
m_hertz = 60.0f;
m_velocityIterations = 8;
m_positionIterations = 1;
m_drawShapes = true;
m_drawJoints = true;
m_drawAABBs = false;
m_drawContactPoints = false;
m_drawContactNormals = false;
m_drawContactImpulse = false;
m_drawFrictionImpulse = false;
m_drawCOMs = false;
m_drawStats = false;
m_drawProfile = false;
m_enableWarmStarting = true;
m_enableContinuous = true;
m_enableSubStepping = false;
m_enableSleep = true;
m_pause = false;
m_singleStep = false;
}
void Save();
void Load();
int m_testIndex;
int m_windowWidth;
int m_windowHeight;
float m_hertz;
int m_velocityIterations;
int m_positionIterations;
bool m_drawShapes;
bool m_drawJoints;
bool m_drawAABBs;
bool m_drawContactPoints;
bool m_drawContactNormals;
bool m_drawContactImpulse;
bool m_drawFrictionImpulse;
bool m_drawCOMs;
bool m_drawStats;
bool m_drawProfile;
bool m_enableWarmStarting;
bool m_enableContinuous;
bool m_enableSubStepping;
bool m_enableSleep;
bool m_pause;
bool m_singleStep;
};

View File

@ -50,7 +50,7 @@ inline float RandomFloat(float lo, float hi)
return r;
}
/// Test settings. Some can be controlled in the GUI.
// //Test settings. Some can be controlled in the GUI.
//struct Settings
//{
// Settings()
@ -113,18 +113,18 @@ public:
};
const int32 k_maxContactPoints = 2048;
//
//struct ContactPoint
//{
// b2Fixture* fixtureA;
// b2Fixture* fixtureB;
// b2Vec2 normal;
// b2Vec2 position;
// b2PointState state;
// float normalImpulse;
// float tangentImpulse;
// float separation;
//};
struct ContactPoint
{
b2Fixture* fixtureA;
b2Fixture* fixtureB;
b2Vec2 normal;
b2Vec2 position;
b2PointState state;
float normalImpulse;
float tangentImpulse;
float separation;
};
class Test : public b2ContactListener
{
public:
@ -133,7 +133,7 @@ public:
virtual ~Test();
void DrawTitle(const char* string);
virtual void Step(Settings* settings);
virtual void Step(Settings& settings);
virtual void UpdateUI() {}
virtual void Keyboard(int key) { B2_NOT_USED(key); }
virtual void KeyboardUp(int key) { B2_NOT_USED(key); }
@ -189,19 +189,18 @@ protected:
};
typedef Test* TestCreateFcn();
int RegisterTest(const char* category, const char* name, TestCreateFcn* fcn);
//
struct TestEntry
{
const char* category;
const char* name;
TestCreateFcn* createFcn;
};
#define MAX_TESTS 256
extern TestEntry g_testEntries[MAX_TESTS];
extern int g_testCount;
////
//struct TestEntry
//{
// const char* category;
// const char* name;
// TestCreateFcn* createFcn;
//};
//
//#define MAX_TESTS 256
//extern TestEntry g_testEntries[MAX_TESTS];
//extern int g_testCount;
#endif

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class AddPair : public Test
{
@ -68,6 +68,4 @@ public:
}
};
static int testIndex = RegisterTest("Benchmark", "Add Pair", AddPair::Create);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
// This test shows how to apply forces and torques to a body.
// It also shows how to use the friction joint that can be useful
@ -167,7 +167,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
g_debugDraw.DrawString(5, m_textLine, "Forward (W), Turn (A) and (D)");
m_textLine += m_textIncrement;
@ -192,30 +192,6 @@ public:
Test::Step(settings);
}
void Keyboard(int key) override
{
switch (key)
{
case GLFW_KEY_W:
{
b2Vec2 f = m_body->GetWorldVector(b2Vec2(0.0f, -50.0f));
b2Vec2 p = m_body->GetWorldPoint(b2Vec2(0.0f, 3.0f));
m_body->ApplyForce(f, p, true);
}
break;
case GLFW_KEY_A:
m_body->ApplyTorque(10.0f, true);
break;
case GLFW_KEY_D:
m_body->ApplyTorque(-10.0f, true);
break;
}
// Test::Step(&settings);
}
static Test* Create()
{
return new ApplyForce;

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class BodyTypes : public Test
{
@ -128,7 +128,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
// Drive the kinematic body.
if (m_platform->GetType() == b2_kinematicBody)

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
extern B2_API bool g_blockSolve;
@ -125,7 +125,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
g_debugDraw.DrawString(5, m_textLine, "Press: (,) to launch a bullet.");

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
// This is used to test sensor shapes.
class Breakable : public Test
@ -119,7 +119,7 @@ public:
body2->SetLinearVelocity(velocity2);
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
if (m_break)
{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class Bridge : public Test
{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class BulletTest : public Test
{
@ -94,7 +94,7 @@ public:
b2_toiMaxRootIters = 0;
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
// It is difficult to make a cantilever made of links completely rigid with weld joints.
// You will have to use a high number of iterations to make them stiff.

View File

@ -1,6 +1,7 @@
// MIT License
// Copyright (c) 2019 Erin Catto
// Copyright(c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@ -20,7 +21,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
// This is a fun demo that shows off the wheel joint
class Car : public Test
@ -258,12 +260,12 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
g_debugDraw.DrawString(5, m_textLine, "Keys: left = a, brake = s, right = d, hz down = q, hz up = e");
m_textLine += m_textIncrement;
// g_camera.m_center.x = m_car->GetPosition().x;
g_debugDrawTestBed.debugNodeOffset.x = 250 + m_car->GetPosition().x * -g_debugDrawTestBed.mRatio;
Test::Step(settings);
}

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
#define TEST_BAD_BODY 0

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class ChainProblem : public Test
{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
/// This is a test of typical character collision scenarios. This does not
/// show how you should implement a character in your application.
@ -230,7 +230,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
b2Vec2 v = m_character->GetLinearVelocity();
v.x = -5.0f;

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class CircleStack : public Test
{
@ -61,21 +61,21 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
for (int32 i = 0; i < e_count; ++i)
{
printf("%g ", m_bodies[i]->GetWorldCenter().y);
}
//for (int32 i = 0; i < e_count; ++i)
//{
// printf("%g ", m_bodies[i]->GetWorldCenter().y);
//}
for (int32 i = 0; i < e_count; ++i)
{
printf("%g ", m_bodies[i]->GetLinearVelocity().y);
}
//for (int32 i = 0; i < e_count; ++i)
//{
// printf("%g ", m_bodies[i]->GetLinearVelocity().y);
//}
printf("\n");
//printf("\n");
}
static Test* Create()

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
// This is a test of collision filtering.
// There is a triangle, a box, and a circle.

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
#include <algorithm>
@ -122,7 +122,7 @@ public:
body6->CreateFixture(&circleShapeDef);
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
#include "ImGuiEXT/CCImGuiEXT.h"
class CompoundShapes : public Test

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class Confined : public Test
{
@ -115,7 +115,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
bool sleeping = true;
for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext())

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class ContinuousTest : public Test
{
@ -110,7 +110,7 @@ public:
m_body->SetAngularVelocity(m_angularVelocity);
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
@ -118,9 +118,9 @@ public:
if (b2_gjkCalls > 0)
{
//g_debugDraw.DrawString(5, m_textLine, "gjk calls = %d, ave gjk iters = %3.1f, max gjk iters = %d",
// b2_gjkCalls, b2_gjkIters / float(b2_gjkCalls), b2_gjkMaxIters);
//m_textLine += m_textIncrement;
g_debugDraw.DrawString(5, m_textLine, "gjk calls = %d, ave gjk iters = %3.1f, max gjk iters = %d",
b2_gjkCalls, b2_gjkIters / float(b2_gjkCalls), b2_gjkMaxIters);
m_textLine += m_textIncrement;
}
extern B2_API int32 b2_toiCalls, b2_toiIters;

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class ConvexHull : public Test
{
@ -70,7 +70,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class ConveyorBelt : public Test
{
@ -85,7 +85,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
}

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
#include "ImGuiEXT/CCImGuiEXT.h"
// This tests distance joints, body destruction, and joint destruction.

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
#include "box2d/b2_distance.h"
class DistanceTest : public Test
@ -48,7 +48,7 @@ public:
return new DistanceTest;
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class Dominos : public Test
{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
// This test holds worlds dumped using b2World::Dump.
class DumpLoader : public Test
@ -61,7 +61,7 @@ public:
m_ball->ApplyForceToCenter(b2Vec2(-1000, -400), true);
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
b2Vec2 v = m_ball->GetLinearVelocity();
float omega = m_ball->GetAngularVelocity();

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class DynamicTree : public Test
{
@ -65,7 +65,7 @@ public:
return new DynamicTree;
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
B2_NOT_USED(settings);
@ -198,8 +198,8 @@ private:
void GetRandomAABB(b2AABB* aabb)
{
b2Vec2 w; w.Set(2.0f * m_proxyExtent, 2.0f * m_proxyExtent);
aabb->lowerBound.x = -m_proxyExtent;
aabb->lowerBound.y = -m_proxyExtent + m_worldExtent;
//aabb->lowerBound.x = -m_proxyExtent;
//aabb->lowerBound.y = -m_proxyExtent + m_worldExtent;
aabb->lowerBound.x = RandomFloat(-m_worldExtent, m_worldExtent);
aabb->lowerBound.y = RandomFloat(0.0f, 2.0f * m_worldExtent);
aabb->upperBound = aabb->lowerBound + w;

View File

@ -20,8 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//#include "settings.h"
#include "../test.h"
#include "settings.h"
#include "test.h"
class EdgeShapesCallback : public b2RayCastCallback
{
@ -200,13 +200,13 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
bool advanceRay = 0.1;// settings.m_pause == 0 || settings.m_singleStep;
bool advanceRay = settings.m_pause == 0 || settings.m_singleStep;
Test::Step(settings);
g_debugDraw.DrawString(5, m_textLine, "Press 1-5 to drop stuff");
// m_textLine += m_textIncrement;
m_textLine += m_textIncrement;
float L = 25.0f;
b2Vec2 point1(0.0f, 10.0f);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
#include "ImGuiEXT/CCImGuiEXT.h"
class EdgeTest : public Test
@ -251,7 +251,7 @@ public:
ImGui::End();
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
//if (glfwGetKey(g_mainWindow, GLFW_KEY_A) == GLFW_PRESS)
//{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class Friction : public Test
{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class GearJoint : public Test
{
@ -148,7 +148,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class Heavy1 : public Test
{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class Heavy2 : public Test
{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class MobileBalanced : public Test
{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class MobileUnbalanced : public Test
{

View File

@ -20,8 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//#include "settings.h"
#include "../test.h"
#include "settings.h"
#include "test.h"
/// This test shows how to use a motor joint. A motor joint
/// can be used to animate a dynamic body. With finite motor forces
@ -82,12 +82,12 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
//if (m_go && settings.m_hertz > 0.0f)
//{
// m_time += 1.0f / settings.m_hertz;
//}
if (m_go && settings.m_hertz > 0.0f)
{
m_time += 1.0f / settings.m_hertz;
}
b2Vec2 linearOffset;
linearOffset.x = 6.0f * sinf(2.0f * m_time);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
/// This tests bullet collision and provides an example of a gameplay scenario.
/// This also uses a loop shape.
@ -116,7 +116,7 @@ public:
m_button = false;
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
if (m_button)
{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class Platformer : public Test
{
@ -110,7 +110,7 @@ public:
#endif
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,24 +20,24 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class PolygonCollision : public Test
{
public:
PolygonCollision()
{
//{
// m_polygonA.SetAsBox(0.2f, 0.4f);
// m_transformA.Set(b2Vec2(0.0f, 0.0f), 0.0f);
//}
{
m_polygonA.SetAsBox(0.2f, 0.4f);
m_transformA.Set(b2Vec2(0.0f, 0.0f), 0.0f);
}
//{
// m_polygonB.SetAsBox(0.5f, 0.5f);
// m_positionB.Set(19.345284f, 1.5632932f);
// m_angleB = 1.9160721f;
// m_transformB.Set(m_positionB, m_angleB);
//}
{
m_polygonB.SetAsBox(0.5f, 0.5f);
m_positionB.Set(19.345284f, 1.5632932f);
m_angleB = 1.9160721f;
m_transformB.Set(m_positionB, m_angleB);
}
}
static Test* Create()
@ -45,38 +45,38 @@ public:
return new PolygonCollision;
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
// B2_NOT_USED(settings);
B2_NOT_USED(settings);
//b2Manifold manifold;
//b2CollidePolygons(&manifold, &m_polygonA, m_transformA, &m_polygonB, m_transformB);
b2Manifold manifold;
b2CollidePolygons(&manifold, &m_polygonA, m_transformA, &m_polygonB, m_transformB);
//b2WorldManifold worldManifold;
//worldManifold.Initialize(&manifold, m_transformA, m_polygonA.m_radius, m_transformB, m_polygonB.m_radius);
b2WorldManifold worldManifold;
worldManifold.Initialize(&manifold, m_transformA, m_polygonA.m_radius, m_transformB, m_polygonB.m_radius);
// g_debugDraw.DrawString(5, m_textLine, "point count = %d", manifold.pointCount);
// m_textLine += m_textIncrement;
g_debugDraw.DrawString(5, m_textLine, "point count = %d", manifold.pointCount);
m_textLine += m_textIncrement;
//{
// b2Color color(0.9f, 0.9f, 0.9f);
// b2Vec2 v[b2_maxPolygonVertices];
// for (int32 i = 0; i < m_polygonA.m_count; ++i)
// {
// v[i] = b2Mul(m_transformA, m_polygonA.m_vertices[i]);
// }
// g_debugDraw.DrawPolygon(v, m_polygonA.m_count, color);
// for (int32 i = 0; i < m_polygonB.m_count; ++i)
// {
// v[i] = b2Mul(m_transformB, m_polygonB.m_vertices[i]);
// }
// g_debugDraw.DrawPolygon(v, m_polygonB.m_count, color);
//}
// for (int32 i = 0; i < manifold.pointCount; ++i)
{
// g_debugDraw.DrawPoint(worldManifold.points[i], 4.0f, b2Color(0.9f, 0.3f, 0.3f));
b2Color color(0.9f, 0.9f, 0.9f);
b2Vec2 v[b2_maxPolygonVertices];
for (int32 i = 0; i < m_polygonA.m_count; ++i)
{
v[i] = b2Mul(m_transformA, m_polygonA.m_vertices[i]);
}
g_debugDraw.DrawPolygon(v, m_polygonA.m_count, color);
for (int32 i = 0; i < m_polygonB.m_count; ++i)
{
v[i] = b2Mul(m_transformB, m_polygonB.m_vertices[i]);
}
g_debugDraw.DrawPolygon(v, m_polygonB.m_count, color);
}
for (int32 i = 0; i < manifold.pointCount; ++i)
{
g_debugDraw.DrawPoint(worldManifold.points[i], 4.0f, b2Color(0.9f, 0.3f, 0.3f));
}
Test::Step(settings);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
/// This tests stacking. It also shows how to use b2World::Query
/// and b2TestOverlap.
@ -225,7 +225,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,8 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//#include "settings.h"
#include "../test.h"
#include "settings.h"
#include "test.h"
#include "ImGuiEXT/CCImGuiEXT.h"
// Test the prismatic joint with limits and motor options.
@ -96,10 +96,10 @@ public:
ImGui::End();
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
float force = m_joint->GetMotorForce(settings->hz);
float force = m_joint->GetMotorForce(settings.m_hertz);
g_debugDraw.DrawString(5, m_textLine, "Motor Force = %4.0f", force);
m_textLine += m_textIncrement;
}

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class PulleyJoint : public Test
{
@ -75,7 +75,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class Pyramid : public Test
{
@ -71,7 +71,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,8 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//#include "settings.h"
#include "../test.h"
#include "settings.h"
#include "test.h"
#include "ImGuiEXT/CCImGuiEXT.h"
enum
@ -293,7 +293,7 @@ public:
void UpdateUI() override
{
ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
ImGui::SetNextWindowPos(ImVec2(g_debugDrawTestBed.debugNodeOffset.x, g_debugDrawTestBed.debugNodeOffset.y));
ImGui::SetNextWindowSize(ImVec2(210.0f, 285.0f));
ImGui::Begin("Ray-cast Controls", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
@ -341,7 +341,7 @@ public:
ImGui::End();
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
// Note: even with a restitution of 1.0, there is some energy change
// due to position correction.

View File

@ -20,8 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//#include "settings.h"
#include "../test.h"
#include "settings.h"
#include "test.h"
#include "ImGuiEXT/CCImGuiEXT.h"
class RevoluteJoint : public Test
@ -133,15 +133,15 @@ public:
ImGui::End();
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
float torque1 = m_joint1->GetMotorTorque(settings->hz);
float torque1 = m_joint1->GetMotorTorque(settings.m_hertz);
g_debugDraw.DrawString(5, m_textLine, "Motor Torque 1= %4.0f", torque1);
m_textLine += m_textIncrement;
float torque2 = m_joint2->GetMotorTorque(1.0); //settings.hz);
float torque2 = m_joint2->GetMotorTorque(settings.m_hertz);
g_debugDraw.DrawString(5, m_textLine, "Motor Torque 2= %4.0f", torque2);
m_textLine += m_textIncrement;
}

View File

@ -20,8 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//#include "settings.h"
#include "../test.h"
#include "settings.h"
#include "test.h"
#include "box2d/b2_rope.h"
#include "ImGuiEXT/CCImGuiEXT.h"
@ -232,11 +232,11 @@ public:
ImGui::End();
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
float dt = 0.5; // settings.hz > 0.0f ? 1.0f / settings.hz : 0.0f;
float dt = settings.m_hertz > 0.0f ? 1.0f / settings.m_hertz : 0.0f;
if (settings->pause == 1 && settings->singleStep == 0)
if (settings.m_pause == 1 && settings.m_singleStep == 0)
{
dt = 0.0f;
}

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
#include "ImGuiEXT/CCImGuiEXT.h"
// This shows how to use sensor shapes. Sensors don't have collision, but report overlap events.
@ -148,7 +148,7 @@ public:
ImGui::End();
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -0,0 +1,83 @@
// MIT License
// Copyright (c) 2019 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
struct Settings
{
Settings()
{
Reset();
}
void Reset()
{
m_testIndex = 0;
m_windowWidth = 1600;
m_windowHeight = 900;
m_hertz = 60.0f;
m_velocityIterations = 8;
m_positionIterations = 3;
m_drawShapes = true;
m_drawJoints = true;
m_drawAABBs = false;
m_drawContactPoints = false;
m_drawContactNormals = false;
m_drawContactImpulse = false;
m_drawFrictionImpulse = false;
m_drawCOMs = false;
m_drawStats = false;
m_drawProfile = false;
m_enableWarmStarting = true;
m_enableContinuous = true;
m_enableSubStepping = false;
m_enableSleep = true;
m_pause = false;
m_singleStep = false;
}
void Save();
void Load();
int m_testIndex;
int m_windowWidth;
int m_windowHeight;
float m_hertz;
int m_velocityIterations;
int m_positionIterations;
bool m_drawShapes;
bool m_drawJoints;
bool m_drawAABBs;
bool m_drawContactPoints;
bool m_drawContactNormals;
bool m_drawContactImpulse;
bool m_drawFrictionImpulse;
bool m_drawCOMs;
bool m_drawStats;
bool m_drawProfile;
bool m_enableWarmStarting;
bool m_enableContinuous;
bool m_enableSubStepping;
bool m_enableSleep;
bool m_pause;
bool m_singleStep;
};

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
#include "box2d/b2_distance.h"
class ShapeCast : public Test
@ -90,7 +90,7 @@ public:
return new ShapeCast;
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class ShapeEditing : public Test
{
@ -51,41 +51,41 @@ public:
m_sensor = false;
}
//void Keyboard(int key) override
//{
// switch (key)
// {
// case GLFW_KEY_C:
// if (m_fixture2 == NULL)
// {
// b2CircleShape shape;
// shape.m_radius = 3.0f;
// shape.m_p.Set(0.5f, -4.0f);
// m_fixture2 = m_body->CreateFixture(&shape, 10.0f);
// m_body->SetAwake(true);
// }
// break;
void Keyboard(int key) override
{
switch (key)
{
case GLFW_KEY_C:
if (m_fixture2 == NULL)
{
b2CircleShape shape;
shape.m_radius = 3.0f;
shape.m_p.Set(0.5f, -4.0f);
m_fixture2 = m_body->CreateFixture(&shape, 10.0f);
m_body->SetAwake(true);
}
break;
// case GLFW_KEY_D:
// if (m_fixture2 != NULL)
// {
// m_body->DestroyFixture(m_fixture2);
// m_fixture2 = NULL;
// m_body->SetAwake(true);
// }
// break;
case GLFW_KEY_D:
if (m_fixture2 != NULL)
{
m_body->DestroyFixture(m_fixture2);
m_fixture2 = NULL;
m_body->SetAwake(true);
}
break;
// case GLFW_KEY_S:
// if (m_fixture2 != NULL)
// {
// m_sensor = !m_sensor;
// m_fixture2->SetSensor(m_sensor);
// }
// break;
// }
//}
case GLFW_KEY_S:
if (m_fixture2 != NULL)
{
m_sensor = !m_sensor;
m_fixture2->SetSensor(m_sensor);
}
break;
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
g_debugDraw.DrawString(5, m_textLine, "Press: (c) create a shape, (d) destroy a shape.");

View File

@ -2,7 +2,7 @@
Test case for collision/jerking issue.
*/
#include "../test.h"
#include "test.h"
#include <vector>
#include <iostream>
@ -107,7 +107,7 @@ public:
//g_camera.m_center = b2Vec2(m_platform_width / 2.0f, 0.0f);
//g_camera.m_zoom = 0.4f;
//m_fixed_camera = true;
m_fixed_camera = true;
}
void Keyboard(int key) override
@ -124,15 +124,15 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
g_debugDraw.DrawString(5, m_textLine, "Keys: c = Camera fixed/tracking");
m_textLine += m_textIncrement;
// if(!m_fixed_camera)
// {
if(!m_fixed_camera)
{
//g_camera.m_center = m_skier->GetPosition();
// }
}
Test::Step(settings);
}

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
// A basic slider crank created for GDC tutorial: Understanding Constraints
class SliderCrank1 : public Test

View File

@ -20,8 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//#include "settings.h"
#include "../test.h"
#include "settings.h"
#include "test.h"
// A motor driven slider crank with joint friction.
@ -138,12 +138,12 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
g_debugDraw.DrawString(5, m_textLine, "Keys: (f) toggle friction, (m) toggle motor");
m_textLine += m_textIncrement;
float torque = m_joint1->GetMotorTorque(settings->hz);
float torque = m_joint1->GetMotorTorque(settings.m_hertz);
g_debugDraw.DrawString(5, m_textLine, "Motor Torque = %5.0f", (float) torque);
m_textLine += m_textIncrement;
}

View File

@ -0,0 +1,148 @@
/*
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef TEST_H
#define TEST_H
#include "box2d/box2d.h"
#include "extensions/cocos-ext.h"
#include <cstdlib>
class Test;
struct Settings;
#define RAND_LIMIT 32767
#define DRAW_STRING_NEW_LINE 25
/// Random number in range [-1,1]
inline float RandomFloat()
{
float r = (float)(std::rand() & (RAND_LIMIT));
r /= RAND_LIMIT;
r = 2.0f * r - 1.0f;
return r;
}
/// Random floating point number in range [lo, hi]
inline float RandomFloat(float lo, float hi)
{
float r = (float)(std::rand() & (RAND_LIMIT));
r /= RAND_LIMIT;
r = (hi - lo) * r + lo;
return r;
}
// This is called when a joint in the world is implicitly destroyed
// because an attached body is destroyed. This gives us a chance to
// nullify the mouse joint.
class DestructionListener : public b2DestructionListener
{
public:
void SayGoodbye(b2Fixture* fixture) { B2_NOT_USED(fixture); }
void SayGoodbye(b2Joint* joint);
Test* test;
};
const int32 k_maxContactPoints = 2048;
struct ContactPoint
{
b2Fixture* fixtureA;
b2Fixture* fixtureB;
b2Vec2 normal;
b2Vec2 position;
b2PointState state;
float normalImpulse;
float tangentImpulse;
float separation;
};
class Test : public b2ContactListener
{
public:
Test();
virtual ~Test();
void DrawTitle(const char* string);
virtual void Step(Settings& settings);
virtual void UpdateUI() {}
virtual void Keyboard(int key) { B2_NOT_USED(key); }
virtual void KeyboardUp(int key) { B2_NOT_USED(key); }
void ShiftMouseDown(const b2Vec2& p);
virtual bool MouseDown(const b2Vec2& p);
virtual void MouseUp(const b2Vec2& p);
virtual void MouseMove(const b2Vec2& p);
void LaunchBomb();
void LaunchBomb(const b2Vec2& position, const b2Vec2& velocity);
void SpawnBomb(const b2Vec2& worldPt);
void CompleteBombSpawn(const b2Vec2& p);
// Let derived tests know that a joint was destroyed.
virtual void JointDestroyed(b2Joint* joint) { B2_NOT_USED(joint); }
// Callbacks for derived classes.
virtual void BeginContact(b2Contact* contact) override { B2_NOT_USED(contact); }
virtual void EndContact(b2Contact* contact) override { B2_NOT_USED(contact); }
virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) override;
virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) override
{
B2_NOT_USED(contact);
B2_NOT_USED(impulse);
}
void ShiftOrigin(const b2Vec2& newOrigin);
DebugDraw g_debugDraw;
DebugDraw g_debugDrawTestBed;
cocos2d::DrawNode* debugDrawNode;
b2World* m_world;
protected:
friend class DestructionListener;
friend class BoundaryListener;
friend class ContactListener;
b2Body* m_groundBody;
b2AABB m_worldAABB;
ContactPoint m_points[k_maxContactPoints];
int32 m_pointCount;
DestructionListener m_destructionListener;
int32 m_textLine;
b2Body* m_bomb;
b2MouseJoint* m_mouseJoint;
b2Vec2 m_bombSpawnPoint;
bool m_bombSpawning;
b2Vec2 m_mouseWorld;
int32 m_stepCount;
int32 m_textIncrement;
b2Profile m_maxProfile;
b2Profile m_totalProfile;
};
typedef Test* TestCreateFcn();
int RegisterTest(const char* category, const char* name, TestCreateFcn* fcn);
#endif

View File

@ -23,7 +23,7 @@
// Inspired by a contribution from roman_m
// Dimensions scooped from APE (http://www.cove.org/ape/index.htm)
#include "../test.h"
#include "test.h"
class TheoJansen : public Test
{
@ -220,7 +220,7 @@ public:
CreateLeg(1.0f, wheelAnchor);
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
g_debugDraw.DrawString(5, m_textLine, "Keys: left = a, brake = s, right = d, toggle motor = m");
m_textLine += m_textIncrement;
@ -230,8 +230,6 @@ public:
void Keyboard(int key) override
{
CCLOG("Keyboard(int key %i A:%i)", key, GLFW_KEY_A);
switch (key)
{
case GLFW_KEY_A:

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
/// This stress tests the dynamic tree broad-phase. This also shows that tile
/// based collision is _not_ smooth due to Box2D not knowing about adjacency.
@ -123,7 +123,7 @@ public:
m_createTime = timer.GetMilliseconds();
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
const b2ContactManager& cm = m_world->GetContactManager();
int32 height = cm.m_broadPhase.GetTreeHeight();
@ -141,10 +141,10 @@ public:
//b2DynamicTree* tree = &m_world->m_contactManager.m_broadPhase.m_tree;
if (m_stepCount == 400)
{
//if (m_stepCount == 400)
//{
// tree->RebuildBottomUp();
}
//}
}
static Test* Create()

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
#include "box2d/b2_time_of_impact.h"
class TimeOfImpact : public Test
@ -37,7 +37,7 @@ public:
return new TimeOfImpact;
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
@ -55,8 +55,8 @@ public:
sweepB.a = 513.62781f; // - 162.0f * b2_pi;
sweepB.localCenter.SetZero();
sweepB.a0 -= 300.0f * b2_pi;
sweepB.a -= 300.0f * b2_pi;
//sweepB.a0 -= 300.0f * b2_pi;
//sweepB.a -= 300.0f * b2_pi;
b2TOIInput input;
input.proxyA.Set(&m_shapeA, 0);
@ -89,7 +89,7 @@ public:
b2Transform transformB;
sweepB.GetTransform(&transformB, 0.0f);
b2Vec2 localPoint(2.0f, -0.1f);
//b2Vec2 localPoint(2.0f, -0.1f);
for (int32 i = 0; i < m_shapeB.m_count; ++i)
{

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
class Tumbler : public Test
{
@ -71,7 +71,7 @@ public:
m_count = 0;
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
// Test distance joints, body destruction, and joint destruction.
class Web : public Test
@ -157,7 +157,7 @@ public:
}
}
void Keyboard(int key)
void Keyboard(int key) override
{
switch (key)
{
@ -187,7 +187,7 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
g_debugDraw.DrawString(5, m_textLine, "Press: (b) to delete a body, (j) to delete a joint");

View File

@ -20,8 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//#include "settings.h"
#include "../test.h"
#include "settings.h"
#include "test.h"
#include "ImGuiEXT/CCImGuiEXT.h"
// Test the wheel joint with motor, spring, and limit options.
@ -75,15 +75,15 @@ public:
}
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
float torque = m_joint->GetMotorTorque(settings->hz);
float torque = m_joint->GetMotorTorque(settings.m_hertz);
g_debugDraw.DrawString(5, m_textLine, "Motor Torque = %4.0f", torque);
m_textLine += m_textIncrement;
b2Vec2 F = m_joint->GetReactionForce(settings->hz);
b2Vec2 F = m_joint->GetReactionForce(settings.m_hertz);
g_debugDraw.DrawString(5, m_textLine, "Reaction Force = (%4.1f, %4.1f)", F.x, F.y);
m_textLine += m_textIncrement;
}

View File

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "../test.h"
#include "test.h"
#include "ImGuiEXT/CCImGuiEXT.h"
/// This test shows how a distance joint can be used to stabilize a chain of
@ -135,10 +135,9 @@ public:
}
ImGui::End();
}
void Step(Settings* settings) override
void Step(Settings& settings) override
{
Test::Step(settings);
@ -151,8 +150,6 @@ public:
g_debugDraw.DrawString(5, m_textLine, "Distance Joint OFF");
}
m_textLine += m_textIncrement;
UpdateUI();
}
static Test* Create()