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