2012-03-21 15:43:35 +08:00
|
|
|
//
|
|
|
|
// Accelerometer + Chipmunk physics + multi touches example
|
|
|
|
// a cocos2d example
|
|
|
|
// http://www.cocos2d-x.org
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "ChipmunkAccelTouchTest.h"
|
|
|
|
|
|
|
|
enum {
|
2012-04-19 14:35:52 +08:00
|
|
|
kTagParentNode = 1,
|
2012-03-21 15:43:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// callback to remove Shapes from the Space
|
|
|
|
void removeShape( cpBody *body, cpShape *shape, void *data )
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
cpShapeFree( shape );
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ChipmunkPhysicsSprite::ChipmunkPhysicsSprite()
|
2012-05-02 14:06:40 +08:00
|
|
|
: m_pBody(NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ChipmunkPhysicsSprite::~ChipmunkPhysicsSprite()
|
|
|
|
{
|
2012-05-02 14:06:40 +08:00
|
|
|
cpBodyEachShape(m_pBody, removeShape, NULL);
|
|
|
|
cpBodyFree( m_pBody );
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkPhysicsSprite::setPhysicsBody(cpBody * body)
|
|
|
|
{
|
2012-05-02 14:06:40 +08:00
|
|
|
m_pBody = body;
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// this method will only get called if the sprite is batched.
|
|
|
|
// return YES if the physics values (angles, position ) changed
|
|
|
|
// If you return NO, then nodeToParentTransform won't be called.
|
2012-04-19 14:35:52 +08:00
|
|
|
bool ChipmunkPhysicsSprite::isDirty(void)
|
|
|
|
{
|
|
|
|
return true;
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAffineTransform ChipmunkPhysicsSprite::nodeToParentTransform(void)
|
|
|
|
{
|
2012-08-01 15:30:12 +08:00
|
|
|
float x = m_pBody->p.x;
|
|
|
|
float y = m_pBody->p.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
if ( isIgnoreAnchorPointForPosition() ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
x += m_tAnchorPointInPoints.x;
|
|
|
|
y += m_tAnchorPointInPoints.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make matrix
|
2012-08-01 15:30:12 +08:00
|
|
|
float c = m_pBody->rot.x;
|
|
|
|
float s = m_pBody->rot.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
if( ! m_tAnchorPointInPoints.equals(CCPointZero) ){
|
2012-04-19 14:35:52 +08:00
|
|
|
x += c*-m_tAnchorPointInPoints.x + -s*-m_tAnchorPointInPoints.y;
|
|
|
|
y += s*-m_tAnchorPointInPoints.x + c*-m_tAnchorPointInPoints.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rot, Translate Matrix
|
|
|
|
m_tTransform = CCAffineTransformMake( c, s,
|
|
|
|
-s, c,
|
|
|
|
x, y );
|
|
|
|
|
|
|
|
return m_tTransform;
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ChipmunkAccelTouchTestLayer::ChipmunkAccelTouchTestLayer()
|
|
|
|
{
|
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-10-23 17:48:50 +08:00
|
|
|
label->setPosition(ccp( VisibleRect::center().x, VisibleRect::bottom().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);
|
2012-05-02 14:06:40 +08:00
|
|
|
m_pSpriteTexture = 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
|
2012-05-02 14:06:40 +08:00
|
|
|
m_pSpriteTexture = 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
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
scheduleUpdate();
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ChipmunkAccelTouchTestLayer::~ChipmunkAccelTouchTestLayer()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// manually Free rogue shapes
|
|
|
|
for( int i=0;i<4;i++) {
|
2012-05-02 14:06:40 +08:00
|
|
|
cpShapeFree( m_pWalls[i] );
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-05-02 14:06:40 +08:00
|
|
|
cpSpaceFree( m_pSpace );
|
2012-03-21 15:43:35 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkAccelTouchTestLayer::initPhysics()
|
|
|
|
{
|
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
|
|
|
|
2012-05-02 14:06:40 +08:00
|
|
|
m_pSpace = cpSpaceNew();
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-05-02 14:06:40 +08:00
|
|
|
m_pSpace->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
|
2012-10-23 17:48:50 +08:00
|
|
|
m_pWalls[0] = cpSegmentShapeNew( m_pSpace->staticBody,
|
|
|
|
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
|
2012-10-23 17:48:50 +08:00
|
|
|
m_pWalls[1] = cpSegmentShapeNew( m_pSpace->staticBody,
|
|
|
|
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
|
2012-10-23 17:48:50 +08:00
|
|
|
m_pWalls[2] = cpSegmentShapeNew( m_pSpace->staticBody,
|
|
|
|
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
|
2012-10-23 17:48:50 +08:00
|
|
|
m_pWalls[3] = cpSegmentShapeNew( m_pSpace->staticBody,
|
|
|
|
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++) {
|
2012-05-02 14:06:40 +08:00
|
|
|
m_pWalls[i]->e = 1.0f;
|
|
|
|
m_pWalls[i]->u = 1.0f;
|
|
|
|
cpSpaceAddStaticShape(m_pSpace, m_pWalls[i] );
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void ChipmunkAccelTouchTestLayer::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++){
|
2012-05-02 14:06:40 +08:00
|
|
|
cpSpaceStep(m_pSpace, dt);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkAccelTouchTestLayer::createResetButton()
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenuItemImage *reset = CCMenuItemImage::create("Images/r1.png", "Images/r2.png", this, menu_selector(ChipmunkAccelTouchTestLayer::reset));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkAccelTouchTestLayer::reset(CCObject* sender)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScene* s = new ChipmunkAccelTouchTestScene();
|
|
|
|
ChipmunkAccelTouchTestLayer* child = new ChipmunkAccelTouchTestLayer();
|
|
|
|
s->addChild(child);
|
|
|
|
child->release();
|
|
|
|
CCDirector::sharedDirector()->replaceScene(s);
|
|
|
|
s->release();
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkAccelTouchTestLayer::addNewSpriteAtPosition(CCPoint pos)
|
|
|
|
{
|
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
|
|
|
ChipmunkPhysicsSprite *sprite = new ChipmunkPhysicsSprite();
|
2012-05-02 14:06:40 +08:00
|
|
|
sprite->initWithTexture(m_pSpriteTexture, CCRectMake(posx, posy, 85, 121));
|
2012-04-19 14:35:52 +08:00
|
|
|
sprite->autorelease();
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
parent->addChild(sprite);
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
sprite->setPosition(pos);
|
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);
|
2012-05-02 14:06:40 +08:00
|
|
|
cpSpaceAddBody(m_pSpace, 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;
|
2012-05-02 14:06:40 +08:00
|
|
|
cpSpaceAddShape(m_pSpace, shape);
|
2012-03-21 15:43:35 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
sprite->setPhysicsBody(body);
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkAccelTouchTestLayer::onEnter()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayer::onEnter();
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkAccelTouchTestLayer::ccTouchesEnded(CCSet* touches, CCEvent* event)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkAccelTouchTestLayer::didAccelerate(CCAcceleration* pAccelerationValue)
|
|
|
|
{
|
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);
|
2012-05-02 14:06:40 +08:00
|
|
|
m_pSpace->gravity = cpv(v.x, v.y);
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChipmunkAccelTouchTestScene::runThisTest()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayer* pLayer = new ChipmunkAccelTouchTestLayer();
|
|
|
|
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
|
|
|
}
|
|
|
|
|