2019-11-23 20:27:39 +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
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-11-23 20:27:39 +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:
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-11-23 20:27:39 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// Accelerometer + Chipmunk physics + multi touches example
|
|
|
|
// a cocos2d example
|
|
|
|
// http://www.cocos2d-x.org
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "chipmunk/chipmunk.h"
|
|
|
|
|
|
|
|
#include "ChipmunkTest.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
enum
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
kTagParentNode = 1,
|
|
|
|
};
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
enum
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Z_PHYSICS_DEBUG = 100,
|
|
|
|
};
|
|
|
|
|
|
|
|
// callback to remove Shapes from the Space
|
|
|
|
|
|
|
|
ChipmunkTest::ChipmunkTest()
|
2021-12-28 16:06:23 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// enable events
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto touchListener = EventListenerTouchAllAtOnce::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
touchListener->onTouchesEnded = CC_CALLBACK_2(ChipmunkTest::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Device::setAccelerometerEnabled(true);
|
|
|
|
auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(ChipmunkTest::onAcceleration, this));
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(accListener, this);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// title
|
|
|
|
auto label = Label::createWithTTF("Multi touch the screen", "fonts/Marker Felt.ttf", 36.0f);
|
|
|
|
label->setPosition(VisibleRect::center().x, VisibleRect::top().y - 30);
|
|
|
|
this->addChild(label, -1);
|
|
|
|
|
|
|
|
// reset button
|
|
|
|
createResetButton();
|
|
|
|
|
|
|
|
// init physics
|
2021-06-18 15:47:48 +08:00
|
|
|
|
|
|
|
physicsDebugNodeOffset = Vec2(0, 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
initPhysics();
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
// Use batch node. Faster
|
2021-12-28 16:06:23 +08:00
|
|
|
auto parent = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100);
|
2019-11-23 20:27:39 +08:00
|
|
|
_spriteTexture = parent->getTexture();
|
|
|
|
#else
|
|
|
|
// doesn't use batch node. Slower
|
|
|
|
_spriteTexture = Director::getInstance()->getTextureCache()->addImage("Images/grossini_dance_atlas.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
auto parent = Node::create();
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
addChild(parent, 0, kTagParentNode);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
addNewSpriteAtPosition(cocos2d::Vec2(200.0f, 200.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// menu for debug layer
|
|
|
|
MenuItemFont::setFontSize(18);
|
|
|
|
auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(ChipmunkTest::toggleDebugCallback, this));
|
|
|
|
|
|
|
|
auto menu = Menu::create(item, nullptr);
|
|
|
|
this->addChild(menu);
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(VisibleRect::right().x - 100, VisibleRect::top().y - 60);
|
2021-08-05 17:23:55 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkTest::toggleDebugCallback(Ref* sender)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_debugLayer->setVisible(!_debugLayer->isVisible());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ChipmunkTest::~ChipmunkTest()
|
|
|
|
{
|
|
|
|
// manually Free rogue shapes
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
cpShapeFree(_walls[i]);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|
2021-12-28 16:06:23 +08:00
|
|
|
cpSpaceFree(_space);
|
2019-11-23 20:27:39 +08:00
|
|
|
#else
|
2021-12-28 16:06:23 +08:00
|
|
|
cpHastySpaceFree(_space);
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Device::setAccelerometerEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkTest::initPhysics()
|
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// init chipmunk
|
2021-12-28 16:06:23 +08:00
|
|
|
// cpInitChipmunk();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|
2021-12-28 16:06:23 +08:00
|
|
|
_space = cpSpaceNew();
|
2019-11-23 20:27:39 +08:00
|
|
|
#else
|
2021-12-28 16:06:23 +08:00
|
|
|
_space = cpHastySpaceNew();
|
|
|
|
cpHastySpaceSetThreads(_space, 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
cpSpaceSetGravity(_space, cpv(0.0f, -100.0f));
|
|
|
|
|
|
|
|
//
|
|
|
|
// rogue shapes
|
|
|
|
// We have to free them manually
|
|
|
|
//
|
|
|
|
// bottom
|
2021-12-28 16:06:23 +08:00
|
|
|
_walls[0] =
|
|
|
|
cpSegmentShapeNew(cpSpaceGetStaticBody(_space), cpv(VisibleRect::leftBottom().x, VisibleRect::leftBottom().y),
|
|
|
|
cpv(VisibleRect::rightBottom().x, VisibleRect::rightBottom().y), 0.0f);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// top
|
2021-12-28 16:06:23 +08:00
|
|
|
_walls[1] = cpSegmentShapeNew(cpSpaceGetStaticBody(_space), cpv(VisibleRect::leftTop().x, VisibleRect::leftTop().y),
|
|
|
|
cpv(VisibleRect::rightTop().x, VisibleRect::rightTop().y), 0.0f);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// left
|
2021-12-28 16:06:23 +08:00
|
|
|
_walls[2] =
|
|
|
|
cpSegmentShapeNew(cpSpaceGetStaticBody(_space), cpv(VisibleRect::leftBottom().x, VisibleRect::leftBottom().y),
|
|
|
|
cpv(VisibleRect::leftTop().x, VisibleRect::leftTop().y), 0.0f);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// right
|
2021-12-28 16:06:23 +08:00
|
|
|
_walls[3] =
|
|
|
|
cpSegmentShapeNew(cpSpaceGetStaticBody(_space), cpv(VisibleRect::rightBottom().x, VisibleRect::rightBottom().y),
|
|
|
|
cpv(VisibleRect::rightTop().x, VisibleRect::rightTop().y), 0.0f);
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
cpShapeSetElasticity(_walls[i], 1.0f);
|
|
|
|
cpShapeSetFriction(_walls[i], 1.0f);
|
|
|
|
cpSpaceAddShape(_space, _walls[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Physics debug layer
|
2021-08-05 17:23:55 +08:00
|
|
|
_debugLayer = PhysicsDebugNodeChipmunk2D::create(_space);
|
2019-11-23 20:27:39 +08:00
|
|
|
this->addChild(_debugLayer, Z_PHYSICS_DEBUG);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkTest::update(float delta)
|
|
|
|
{
|
|
|
|
// Should use a fixed size step based on the animation interval.
|
|
|
|
int steps = 2;
|
2021-12-28 16:06:23 +08:00
|
|
|
float dt = Director::getInstance()->getAnimationInterval() / (float)steps;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < steps; i++)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|
2021-12-28 16:06:23 +08:00
|
|
|
cpSpaceStep(_space, dt);
|
2019-11-23 20:27:39 +08:00
|
|
|
#else
|
2021-12-28 16:06:23 +08:00
|
|
|
cpHastySpaceStep(_space, dt);
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkTest::createResetButton()
|
|
|
|
{
|
|
|
|
auto reset = MenuItemImage::create("Images/r1.png", "Images/r2.png", CC_CALLBACK_1(ChipmunkTest::reset, this));
|
|
|
|
|
|
|
|
auto menu = Menu::create(reset, nullptr);
|
|
|
|
|
|
|
|
menu->setPosition(VisibleRect::center().x, VisibleRect::bottom().y + 30);
|
|
|
|
this->addChild(menu, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkTest::reset(Ref* sender)
|
|
|
|
{
|
|
|
|
getTestSuite()->restartCurrTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkTest::addNewSpriteAtPosition(cocos2d::Vec2 pos)
|
|
|
|
{
|
|
|
|
int posx, posy;
|
|
|
|
|
|
|
|
auto parent = getChildByTag(kTagParentNode);
|
|
|
|
|
|
|
|
posx = CCRANDOM_0_1() * 200.0f;
|
|
|
|
posy = CCRANDOM_0_1() * 200.0f;
|
|
|
|
|
|
|
|
posx = (posx % 4) * 85;
|
|
|
|
posy = (posy % 3) * 121;
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
int num = 4;
|
2019-11-23 20:27:39 +08:00
|
|
|
cpVect verts[] = {
|
2021-12-28 16:06:23 +08:00
|
|
|
cpv(-24, -54),
|
2019-11-23 20:27:39 +08:00
|
|
|
cpv(-24, 54),
|
2021-12-28 16:06:23 +08:00
|
|
|
cpv(24, 54),
|
|
|
|
cpv(24, -54),
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
cpBody* body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero, 0.0f));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
cpBodySetPosition(body, cpv(pos.x, pos.y));
|
|
|
|
cpSpaceAddBody(_space, body);
|
|
|
|
|
|
|
|
cpShape* shape = cpPolyShapeNew(body, num, verts, cpTransformIdentity, 0.0f);
|
|
|
|
cpShapeSetElasticity(shape, 0.5f);
|
|
|
|
cpShapeSetFriction(shape, 0.5f);
|
|
|
|
cpSpaceAddShape(_space, shape);
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
auto sprite = PhysicsSpriteChipmunk2D::createWithTexture(_spriteTexture, cocos2d::Rect(posx, posy, 85, 121));
|
2019-11-23 20:27:39 +08:00
|
|
|
parent->addChild(sprite);
|
|
|
|
|
|
|
|
sprite->setCPBody(body);
|
|
|
|
sprite->setPosition(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkTest::onEnter()
|
|
|
|
{
|
|
|
|
TestCase::onEnter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
// Add a new body/atlas sprite at the touched location
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
for (auto& touch : touches)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
addNewSpriteAtPosition(location);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkTest::onAcceleration(Acceleration* acc, Event* event)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
static float prevX = 0, prevY = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#define kFilterFactor 0.05f
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
float accelX = (float)acc->x * kFilterFactor + (1 - kFilterFactor) * prevX;
|
|
|
|
float accelY = (float)acc->y * kFilterFactor + (1 - kFilterFactor) * prevY;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
prevX = accelX;
|
|
|
|
prevY = accelY;
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto v = cocos2d::Vec2(accelX, accelY);
|
|
|
|
v = v * 200;
|
2019-11-23 20:27:39 +08:00
|
|
|
cpSpaceSetGravity(_space, cpv(v.x, v.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
ChipmunkTests::ChipmunkTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(ChipmunkTest);
|
|
|
|
}
|