2012-04-19 14:35:52 +08:00
|
|
|
#include "Box2dTest.h"
|
|
|
|
#include "../testResource.h"
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "extensions/cocos-ext.h"
|
2013-12-25 10:54:31 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCCustomCommand.h"
|
|
|
|
|
2012-11-20 16:34:55 +08:00
|
|
|
USING_NS_CC_EXT;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#define PTM_RATIO 32
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kTagParentNode = 1,
|
|
|
|
};
|
|
|
|
|
2010-09-14 14:47:12 +08:00
|
|
|
Box2DTestLayer::Box2DTestLayer()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _spriteTexture(NULL)
|
2012-11-26 17:23:36 +08:00
|
|
|
, world(NULL)
|
2010-09-14 14:47:12 +08:00
|
|
|
{
|
2012-11-26 17:23:36 +08:00
|
|
|
#if CC_ENABLE_BOX2D_INTEGRATION
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled( true );
|
|
|
|
setAccelerometerEnabled( true );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// init physics
|
|
|
|
this->initPhysics();
|
|
|
|
// create reset button
|
|
|
|
this->createResetButton();
|
|
|
|
|
|
|
|
//Set up sprite
|
|
|
|
#if 1
|
|
|
|
// Use batch node. Faster
|
2013-08-16 16:05:27 +08:00
|
|
|
auto parent = SpriteBatchNode::create("Images/blocks.png", 100);
|
2013-06-15 14:03:30 +08:00
|
|
|
_spriteTexture = parent->getTexture();
|
2012-04-19 14:35:52 +08:00
|
|
|
#else
|
|
|
|
// doesn't use batch node. Slower
|
2013-11-07 21:48:39 +08:00
|
|
|
_spriteTexture = Director::getInstance()->getTextureCache()->addImage("Images/blocks.png");
|
2013-08-16 16:05:27 +08:00
|
|
|
auto parent = Node::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
#endif
|
|
|
|
addChild(parent, 0, kTagParentNode);
|
|
|
|
|
|
|
|
|
2012-10-23 17:48:50 +08:00
|
|
|
addNewSpriteAtPosition(VisibleRect::center());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto label = LabelTTF::create("Tap screen", "Marker Felt", 32);
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(label, 0);
|
2013-07-05 16:49:22 +08:00
|
|
|
label->setColor(Color3B(0,0,255));
|
2013-07-12 18:04:32 +08:00
|
|
|
label->setPosition(Point( VisibleRect::center().x, VisibleRect::top().y-50));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
scheduleUpdate();
|
2012-11-26 17:23:36 +08:00
|
|
|
#else
|
2013-08-16 16:05:27 +08:00
|
|
|
auto label = LabelTTF::create("Should define CC_ENABLE_BOX2D_INTEGRATION=1\n to run this test case",
|
2012-11-26 17:23:36 +08:00
|
|
|
"Arial",
|
|
|
|
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));
|
2012-11-26 17:23:36 +08:00
|
|
|
|
2013-07-24 06:20:22 +08:00
|
|
|
addChild(label);
|
2012-11-26 17:23:36 +08:00
|
|
|
#endif
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Box2DTestLayer::~Box2DTestLayer()
|
|
|
|
{
|
2012-11-26 17:23:36 +08:00
|
|
|
CC_SAFE_DELETE(world);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
//delete _debugDraw;
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void Box2DTestLayer::initPhysics()
|
|
|
|
{
|
|
|
|
b2Vec2 gravity;
|
|
|
|
gravity.Set(0.0f, -10.0f);
|
|
|
|
world = new b2World(gravity);
|
|
|
|
|
|
|
|
// Do we want to let bodies sleep?
|
|
|
|
world->SetAllowSleeping(true);
|
|
|
|
|
|
|
|
world->SetContinuousPhysics(true);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// _debugDraw = new GLESDebugDraw( PTM_RATIO );
|
|
|
|
// world->SetDebugDraw(_debugDraw);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
uint32 flags = 0;
|
|
|
|
flags += b2Draw::e_shapeBit;
|
|
|
|
// flags += b2Draw::e_jointBit;
|
|
|
|
// flags += b2Draw::e_aabbBit;
|
|
|
|
// flags += b2Draw::e_pairBit;
|
|
|
|
// flags += b2Draw::e_centerOfMassBit;
|
2013-06-15 14:03:30 +08:00
|
|
|
//_debugDraw->SetFlags(flags);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Define the ground body.
|
|
|
|
b2BodyDef groundBodyDef;
|
|
|
|
groundBodyDef.position.Set(0, 0); // bottom-left corner
|
|
|
|
|
|
|
|
// Call the body factory which allocates memory for the ground body
|
|
|
|
// from a pool and creates the ground box shape (also from a pool).
|
|
|
|
// The body is also added to the world.
|
|
|
|
b2Body* groundBody = world->CreateBody(&groundBodyDef);
|
|
|
|
|
|
|
|
// Define the ground box shape.
|
|
|
|
b2EdgeShape groundBox;
|
|
|
|
|
|
|
|
// bottom
|
2012-10-23 17:48:50 +08:00
|
|
|
groundBox.Set(b2Vec2(VisibleRect::leftBottom().x/PTM_RATIO,VisibleRect::leftBottom().y/PTM_RATIO), b2Vec2(VisibleRect::rightBottom().x/PTM_RATIO,VisibleRect::rightBottom().y/PTM_RATIO));
|
2012-04-19 14:35:52 +08:00
|
|
|
groundBody->CreateFixture(&groundBox,0);
|
|
|
|
|
|
|
|
// top
|
2012-10-23 17:48:50 +08:00
|
|
|
groundBox.Set(b2Vec2(VisibleRect::leftTop().x/PTM_RATIO,VisibleRect::leftTop().y/PTM_RATIO), b2Vec2(VisibleRect::rightTop().x/PTM_RATIO,VisibleRect::rightTop().y/PTM_RATIO));
|
2012-04-19 14:35:52 +08:00
|
|
|
groundBody->CreateFixture(&groundBox,0);
|
|
|
|
|
|
|
|
// left
|
2012-10-23 17:48:50 +08:00
|
|
|
groundBox.Set(b2Vec2(VisibleRect::leftTop().x/PTM_RATIO,VisibleRect::leftTop().y/PTM_RATIO), b2Vec2(VisibleRect::leftBottom().x/PTM_RATIO,VisibleRect::leftBottom().y/PTM_RATIO));
|
2012-04-19 14:35:52 +08:00
|
|
|
groundBody->CreateFixture(&groundBox,0);
|
|
|
|
|
|
|
|
// right
|
2012-10-23 17:48:50 +08:00
|
|
|
groundBox.Set(b2Vec2(VisibleRect::rightBottom().x/PTM_RATIO,VisibleRect::rightBottom().y/PTM_RATIO), b2Vec2(VisibleRect::rightTop().x/PTM_RATIO,VisibleRect::rightTop().y/PTM_RATIO));
|
2012-04-19 14:35:52 +08:00
|
|
|
groundBody->CreateFixture(&groundBox,0);
|
2012-03-21 15:43:35 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void Box2DTestLayer::createResetButton()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto reset = MenuItemImage::create("Images/r1.png", "Images/r2.png", [](Object *sender) {
|
|
|
|
auto s = new Box2DTestScene();
|
|
|
|
auto child = new Box2DTestLayer();
|
2013-06-08 08:21:11 +08:00
|
|
|
s->addChild(child);
|
|
|
|
child->release();
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2013-06-08 08:21:11 +08:00
|
|
|
s->release();
|
|
|
|
});
|
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
|
|
|
|
2013-07-12 14:11:55 +08:00
|
|
|
menu->setPosition(Point(VisibleRect::bottom().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
|
|
|
}
|
|
|
|
|
2010-09-14 14:47:12 +08:00
|
|
|
void Box2DTestLayer::draw()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// IMPORTANT:
|
|
|
|
// This is only for debug purposes
|
|
|
|
// It is recommend to disable it
|
|
|
|
//
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer::draw();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-26 17:23:36 +08:00
|
|
|
#if CC_ENABLE_BOX2D_INTEGRATION
|
2013-07-28 14:33:02 +08:00
|
|
|
GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
kmGLPushMatrix();
|
2013-12-25 10:54:31 +08:00
|
|
|
kmGLGetMatrix(KM_GL_MODELVIEW, &_modelViewMV);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
_customCommand.init(_globalZOrder);
|
2013-12-26 21:19:12 +08:00
|
|
|
_customCommand.func = CC_CALLBACK_0(Box2DTestLayer::onDraw, this);
|
|
|
|
Director::getInstance()->getRenderer()->addCommand(&_customCommand);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
kmGLPopMatrix();
|
2012-11-26 17:23:36 +08:00
|
|
|
#endif
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
|
2013-12-25 10:54:31 +08:00
|
|
|
#if CC_ENABLE_BOX2D_INTEGRATION
|
|
|
|
void Box2DTestLayer::onDraw()
|
|
|
|
{
|
|
|
|
kmMat4 oldMV;
|
|
|
|
kmGLGetMatrix(KM_GL_MODELVIEW, &oldMV);
|
|
|
|
kmGLLoadMatrix(&_modelViewMV);
|
|
|
|
world->DrawDebugData();
|
|
|
|
kmGLLoadMatrix(&oldMV);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void Box2DTestLayer::addNewSpriteAtPosition(Point p)
|
2010-09-14 14:47:12 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("Add sprite %0.2f x %02.f",p.x,p.y);
|
|
|
|
|
|
|
|
// Define the dynamic body.
|
|
|
|
//Set up a 1m squared box in the physics world
|
|
|
|
b2BodyDef bodyDef;
|
|
|
|
bodyDef.type = b2_dynamicBody;
|
|
|
|
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
|
|
|
|
|
|
|
|
b2Body *body = world->CreateBody(&bodyDef);
|
|
|
|
|
|
|
|
// Define another box shape for our dynamic body.
|
|
|
|
b2PolygonShape dynamicBox;
|
|
|
|
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
|
|
|
|
|
|
|
|
// Define the dynamic body fixture.
|
|
|
|
b2FixtureDef fixtureDef;
|
|
|
|
fixtureDef.shape = &dynamicBox;
|
|
|
|
fixtureDef.density = 1.0f;
|
|
|
|
fixtureDef.friction = 0.3f;
|
2013-01-28 20:34:52 +08:00
|
|
|
body->CreateFixture(&fixtureDef);
|
|
|
|
|
|
|
|
#if CC_ENABLE_BOX2D_INTEGRATION
|
2013-08-16 16:05:27 +08:00
|
|
|
auto parent = this->getChildByTag(kTagParentNode);
|
2013-01-28 20:34:52 +08:00
|
|
|
|
2012-11-26 17:23:36 +08:00
|
|
|
//We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
|
|
|
|
//just randomly picking one of the images
|
|
|
|
int idx = (CCRANDOM_0_1() > .5 ? 0:1);
|
|
|
|
int idy = (CCRANDOM_0_1() > .5 ? 0:1);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sprite = PhysicsSprite::createWithTexture(_spriteTexture,Rect(32 * idx,32 * idy,32,32));
|
2012-11-26 17:23:36 +08:00
|
|
|
parent->addChild(sprite);
|
2013-01-06 10:22:07 +08:00
|
|
|
sprite->setB2Body(body);
|
2012-11-26 17:23:36 +08:00
|
|
|
sprite->setPTMRatio(PTM_RATIO);
|
2013-07-12 18:04:32 +08:00
|
|
|
sprite->setPosition( Point( p.x, p.y) );
|
2012-11-20 16:34:55 +08:00
|
|
|
#endif
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void Box2DTestLayer::update(float dt)
|
2010-09-14 14:47:12 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
//It is recommended that a fixed time step is used with Box2D for stability
|
|
|
|
//of the simulation, however, we are using a variable time step here.
|
|
|
|
//You need to make an informed choice, the following URL is useful
|
|
|
|
//http://gafferongames.com/game-physics/fix-your-timestep/
|
|
|
|
|
|
|
|
int velocityIterations = 8;
|
|
|
|
int positionIterations = 1;
|
|
|
|
|
|
|
|
// Instruct the world to perform a single step of simulation. It is
|
|
|
|
// generally best to keep the time step and iterations fixed.
|
|
|
|
world->Step(dt, velocityIterations, positionIterations);
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void Box2DTestLayer::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2010-09-14 14:47:12 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
//Add a new body/atlas sprite at the touched location
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
for (auto& touch : touches)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if(!touch)
|
|
|
|
break;
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto location = touch->getLocation();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
addNewSpriteAtPosition( location );
|
|
|
|
}
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-06-20 14:17:10 +08:00
|
|
|
void Box2DTestLayer::accelerometer(UIAccelerometer* accelerometer, Acceleration* acceleration)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
static float prevX=0, prevY=0;
|
2010-09-14 14:47:12 +08:00
|
|
|
|
|
|
|
//#define kFilterFactor 0.05f
|
2012-04-19 14:35:52 +08:00
|
|
|
#define kFilterFactor 1.0f // don't use filter. the code is here just as an example
|
|
|
|
|
|
|
|
float accelX = (float) acceleration.x * kFilterFactor + (1- kFilterFactor)*prevX;
|
|
|
|
float accelY = (float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY;
|
|
|
|
|
|
|
|
prevX = accelX;
|
|
|
|
prevY = accelY;
|
|
|
|
|
|
|
|
// accelerometer values are in "Portrait" mode. Change them to Landscape left
|
|
|
|
// multiply the gravity by 10
|
|
|
|
b2Vec2 gravity( -accelY * 10, accelX * 10);
|
|
|
|
|
|
|
|
world->SetGravity( gravity );
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Box2DTestScene::runThisTest()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto layer = new Box2DTestLayer();
|
2013-07-23 08:25:44 +08:00
|
|
|
addChild(layer);
|
|
|
|
layer->release();
|
2010-09-14 14:47:12 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
|