2020-11-20 11:51:47 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-08-05 17:23:55 +08:00
|
|
|
Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
|
2020-11-23 14:46:09 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2020-11-23 14:46:09 +08:00
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
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:
|
2020-11-23 14:46:09 +08:00
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2020-11-23 14:46:09 +08:00
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "Box2dTest.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
#include "extensions/cocos-ext.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCCustomCommand.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
|
|
|
USING_NS_AX_EXT;
|
2020-11-20 11:51:47 +08:00
|
|
|
|
|
|
|
#define PTM_RATIO 32
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
enum
|
|
|
|
{
|
2020-11-20 11:51:47 +08:00
|
|
|
kTagParentNode = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
Box2DTests::Box2DTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(Box2DTest);
|
|
|
|
}
|
|
|
|
|
2021-08-06 19:40:32 +08:00
|
|
|
std::string Box2DTest::title() const
|
|
|
|
{
|
|
|
|
return "Box2D - Basic";
|
|
|
|
}
|
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
bool Box2DTest::init()
|
|
|
|
{
|
|
|
|
if (!TestCase::init())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto touchListener = EventListenerTouchAllAtOnce::create();
|
2022-07-16 10:43:05 +08:00
|
|
|
touchListener->onTouchesEnded = AX_CALLBACK_2(Box2DTest::onTouchesEnded, this);
|
2020-11-20 11:51:47 +08:00
|
|
|
dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
|
|
|
|
|
|
|
|
// init physics
|
|
|
|
this->initPhysics();
|
|
|
|
// create reset button
|
|
|
|
this->createResetButton();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// Set up sprite
|
2020-11-20 11:51:47 +08:00
|
|
|
#if 1
|
|
|
|
// Use batch node. Faster
|
2021-12-31 12:12:40 +08:00
|
|
|
auto parent = SpriteBatchNode::create("Images/blocks.png", 100);
|
2020-11-20 11:51:47 +08:00
|
|
|
_spriteTexture = parent->getTexture();
|
|
|
|
#else
|
|
|
|
// doesn't use batch node. Slower
|
|
|
|
_spriteTexture = Director::getInstance()->getTextureCache()->addImage("Images/blocks.png");
|
2021-12-31 12:12:40 +08:00
|
|
|
auto parent = Node::create();
|
2020-11-20 11:51:47 +08:00
|
|
|
#endif
|
|
|
|
addChild(parent, 0, kTagParentNode);
|
|
|
|
|
|
|
|
addNewSpriteAtPosition(VisibleRect::center());
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto label = Label::createWithTTF("Tap screen add boxes.\nSome objects be only visible with debug on.",
|
|
|
|
"fonts/Marker Felt.ttf", 12.0f);
|
2020-11-20 11:51:47 +08:00
|
|
|
addChild(label, 0);
|
|
|
|
label->setColor(Color3B(0, 0, 255));
|
2021-12-31 12:12:40 +08:00
|
|
|
label->setPosition(VisibleRect::center().x - 50, VisibleRect::top().y - 60);
|
2020-11-20 11:51:47 +08:00
|
|
|
|
2021-08-05 17:23:55 +08:00
|
|
|
// menu for debug layer
|
|
|
|
MenuItemFont::setFontSize(18);
|
2022-07-16 10:43:05 +08:00
|
|
|
auto item = MenuItemFont::create("Toggle debug", AX_CALLBACK_1(Box2DTest::toggleDebugCallback, this));
|
2021-08-05 17:23:55 +08:00
|
|
|
|
|
|
|
auto menu = Menu::create(item, nullptr);
|
|
|
|
this->addChild(menu);
|
|
|
|
menu->setPosition(VisibleRect::right().x - 100, VisibleRect::top().y - 60);
|
|
|
|
|
2021-08-12 15:34:24 +08:00
|
|
|
drawBox2D = g_debugDraw.GetDrawNode();
|
2021-08-05 17:23:55 +08:00
|
|
|
addChild(drawBox2D, 100);
|
|
|
|
drawBox2D->setOpacity(150);
|
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
scheduleUpdate();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Box2DTest::Box2DTest() : _spriteTexture(nullptr), world(nullptr) {}
|
2020-11-20 11:51:47 +08:00
|
|
|
|
|
|
|
Box2DTest::~Box2DTest()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(world);
|
2020-11-20 11:51:47 +08:00
|
|
|
}
|
|
|
|
|
2021-08-05 17:23:55 +08:00
|
|
|
void Box2DTest::toggleDebugCallback(Ref* sender)
|
|
|
|
{
|
2021-08-06 19:40:32 +08:00
|
|
|
showDebugDraw = !showDebugDraw;
|
2021-12-31 12:12:40 +08:00
|
|
|
drawBox2D->clear();
|
2021-08-05 17:23:55 +08:00
|
|
|
}
|
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
void Box2DTest::initPhysics()
|
|
|
|
{
|
|
|
|
b2Vec2 gravity;
|
|
|
|
gravity.Set(0.0f, -10.0f);
|
|
|
|
world = new b2World(gravity);
|
|
|
|
|
|
|
|
// Do we want to let bodies sleep?
|
|
|
|
world->SetAllowSleeping(true);
|
|
|
|
|
|
|
|
world->SetContinuousPhysics(true);
|
|
|
|
|
|
|
|
// Define the ground body.
|
|
|
|
b2BodyDef groundBodyDef;
|
2021-12-31 12:12:40 +08:00
|
|
|
groundBodyDef.position.Set(0, 0); // bottom-left corner
|
2020-11-20 11:51:47 +08:00
|
|
|
|
|
|
|
// Call the body factory which allocates memory for the ground body
|
|
|
|
// from a pool and creates the ground box shape (also from a pool).
|
|
|
|
// The body is also added to the world.
|
|
|
|
b2Body* groundBody = world->CreateBody(&groundBodyDef);
|
|
|
|
|
|
|
|
// Define the ground box shape.
|
|
|
|
b2EdgeShape groundBox;
|
|
|
|
|
|
|
|
// bottom
|
2021-12-31 12:12:40 +08:00
|
|
|
groundBox.SetTwoSided(b2Vec2(VisibleRect::leftBottom().x / PTM_RATIO, VisibleRect::leftBottom().y / PTM_RATIO),
|
|
|
|
b2Vec2(VisibleRect::rightBottom().x / PTM_RATIO, VisibleRect::rightBottom().y / PTM_RATIO));
|
2020-11-23 14:46:09 +08:00
|
|
|
groundBody->CreateFixture(&groundBox, 0);
|
2020-11-20 11:51:47 +08:00
|
|
|
|
|
|
|
// top
|
2021-12-31 12:12:40 +08:00
|
|
|
groundBox.SetTwoSided(b2Vec2(VisibleRect::leftTop().x / PTM_RATIO, VisibleRect::leftTop().y / PTM_RATIO),
|
|
|
|
b2Vec2(VisibleRect::rightTop().x / PTM_RATIO, VisibleRect::rightTop().y / PTM_RATIO));
|
2020-11-23 14:46:09 +08:00
|
|
|
groundBody->CreateFixture(&groundBox, 0);
|
2020-11-20 11:51:47 +08:00
|
|
|
|
|
|
|
// left
|
2021-12-31 12:12:40 +08:00
|
|
|
groundBox.SetTwoSided(b2Vec2(VisibleRect::leftTop().x / PTM_RATIO, VisibleRect::leftTop().y / PTM_RATIO),
|
|
|
|
b2Vec2(VisibleRect::leftBottom().x / PTM_RATIO, VisibleRect::leftBottom().y / PTM_RATIO));
|
2020-11-23 14:46:09 +08:00
|
|
|
groundBody->CreateFixture(&groundBox, 0);
|
2020-11-20 11:51:47 +08:00
|
|
|
|
|
|
|
// right
|
2021-12-31 12:12:40 +08:00
|
|
|
groundBox.SetTwoSided(b2Vec2(VisibleRect::rightBottom().x / PTM_RATIO, VisibleRect::rightBottom().y / PTM_RATIO),
|
|
|
|
b2Vec2(VisibleRect::rightTop().x / PTM_RATIO, VisibleRect::rightTop().y / PTM_RATIO));
|
2020-11-23 14:46:09 +08:00
|
|
|
groundBody->CreateFixture(&groundBox, 0);
|
|
|
|
|
2021-08-06 19:40:32 +08:00
|
|
|
// Small triangle
|
|
|
|
b2Vec2 vertices[3];
|
|
|
|
vertices[0].Set(-1.0f, 0.0f);
|
|
|
|
vertices[1].Set(1.0f, 0.0f);
|
|
|
|
vertices[2].Set(0.0f, 2.0f);
|
|
|
|
|
|
|
|
b2PolygonShape polygon;
|
|
|
|
polygon.Set(vertices, 3);
|
|
|
|
|
|
|
|
b2FixtureDef triangleShapeDef;
|
2021-12-31 12:12:40 +08:00
|
|
|
triangleShapeDef.shape = &polygon;
|
2021-08-06 19:40:32 +08:00
|
|
|
triangleShapeDef.density = 1.0f;
|
|
|
|
|
|
|
|
b2BodyDef triangleBodyDef;
|
|
|
|
triangleBodyDef.type = b2_dynamicBody;
|
|
|
|
triangleBodyDef.position.Set(rand() % 13 + 3, 4);
|
|
|
|
|
|
|
|
b2Body* body1 = world->CreateBody(&triangleBodyDef);
|
|
|
|
body1->CreateFixture(&triangleShapeDef);
|
|
|
|
|
|
|
|
// Large triangle (recycle definitions)
|
|
|
|
vertices[0] *= 2.0f;
|
|
|
|
vertices[1] *= 2.0f;
|
|
|
|
vertices[2] *= 2.0f;
|
|
|
|
polygon.Set(vertices, 3);
|
|
|
|
|
|
|
|
triangleBodyDef.position.Set(rand() % 13 + 3, 4);
|
|
|
|
|
|
|
|
b2Body* body2 = world->CreateBody(&triangleBodyDef);
|
|
|
|
body2->CreateFixture(&triangleShapeDef);
|
|
|
|
|
|
|
|
// Small box
|
|
|
|
polygon.SetAsBox(1.0f, 0.5f);
|
|
|
|
|
|
|
|
b2FixtureDef boxShapeDef;
|
2021-12-31 12:12:40 +08:00
|
|
|
boxShapeDef.shape = &polygon;
|
2021-08-06 19:40:32 +08:00
|
|
|
boxShapeDef.density = 1.0f;
|
|
|
|
|
|
|
|
b2BodyDef boxBodyDef;
|
|
|
|
boxBodyDef.type = b2_dynamicBody;
|
|
|
|
boxBodyDef.position.Set(rand() % 13 + 3, 4);
|
|
|
|
|
|
|
|
b2Body* body3 = world->CreateBody(&boxBodyDef);
|
|
|
|
body3->CreateFixture(&boxShapeDef);
|
|
|
|
|
|
|
|
// Large box (recycle definitions)
|
|
|
|
polygon.SetAsBox(2.0f, 1.0f);
|
|
|
|
boxBodyDef.position.Set(rand() % 13 + 3, 4);
|
|
|
|
|
|
|
|
b2Body* body4 = world->CreateBody(&boxBodyDef);
|
|
|
|
body4->CreateFixture(&boxShapeDef);
|
|
|
|
|
|
|
|
// Small circle
|
|
|
|
b2CircleShape circle;
|
|
|
|
circle.m_radius = 1.0f;
|
|
|
|
|
|
|
|
b2FixtureDef circleShapeDef;
|
2021-12-31 12:12:40 +08:00
|
|
|
circleShapeDef.shape = &circle;
|
2021-08-06 19:40:32 +08:00
|
|
|
circleShapeDef.density = 1.0f;
|
|
|
|
|
|
|
|
b2BodyDef circleBodyDef;
|
|
|
|
circleBodyDef.type = b2_dynamicBody;
|
|
|
|
circleBodyDef.position.Set(rand() % 13 + 3, 4);
|
|
|
|
|
|
|
|
b2Body* body5 = world->CreateBody(&circleBodyDef);
|
|
|
|
body5->CreateFixture(&circleShapeDef);
|
|
|
|
|
|
|
|
// Large circle
|
|
|
|
circle.m_radius *= 2.0f;
|
|
|
|
circleBodyDef.position.Set(rand() % 13 + 3, 4);
|
|
|
|
|
|
|
|
b2Body* body6 = world->CreateBody(&circleBodyDef);
|
|
|
|
body6->CreateFixture(&circleShapeDef);
|
|
|
|
|
|
|
|
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);
|
2021-12-31 12:12:40 +08:00
|
|
|
g_debugDraw.mRatio = PTM_RATIO;
|
|
|
|
g_debugDraw.debugNodeOffset = {0, 0};
|
2021-08-05 17:23:55 +08:00
|
|
|
world->SetDebugDraw(&g_debugDraw);
|
2020-11-20 11:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DTest::createResetButton()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto reset = MenuItemImage::create("Images/r1.png", "Images/r2.png",
|
|
|
|
[&](Ref* sender) { getTestSuite()->restartCurrTest(); });
|
2020-11-20 11:51:47 +08:00
|
|
|
|
|
|
|
auto menu = Menu::create(reset, nullptr);
|
|
|
|
|
|
|
|
menu->setPosition(VisibleRect::bottom().x, VisibleRect::bottom().y + 30);
|
|
|
|
this->addChild(menu, -1);
|
2020-11-23 14:46:09 +08:00
|
|
|
}
|
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
void Box2DTest::addNewSpriteAtPosition(Vec2 p)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Add sprite %0.2f x %02.f", p.x, p.y);
|
2020-11-23 14:46:09 +08:00
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
// Define the dynamic body.
|
2021-12-31 12:12:40 +08:00
|
|
|
// Set up a 1m squared box in the physics world
|
2020-11-20 11:51:47 +08:00
|
|
|
b2BodyDef bodyDef;
|
|
|
|
bodyDef.type = b2_dynamicBody;
|
2020-11-23 14:46:09 +08:00
|
|
|
bodyDef.position.Set(p.x / PTM_RATIO, p.y / PTM_RATIO);
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Add PTM_RATIO sprite %0.2f x %0.2f", p.x / PTM_RATIO, p.y / PTM_RATIO);
|
2021-08-12 15:34:24 +08:00
|
|
|
|
2020-11-23 14:46:09 +08:00
|
|
|
b2Body* body = world->CreateBody(&bodyDef);
|
2020-11-20 11:51:47 +08:00
|
|
|
|
|
|
|
// Define another box shape for our dynamic body.
|
|
|
|
b2PolygonShape dynamicBox;
|
2021-12-31 12:12:40 +08:00
|
|
|
dynamicBox.SetAsBox(.5f, .5f); // These are mid points for our 1m box
|
2020-11-23 14:46:09 +08:00
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
// Define the dynamic body fixture.
|
|
|
|
b2FixtureDef fixtureDef;
|
2021-12-31 12:12:40 +08:00
|
|
|
fixtureDef.shape = &dynamicBox;
|
|
|
|
fixtureDef.density = 1.0f;
|
2020-11-20 11:51:47 +08:00
|
|
|
fixtureDef.friction = 0.3f;
|
2020-11-23 14:46:09 +08:00
|
|
|
body->CreateFixture(&fixtureDef);
|
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
auto parent = this->getChildByTag(kTagParentNode);
|
2020-11-23 14:46:09 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
|
|
|
|
// just randomly picking one of the images
|
2022-07-16 10:43:05 +08:00
|
|
|
int idx = (AXRANDOM_0_1() > .5 ? 0 : 1);
|
|
|
|
int idy = (AXRANDOM_0_1() > .5 ? 0 : 1);
|
2021-04-22 22:01:47 +08:00
|
|
|
auto sprite = PhysicsSpriteBox2D::createWithTexture(_spriteTexture, Rect(32 * idx, 32 * idy, 32, 32));
|
2020-11-20 11:51:47 +08:00
|
|
|
parent->addChild(sprite);
|
|
|
|
sprite->setB2Body(body);
|
|
|
|
sprite->setPTMRatio(PTM_RATIO);
|
2022-10-18 19:17:36 +08:00
|
|
|
sprite->setPosition(ax::Vec2(p.x, p.y));
|
2020-11-20 11:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DTest::update(float dt)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// It is recommended that a fixed time step is used with Box2D for stability
|
|
|
|
// of the simulation, however, we are using a variable time step here.
|
|
|
|
// You need to make an informed choice, the following URL is useful
|
|
|
|
// http://gafferongames.com/game-physics/fix-your-timestep/
|
2020-11-23 14:46:09 +08:00
|
|
|
|
2020-11-20 11:51:47 +08:00
|
|
|
int velocityIterations = 8;
|
|
|
|
int positionIterations = 1;
|
|
|
|
|
|
|
|
// Instruct the world to perform a single step of simulation. It is
|
|
|
|
// generally best to keep the time step and iterations fixed.
|
|
|
|
world->Step(dt, velocityIterations, positionIterations);
|
2021-08-06 19:40:32 +08:00
|
|
|
|
|
|
|
// Debug draw
|
|
|
|
if (showDebugDraw)
|
|
|
|
{
|
|
|
|
drawBox2D->clear();
|
|
|
|
world->DebugDraw();
|
|
|
|
}
|
2020-11-20 11:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// Add a new body/atlas sprite at the touched location
|
2020-11-20 11:51:47 +08:00
|
|
|
|
|
|
|
for (auto& touch : touches)
|
|
|
|
{
|
2020-11-23 14:46:09 +08:00
|
|
|
if (!touch)
|
2020-11-20 11:51:47 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
auto location = touch->getLocation();
|
2020-11-23 14:46:09 +08:00
|
|
|
|
|
|
|
addNewSpriteAtPosition(location);
|
2020-11-20 11:51:47 +08:00
|
|
|
}
|
2021-04-22 22:01:47 +08:00
|
|
|
}
|