31#include "chipmunk/chipmunk.h"
50#if CC_ENABLE_CHIPMUNK_INTEGRATION
53 auto touchListener = EventListenerTouchAllAtOnce::create();
55 _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener,
this);
57 Device::setAccelerometerEnabled(
true);
59 _eventDispatcher->addEventListenerWithSceneGraphPriority(accListener,
this);
62 auto label = Label::createWithTTF(
"Multi touch the screen",
"fonts/Marker Felt.ttf", 36.0f);
64 this->addChild(label, -1);
74 auto parent = SpriteBatchNode::create(
"Images/grossini_dance_atlas.png", 100);
78 _spriteTexture = Director::getInstance()->getTextureCache()->addImage(
"Images/grossini_dance_atlas.png");
79 auto parent = Node::create();
86 MenuItemFont::setFontSize(18);
89 auto menu = Menu::create(item,
nullptr);
95 auto label = Label::createWithTTF(
"Should define CC_ENABLE_CHIPMUNK_INTEGRATION=1\n to run this test case",
98 auto size = Director::getInstance()->getWinSize();
99 label->setPosition(size.width/2, size.height/2);
109#if CC_ENABLE_CHIPMUNK_INTEGRATION
110 _debugLayer->setVisible(! _debugLayer->isVisible());
116#if CC_ENABLE_CHIPMUNK_INTEGRATION
118 for(
int i=0;i<4;i++) {
122#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
128 Device::setAccelerometerEnabled(
false);
134#if CC_ENABLE_CHIPMUNK_INTEGRATION
138#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
141 _space = cpHastySpaceNew();
142 cpHastySpaceSetThreads(
_space, 0);
145 cpSpaceSetGravity(
_space, cpv(0.0f, -100.0f));
152 _walls[0] = cpSegmentShapeNew( cpSpaceGetStaticBody(
_space),
157 _walls[1] = cpSegmentShapeNew( cpSpaceGetStaticBody(
_space),
162 _walls[2] = cpSegmentShapeNew( cpSpaceGetStaticBody(
_space),
167 _walls[3] = cpSegmentShapeNew( cpSpaceGetStaticBody(
_space),
171 for(
int i=0;i<4;i++) {
173 cpShapeSetElasticity(
_walls[i], 1.0f);
174 cpShapeSetFriction(
_walls[i], 1.0f);
179 _debugLayer = PhysicsDebugNode::create(
_space);
188 float dt = Director::getInstance()->getAnimationInterval()/(float)steps;
190 for(
int i=0; i<steps; i++){
192#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
195 cpHastySpaceStep(
_space, dt);
204 auto menu = Menu::create(
reset,
nullptr);
207 this->addChild(menu, -1);
217#if CC_ENABLE_CHIPMUNK_INTEGRATION
222 posx = CCRANDOM_0_1() * 200.0f;
223 posy = CCRANDOM_0_1() * 200.0f;
225 posx = (posx % 4) * 85;
226 posy = (posy % 3) * 121;
237 cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, cpvzero, 0.0f));
239 cpBodySetPosition(body, cpv(pos.x, pos.y));
240 cpSpaceAddBody(
_space, body);
242 cpShape* shape = cpPolyShapeNew(body, num, verts, cpTransformIdentity, 0.0f);
243 cpShapeSetElasticity(shape, 0.5f);
244 cpShapeSetFriction(shape, 0.5f);
245 cpSpaceAddShape(
_space, shape);
247 auto sprite = PhysicsSprite::createWithTexture(
_spriteTexture, cocos2d::Rect(posx, posy, 85, 121));
248 parent->addChild(sprite);
250 sprite->setCPBody(body);
251 sprite->setPosition(pos);
264 for(
auto &touch: touches)
266 auto location = touch->getLocation();
274 static float prevX=0, prevY=0;
276#define kFilterFactor 0.05f
284 auto v = cocos2d::Vec2( accelX, accelY);
286 cpSpaceSetGravity(
_space, cpv(v.x, v.y));
289ChipmunkTests::ChipmunkTests()
#define ADD_TEST_CASE(__className__)
void addNewSpriteAtPosition(cocos2d::Vec2 p)
void toggleDebugCallback(cocos2d::Ref *sender)
void reset(cocos2d::Ref *sender)
void update(float dt) override
virtual void onAcceleration(cocos2d::Acceleration *acc, cocos2d::Event *event)
cocos2d::Texture2D * _spriteTexture
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
TestSuite * getTestSuite() const
virtual void onEnter() override
virtual void restartCurrTest()
static cocos2d::Vec2 top()
static cocos2d::Vec2 leftTop()
static cocos2d::Vec2 center()
static cocos2d::Vec2 bottom()
static cocos2d::Vec2 rightTop()
static cocos2d::Vec2 rightBottom()
static cocos2d::Vec2 leftBottom()
static cocos2d::Vec2 right()