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
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *label = LabelTTF::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
|
2013-06-20 14:17:10 +08:00
|
|
|
SpriteBatchNode *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-06-20 14:17:10 +08:00
|
|
|
_spriteTexture = TextureCache::sharedTextureCache()->addImage("Images/grossini_dance_atlas.png");
|
|
|
|
Node *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
|
|
|
|
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
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont::setFontSize(18);
|
|
|
|
MenuItemFont *item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(ChipmunkTestLayer::toggleDebugCallback, this));
|
2013-01-06 10:13:59 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Menu *menu = Menu::create(item, NULL);
|
2013-01-06 10:13:59 +08:00
|
|
|
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
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF *pLabel = LabelTTF::create("Should define CC_ENABLE_CHIPMUNK_INTEGRATION=1\n to run this test case",
|
2013-01-06 10:22:07 +08:00
|
|
|
"Arial",
|
|
|
|
18);
|
2013-06-20 14:17:10 +08:00
|
|
|
Size size = Director::sharedDirector()->getWinSize();
|
2013-01-06 10:22:07 +08:00
|
|
|
pLabel->setPosition(ccp(size.width/2, size.height/2));
|
|
|
|
|
|
|
|
addChild(pLabel);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void ChipmunkTestLayer::toggleDebugCallback(Object* pSender)
|
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
|
|
|
{
|
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-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-06-20 14:17:10 +08:00
|
|
|
float dt = Director::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-20 14:17:10 +08:00
|
|
|
MenuItemImage *reset = MenuItemImage::create("Images/r1.png", "Images/r2.png", CC_CALLBACK_1(ChipmunkTestLayer::reset, this));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Menu *menu = Menu::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
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void ChipmunkTestLayer::reset(Object* sender)
|
2012-03-21 15:43:35 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene* 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();
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene(s);
|
2012-04-19 14:35:52 +08:00
|
|
|
s->release();
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void ChipmunkTestLayer::addNewSpriteAtPosition(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-06-20 14:17:10 +08:00
|
|
|
Node *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-20 14:17:10 +08:00
|
|
|
PhysicsSprite *sprite = PhysicsSprite::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
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer::onEnter();
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void ChipmunkTestLayer::ccTouchesEnded(Set* 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-06-22 11:02:43 +08:00
|
|
|
for( auto &item: *touches)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-22 11:02:43 +08:00
|
|
|
Touch* touch = static_cast<Touch*>(item);
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Point 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-06-20 14:17:10 +08:00
|
|
|
void ChipmunkTestLayer::didAccelerate(Acceleration* 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
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Point v = ccp( accelX, accelY);
|
2012-04-19 14:35:52 +08:00
|
|
|
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()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* pLayer = new ChipmunkTestLayer();
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(pLayer);
|
|
|
|
pLayer->release();
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene(this);
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|