2012-03-21 15:43:35 +08:00
|
|
|
//
|
|
|
|
// Accelerometer + Chipmunk physics + multi touches example
|
|
|
|
// a cocos2d example
|
|
|
|
// http://www.cocos2d-x.org
|
|
|
|
//
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
#include "ChipmunkTest.h"
|
|
|
|
|
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
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
ChipmunkTestLayer::ChipmunkTestLayer()
|
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();
|
|
|
|
touchListener->onTouchesEnded = CC_CALLBACK_2(ChipmunkTestLayer::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);
|
2013-10-23 16:14:03 +08:00
|
|
|
auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(ChipmunkTestLayer::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-03-26 01:37:35 +08:00
|
|
|
label->setPosition(cocos2d::Point( 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-03-26 01:37:35 +08:00
|
|
|
addNewSpriteAtPosition(cocos2d::Point(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);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(ChipmunkTestLayer::toggleDebugCallback, this));
|
2013-01-06 10:13:59 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto menu = Menu::create(item, NULL);
|
2013-01-06 10:13:59 +08:00
|
|
|
this->addChild(menu);
|
2014-03-26 01:37:35 +08:00
|
|
|
menu->setPosition(cocos2d::Point(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();
|
2013-07-24 06:20:22 +08:00
|
|
|
label->setPosition(Point(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
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ChipmunkTestLayer::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
|
|
|
}
|
|
|
|
|
|
|
|
ChipmunkTestLayer::~ChipmunkTestLayer()
|
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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
cpSpaceFree( _space );
|
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
|
|
|
}
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
void ChipmunkTestLayer::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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_space = cpSpaceNew();
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_space->gravity = 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
|
2013-06-15 14:03:30 +08:00
|
|
|
_walls[0] = cpSegmentShapeNew( _space->staticBody,
|
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
|
2013-06-15 14:03:30 +08:00
|
|
|
_walls[1] = cpSegmentShapeNew( _space->staticBody,
|
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
|
2013-06-15 14:03:30 +08:00
|
|
|
_walls[2] = cpSegmentShapeNew( _space->staticBody,
|
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
|
2013-06-15 14:03:30 +08:00
|
|
|
_walls[3] = cpSegmentShapeNew( _space->staticBody,
|
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++) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_walls[i]->e = 1.0f;
|
|
|
|
_walls[i]->u = 1.0f;
|
|
|
|
cpSpaceAddStaticShape(_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
|
|
|
}
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
void ChipmunkTestLayer::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++){
|
2013-06-15 14:03:30 +08:00
|
|
|
cpSpaceStep(_space, dt);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
void ChipmunkTestLayer::createResetButton()
|
2012-03-21 15:43:35 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto reset = MenuItemImage::create("Images/r1.png", "Images/r2.png", CC_CALLBACK_1(ChipmunkTestLayer::reset, this));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto menu = Menu::create(reset, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-03-26 01:37:35 +08:00
|
|
|
menu->setPosition(cocos2d::Point(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
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ChipmunkTestLayer::reset(Ref* sender)
|
2012-03-21 15:43:35 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = new ChipmunkAccelTouchTestScene();
|
|
|
|
auto child = new ChipmunkTestLayer();
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild(child);
|
|
|
|
child->release();
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2012-04-19 14:35:52 +08:00
|
|
|
s->release();
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2014-03-26 01:37:35 +08:00
|
|
|
void ChipmunkTestLayer::addNewSpriteAtPosition(cocos2d::Point 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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero));
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
body->p = cpv(pos.x, pos.y);
|
2013-06-15 14:03:30 +08:00
|
|
|
cpSpaceAddBody(_space, body);
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero);
|
|
|
|
shape->e = 0.5f; shape->u = 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
|
|
|
}
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
void ChipmunkTestLayer::onEnter()
|
2012-03-21 15:43:35 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer::onEnter();
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void ChipmunkTestLayer::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
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void ChipmunkTestLayer::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-03-26 01:37:35 +08:00
|
|
|
auto v = cocos2d::Point( accelX, accelY);
|
2013-07-11 16:38:58 +08:00
|
|
|
v = v * 200;
|
2013-06-15 14:03:30 +08:00
|
|
|
_space->gravity = cpv(v.x, v.y);
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkAccelTouchTestScene::runThisTest()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto layer = new ChipmunkTestLayer();
|
2013-07-23 08:25:44 +08:00
|
|
|
addChild(layer);
|
|
|
|
layer->release();
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|