2013-09-10 17:41:06 +08:00
|
|
|
#include "PhysicsTest.h"
|
|
|
|
#include "../testResource.h"
|
2013-09-16 21:22:22 +08:00
|
|
|
USING_NS_CC;
|
2013-09-10 17:41:06 +08:00
|
|
|
|
2013-09-29 15:09:53 +08:00
|
|
|
namespace
|
|
|
|
{
|
2013-09-30 13:02:17 +08:00
|
|
|
static std::function<Layer*()> createFunctions[] = {
|
2013-09-30 20:43:19 +08:00
|
|
|
CL(PhysicsDemoLogoSmash),
|
2013-09-30 13:02:17 +08:00
|
|
|
CL(PhysicsDemoClickAdd),
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sceneIdx=-1;
|
|
|
|
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
|
|
|
|
|
2013-09-29 15:09:53 +08:00
|
|
|
static Layer* next()
|
|
|
|
{
|
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % MAX_LAYER;
|
|
|
|
|
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
layer->init();
|
|
|
|
layer->autorelease();
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Layer* back()
|
|
|
|
{
|
|
|
|
sceneIdx--;
|
|
|
|
int total = MAX_LAYER;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx += total;
|
|
|
|
|
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
layer->init();
|
|
|
|
layer->autorelease();
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Layer* restart()
|
|
|
|
{
|
|
|
|
auto layer = (createFunctions[sceneIdx])();
|
|
|
|
layer->init();
|
|
|
|
layer->autorelease();
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsTestScene::initTest()
|
2013-09-10 17:41:06 +08:00
|
|
|
{
|
|
|
|
#ifdef CC_USE_PHYSICS
|
2013-10-09 13:41:19 +08:00
|
|
|
return TestScene::initWithPhysics();
|
2013-09-29 15:09:53 +08:00
|
|
|
#else
|
|
|
|
return TestScene::init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
sceneIdx = -1;
|
|
|
|
addChild(next());
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(this);
|
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsDemo::PhysicsDemo()
|
|
|
|
: _scene(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsDemo::~PhysicsDemo()
|
|
|
|
{
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-09-29 15:09:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string PhysicsDemo::title()
|
|
|
|
{
|
|
|
|
return "PhysicsTest";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PhysicsDemo::subtitle()
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsDemo::restartCallback(Object* sender)
|
|
|
|
{
|
|
|
|
auto s = new PhysicsTestScene();
|
|
|
|
s->addChild( restart() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsDemo::nextCallback(Object* sender)
|
|
|
|
{
|
|
|
|
auto s = new PhysicsTestScene();
|
|
|
|
s->addChild( next() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsDemo::backCallback(Object* sender)
|
|
|
|
{
|
|
|
|
auto s = new PhysicsTestScene();
|
|
|
|
s->addChild( back() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsDemo::onEnter()
|
|
|
|
{
|
|
|
|
BaseTest::onEnter();
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-09-29 15:09:53 +08:00
|
|
|
_scene = dynamic_cast<Scene*>(this->getParent());
|
|
|
|
#ifdef CC_USE_PHYSICS
|
2013-09-16 21:22:22 +08:00
|
|
|
// menu for debug layer
|
|
|
|
MenuItemFont::setFontSize(18);
|
2013-09-29 15:09:53 +08:00
|
|
|
auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(PhysicsDemo::toggleDebugCallback, this));
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
auto menu = Menu::create(item, NULL);
|
|
|
|
this->addChild(menu);
|
2013-09-29 15:09:53 +08:00
|
|
|
menu->setPosition(Point(VisibleRect::right().x-50, VisibleRect::top().y-10));
|
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PhysicsDemo::toggleDebugCallback(Object* sender)
|
|
|
|
{
|
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
if (_scene != nullptr)
|
|
|
|
{
|
|
|
|
_scene->getPhysicsWorld()->setDebugDraw(!_scene->getPhysicsWorld()->isDebugDraw());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsDemoClickAdd::onEnter()
|
|
|
|
{
|
|
|
|
PhysicsDemo::onEnter();
|
|
|
|
|
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
setTouchEnabled(true);
|
|
|
|
setAccelerometerEnabled(true);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
2013-09-30 13:02:17 +08:00
|
|
|
auto node = Node::create();
|
2013-09-16 21:22:22 +08:00
|
|
|
auto body = PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size);
|
2013-09-30 13:02:17 +08:00
|
|
|
node->setPhysicsBody(body);
|
|
|
|
node->setPosition(VisibleRect::center());
|
|
|
|
this->addChild(node);
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
auto parent = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100);
|
2013-09-10 17:41:06 +08:00
|
|
|
_spriteTexture = parent->getTexture();
|
2013-09-16 21:22:22 +08:00
|
|
|
|
|
|
|
addNewSpriteAtPosition(VisibleRect::center());
|
|
|
|
|
2013-09-10 17:41:06 +08:00
|
|
|
#else
|
2013-09-16 21:22:22 +08:00
|
|
|
auto label = LabelTTF::create("Should define CC_USE_BOX2D or CC_USE_CHIPMUNK\n to run this test case",
|
2013-09-10 17:41:06 +08:00
|
|
|
"Arial",
|
|
|
|
18);
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
label->setPosition(Point(size.width/2, size.height/2));
|
|
|
|
|
|
|
|
addChild(label);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-29 15:09:53 +08:00
|
|
|
void PhysicsDemoClickAdd::addNewSpriteAtPosition(Point p)
|
2013-09-10 17:41:06 +08:00
|
|
|
{
|
2013-09-16 21:22:22 +08:00
|
|
|
#ifdef CC_USE_PHYSICS
|
2013-09-29 15:09:53 +08:00
|
|
|
CCLOG("Add sprite %0.2f x %02.f",p.x,p.y);
|
|
|
|
|
|
|
|
int posx, posy;
|
|
|
|
|
|
|
|
posx = CCRANDOM_0_1() * 200.0f;
|
|
|
|
posy = CCRANDOM_0_1() * 200.0f;
|
|
|
|
|
|
|
|
posx = (posx % 4) * 85;
|
|
|
|
posy = (posy % 3) * 121;
|
|
|
|
|
|
|
|
auto sp = Sprite::createWithTexture(_spriteTexture, Rect(posx, posy, 85, 121));
|
|
|
|
auto body = PhysicsBody::createBox(Size(48, 108));
|
|
|
|
sp->setPhysicsBody(body);
|
|
|
|
this->addChild(sp);
|
|
|
|
sp->setPosition(p);
|
2013-09-16 21:22:22 +08:00
|
|
|
#endif
|
2013-09-10 17:41:06 +08:00
|
|
|
}
|
|
|
|
|
2013-09-29 15:09:53 +08:00
|
|
|
std::string PhysicsDemoClickAdd::subtitle()
|
2013-09-10 17:41:06 +08:00
|
|
|
{
|
2013-09-29 15:09:53 +08:00
|
|
|
return "multi touch to add grossini";
|
2013-09-10 17:41:06 +08:00
|
|
|
}
|
|
|
|
|
2013-09-29 15:09:53 +08:00
|
|
|
void PhysicsDemoClickAdd::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2013-09-10 17:41:06 +08:00
|
|
|
{
|
|
|
|
//Add a new body/atlas sprite at the touched location
|
|
|
|
|
2013-09-16 22:16:50 +08:00
|
|
|
for( auto &touch: touches)
|
2013-09-10 17:41:06 +08:00
|
|
|
{
|
|
|
|
auto location = touch->getLocation();
|
|
|
|
|
|
|
|
addNewSpriteAtPosition( location );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-29 15:09:53 +08:00
|
|
|
void PhysicsDemoClickAdd::onAcceleration(Acceleration* acc, Event* event)
|
2013-09-16 22:16:50 +08:00
|
|
|
{
|
|
|
|
#ifdef CC_USE_PHYSICS
|
|
|
|
static float prevX=0, prevY=0;
|
|
|
|
|
|
|
|
#define kFilterFactor 0.05f
|
|
|
|
|
|
|
|
float accelX = (float) acc->x * kFilterFactor + (1- kFilterFactor)*prevX;
|
|
|
|
float accelY = (float) acc->y * kFilterFactor + (1- kFilterFactor)*prevY;
|
|
|
|
|
|
|
|
prevX = accelX;
|
|
|
|
prevY = accelY;
|
|
|
|
|
|
|
|
auto v = Point( accelX, accelY);
|
|
|
|
v = v * 200;
|
|
|
|
|
|
|
|
if(_scene != nullptr)
|
|
|
|
{
|
|
|
|
_scene->getPhysicsWorld()->setGravity(v);
|
|
|
|
}
|
|
|
|
#endif
|
2013-09-30 20:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
static const int logo_width = 188;
|
|
|
|
static const int logo_height = 35;
|
|
|
|
static const int logo_row_length = 24;
|
|
|
|
static const char logo_image[] =
|
|
|
|
{
|
|
|
|
15,-16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-64,15,63,-32,-2,0,0,0,0,0,0,0,
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,31,-64,15,127,-125,-1,-128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
|
|
0,0,0,127,-64,15,127,15,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-2,
|
|
|
|
31,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,0,-4,63,-1,-32,0,0,0,0,0,0,
|
|
|
|
0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-8,127,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
|
|
1,-1,-64,0,-8,-15,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-31,-1,-64,15,-8,-32,
|
|
|
|
-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-15,-1,-64,9,-15,-32,-1,-32,0,0,0,0,0,
|
|
|
|
0,0,0,0,0,0,0,0,0,0,31,-15,-1,-64,0,-15,-32,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
|
|
0,0,63,-7,-1,-64,9,-29,-32,127,-61,-16,63,15,-61,-1,-8,31,-16,15,-8,126,7,-31,
|
|
|
|
-8,31,-65,-7,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13,
|
|
|
|
-4,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13,
|
|
|
|
-2,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13,
|
|
|
|
-2,63,-33,-1,-1,-32,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13,
|
|
|
|
-1,63,-33,-1,-1,-16,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13,
|
|
|
|
-1,63,-49,-1,-1,-8,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13,
|
|
|
|
-1,-65,-49,-1,-1,-4,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13,
|
|
|
|
-1,-65,-57,-1,-1,-2,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13,
|
|
|
|
-1,-1,-57,-1,-1,-1,9,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1,
|
|
|
|
-1,-61,-1,-1,-1,-119,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1,
|
|
|
|
-1,-61,-1,-1,-1,-55,-49,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1,
|
|
|
|
-1,-63,-1,-1,-1,-23,-49,-32,127,-57,-1,-1,-97,-25,-1,-1,63,-1,-1,-4,-1,15,-13,
|
|
|
|
-1,-1,-63,-1,-1,-1,-16,-49,-32,-1,-25,-1,-1,-97,-25,-1,-1,63,-33,-5,-4,-1,15,
|
|
|
|
-13,-1,-1,-64,-1,-9,-1,-7,-49,-32,-1,-25,-8,127,-97,-25,-1,-1,63,-33,-5,-4,-1,
|
|
|
|
15,-13,-1,-1,-64,-1,-13,-1,-32,-49,-32,-1,-25,-8,127,-97,-25,-1,-2,63,-49,-13,
|
|
|
|
-4,-1,15,-13,-1,-1,-64,127,-7,-1,-119,-17,-15,-1,-25,-8,127,-97,-25,-1,-2,63,
|
|
|
|
-49,-13,-4,-1,15,-13,-3,-1,-64,127,-8,-2,15,-17,-1,-1,-25,-8,127,-97,-25,-1,
|
|
|
|
-8,63,-49,-13,-4,-1,15,-13,-3,-1,-64,63,-4,120,0,-17,-1,-1,-25,-8,127,-97,-25,
|
|
|
|
-8,0,63,-57,-29,-4,-1,15,-13,-4,-1,-64,63,-4,0,15,-17,-1,-1,-25,-8,127,-97,
|
|
|
|
-25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,-1,-64,31,-2,0,0,103,-1,-1,-57,-8,127,-97,
|
|
|
|
-25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,127,-64,31,-2,0,15,103,-1,-1,-57,-8,127,
|
|
|
|
-97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,127,-64,15,-8,0,0,55,-1,-1,-121,-8,
|
|
|
|
127,-97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,63,-64,15,-32,0,0,23,-1,-2,3,-16,
|
|
|
|
63,15,-61,-16,0,31,-127,-127,-8,31,-1,-127,-8,31,-128,7,-128,0,0};
|
|
|
|
|
2013-09-30 21:48:25 +08:00
|
|
|
static inline int get_pixel(int x, int y)
|
2013-09-30 20:43:19 +08:00
|
|
|
{
|
|
|
|
return (logo_image[(x>>3) + y*logo_row_length]>>(~x&0x7)) & 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RAND_MAX 0x7fffffff
|
2013-09-30 21:48:25 +08:00
|
|
|
static inline float frand(void)
|
2013-09-30 20:43:19 +08:00
|
|
|
{
|
|
|
|
return rand()/RAND_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* PhysicsDemoLogoSmash::makeBall(float x, float y)
|
|
|
|
{
|
|
|
|
Sprite* ball = Sprite::create("Images/ball.png");
|
|
|
|
ball->setScale(0.1);
|
|
|
|
|
|
|
|
PhysicsBody* body = PhysicsBody::createCircle(0.95);
|
2013-10-09 13:41:19 +08:00
|
|
|
body->setMass(1.0);
|
|
|
|
body->setAngularDamping(PHYSICS_INFINITY);
|
|
|
|
|
|
|
|
body->getShape()->setElasticity(0);
|
|
|
|
body->getShape()->setFriction(0);
|
|
|
|
|
2013-09-30 21:48:25 +08:00
|
|
|
//body->setDynamic(false);
|
2013-09-30 20:43:19 +08:00
|
|
|
ball->setPhysicsBody(body);
|
|
|
|
|
|
|
|
ball->setPosition(Point(x, y));
|
|
|
|
|
|
|
|
return ball;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsDemoLogoSmash::onEnter()
|
|
|
|
{
|
|
|
|
PhysicsDemo::onEnter();
|
|
|
|
_draw = DrawNode::create();
|
|
|
|
|
2013-09-30 21:48:25 +08:00
|
|
|
_scene->getPhysicsWorld()->setGravity(Point(0, 0));
|
2013-09-30 20:43:19 +08:00
|
|
|
//addChild(makeBall(200, 200));
|
|
|
|
for (int y = 0; y < logo_height; ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < logo_width; ++x)
|
|
|
|
{
|
|
|
|
if (get_pixel(x, y))
|
|
|
|
{
|
|
|
|
float x_jitter = 0.05*frand();
|
|
|
|
float y_jitter = 0.05*frand();
|
|
|
|
|
|
|
|
addChild(makeBall(2*(x - logo_width/2 + x_jitter) + VisibleRect::getVisibleRect().size.width/2,
|
|
|
|
2*(logo_height-y + y_jitter) + VisibleRect::getVisibleRect().size.height/2 - logo_height/2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Sprite* bullet = Sprite::create("Images/ball.png");
|
2013-09-30 21:48:25 +08:00
|
|
|
bullet->setScale(0.5);
|
2013-09-30 20:43:19 +08:00
|
|
|
|
2013-10-09 13:41:19 +08:00
|
|
|
PhysicsBody* body = PhysicsBody::createCircle(8, PHYSICS_INFINITY);
|
2013-09-30 20:43:19 +08:00
|
|
|
body->setVelocity(Point(400, 0));
|
2013-10-09 13:41:19 +08:00
|
|
|
body->getShape()->setElasticity(0);
|
|
|
|
body->getShape()->setFriction(0);
|
2013-09-30 20:43:19 +08:00
|
|
|
bullet->setPhysicsBody(body);
|
|
|
|
|
|
|
|
bullet->setPosition(Point(-1000, VisibleRect::getVisibleRect().size.height/2));
|
2013-09-30 21:48:25 +08:00
|
|
|
|
|
|
|
addChild(bullet);
|
2013-09-30 20:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string PhysicsDemoLogoSmash::title()
|
|
|
|
{
|
|
|
|
return "Logo Smash";
|
2013-09-29 15:09:53 +08:00
|
|
|
}
|