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-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
|
|
|
setAccelerometerEnabled(true);
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// title
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF *label = CCLabelTTF::create("Multi touch the screen", "Marker Felt", 36);
|
2012-11-20 16:19:05 +08:00
|
|
|
label->setPosition(ccp( 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
|
2012-06-14 15:13:16 +08:00
|
|
|
CCSpriteBatchNode *parent = CCSpriteBatchNode::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-06-15 14:03:30 +08:00
|
|
|
_spriteTexture = CCTextureCache::sharedTextureCache()->addImage("Images/grossini_dance_atlas.png");
|
2012-06-14 15:13:16 +08:00
|
|
|
CCNode *parent = CCNode::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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
addNewSpriteAtPosition(ccp(200,200));
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2013-01-06 10:13:59 +08:00
|
|
|
// menu for debug layer
|
|
|
|
CCMenuItemFont::setFontSize(18);
|
2013-06-14 11:36:43 +08:00
|
|
|
CCMenuItemFont *item = CCMenuItemFont::create("Toggle debug", CC_CALLBACK_1(ChipmunkTestLayer::toggleDebugCallback, this));
|
2013-01-06 10:13:59 +08:00
|
|
|
|
|
|
|
CCMenu *menu = CCMenu::create(item, NULL);
|
|
|
|
this->addChild(menu);
|
2012-11-20 16:19:05 +08:00
|
|
|
menu->setPosition(ccp(VisibleRect::right().x-100, VisibleRect::top().y-60));
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
scheduleUpdate();
|
2013-01-06 10:22:07 +08:00
|
|
|
#else
|
|
|
|
CCLabelTTF *pLabel = CCLabelTTF::create("Should define CC_ENABLE_CHIPMUNK_INTEGRATION=1\n to run this test case",
|
|
|
|
"Arial",
|
|
|
|
18);
|
|
|
|
CCSize size = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
pLabel->setPosition(ccp(size.width/2, size.height/2));
|
|
|
|
|
|
|
|
addChild(pLabel);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
void ChipmunkTestLayer::toggleDebugCallback(CCObject* pSender)
|
|
|
|
{
|
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
|
|
|
{
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
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-15 14:03:30 +08:00
|
|
|
_debugLayer = CCPhysicsDebugNode::create(_space);
|
|
|
|
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;
|
2012-08-01 15:30:12 +08:00
|
|
|
float dt = CCDirector::sharedDirector()->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-06-14 11:36:43 +08:00
|
|
|
CCMenuItemImage *reset = CCMenuItemImage::create("Images/r1.png", "Images/r2.png", CC_CALLBACK_1(ChipmunkTestLayer::reset, this));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenu *menu = CCMenu::create(reset, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-10-23 17:48:50 +08:00
|
|
|
menu->setPosition(ccp(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
|
|
|
}
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
void ChipmunkTestLayer::reset(CCObject* sender)
|
2012-03-21 15:43:35 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScene* s = new ChipmunkAccelTouchTestScene();
|
2012-11-20 16:19:05 +08:00
|
|
|
ChipmunkTestLayer* child = new ChipmunkTestLayer();
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild(child);
|
|
|
|
child->release();
|
|
|
|
CCDirector::sharedDirector()->replaceScene(s);
|
|
|
|
s->release();
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
void ChipmunkTestLayer::addNewSpriteAtPosition(CCPoint 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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCNode *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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CCPhysicsSprite *sprite = CCPhysicsSprite::createWithTexture(_spriteTexture, CCRectMake(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
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayer::onEnter();
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
void ChipmunkTestLayer::ccTouchesEnded(CCSet* touches, CCEvent* 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
|
|
|
|
CCSetIterator it;
|
|
|
|
CCTouch* touch;
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
for( it = touches->begin(); it != touches->end(); it++)
|
|
|
|
{
|
|
|
|
touch = (CCTouch*)(*it);
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if(!touch)
|
|
|
|
break;
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-07-31 17:41:53 +08:00
|
|
|
CCPoint 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
|
|
|
}
|
|
|
|
|
2012-11-20 16:19:05 +08:00
|
|
|
void ChipmunkTestLayer::didAccelerate(CCAcceleration* pAccelerationValue)
|
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
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
float accelX = (float) pAccelerationValue->x * kFilterFactor + (1- kFilterFactor)*prevX;
|
|
|
|
float accelY = (float) pAccelerationValue->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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint v = ccp( accelX, accelY);
|
|
|
|
v = ccpMult(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()
|
|
|
|
{
|
2012-11-20 16:19:05 +08:00
|
|
|
CCLayer* pLayer = new ChipmunkTestLayer();
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(pLayer);
|
|
|
|
pLayer->release();
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector::sharedDirector()->replaceScene(this);
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|