axmol/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp

1958 lines
70 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-28 16:06:23 +08:00
2022-02-24 18:45:25 +08:00
https://adxeproject.github.io/
2021-12-28 16:06:23 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-28 16:06:23 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-28 16:06:23 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "PhysicsTest.h"
2015-09-08 09:54:01 +08:00
#if CC_USE_PHYSICS
2021-12-28 16:06:23 +08:00
# include <cmath>
# include "ui/CocosGUI.h"
# include "../testResource.h"
2015-09-08 09:54:01 +08:00
USING_NS_CC;
PhysicsTests::PhysicsTests()
2013-09-29 15:09:53 +08:00
{
ADD_TEST_CASE(PhysicsDemoLogoSmash);
ADD_TEST_CASE(PhysicsDemoPyramidStack);
ADD_TEST_CASE(PhysicsDemoClickAdd);
ADD_TEST_CASE(PhysicsDemoRayCast);
ADD_TEST_CASE(PhysicsDemoActions);
2015-09-08 09:54:01 +08:00
ADD_TEST_CASE(PhysicsDemoJoints);
ADD_TEST_CASE(PhysicsDemoPump);
ADD_TEST_CASE(PhysicsDemoOneWayPlatform);
ADD_TEST_CASE(PhysicsDemoSlice);
ADD_TEST_CASE(PhysicsDemoBug3988);
ADD_TEST_CASE(PhysicsContactTest);
ADD_TEST_CASE(PhysicsPositionRotationTest);
ADD_TEST_CASE(PhysicsSetGravityEnableTest);
2015-09-08 09:54:01 +08:00
ADD_TEST_CASE(PhysicsDemoBug5482);
ADD_TEST_CASE(PhysicsFixedUpdate);
ADD_TEST_CASE(PhysicsTransformTest);
ADD_TEST_CASE(PhysicsIssue9959);
ADD_TEST_CASE(PhysicsIssue15932);
2013-09-29 15:09:53 +08:00
}
2015-09-08 09:54:01 +08:00
namespace
{
2021-12-28 16:06:23 +08:00
Color4F STATIC_COLOR(1.0f, 0.0f, 0.0f, 1.0f);
const int DRAG_BODYS_TAG = 0x80;
} // namespace
2013-09-29 15:09:53 +08:00
void PhysicsDemo::toggleDebug()
2014-05-02 07:55:28 +08:00
{
2021-12-28 16:06:23 +08:00
# if CC_USE_PHYSICS
2014-05-02 07:55:28 +08:00
_debugDraw = !_debugDraw;
2015-09-08 09:54:01 +08:00
_physicsWorld->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE);
2021-12-28 16:06:23 +08:00
# endif
2014-05-02 07:55:28 +08:00
}
2021-12-28 16:06:23 +08:00
PhysicsDemo::PhysicsDemo() : _spriteTexture(nullptr), _ball(nullptr), _debugDraw(false) {}
2013-09-29 15:09:53 +08:00
2015-09-08 09:54:01 +08:00
bool PhysicsDemo::init()
2013-09-29 15:09:53 +08:00
{
2015-09-08 09:54:01 +08:00
TestCase::init();
return initWithPhysics();
2013-09-29 15:09:53 +08:00
}
2021-12-28 16:06:23 +08:00
PhysicsDemo::~PhysicsDemo() {}
2015-09-08 09:54:01 +08:00
std::string PhysicsDemo::title() const
2013-09-29 15:09:53 +08:00
{
2015-09-08 09:54:01 +08:00
return "PhysicsComponentTest";
2013-09-29 15:09:53 +08:00
}
void PhysicsDemo::onEnter()
{
TestCase::onEnter();
2021-12-28 16:06:23 +08:00
2013-10-09 17:53:12 +08:00
_spriteTexture = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100)->getTexture();
2021-12-28 16:06:23 +08:00
2014-05-02 07:55:28 +08:00
// menu for debug layer
MenuItemFont::setFontSize(18);
auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(PhysicsDemo::toggleDebugCallback, this));
2021-12-28 16:06:23 +08:00
2014-08-12 11:29:37 +08:00
auto menu = Menu::create(item, nullptr);
2014-05-02 07:55:28 +08:00
this->addChild(menu);
2021-12-28 16:06:23 +08:00
menu->setPosition(Vec2(VisibleRect::right().x - item->getContentSize().width / 2 - 10,
VisibleRect::top().y - item->getContentSize().height / 2 - 10));
2013-09-29 15:09:53 +08:00
}
2021-12-28 16:06:23 +08:00
Sprite* PhysicsDemo::addGrossiniAtPosition(Vec2 p, float scale /* = 1.0*/)
2013-10-09 17:53:12 +08:00
{
2015-09-08 09:54:01 +08:00
CCLOG("Add sprite %0.2f x %02.f", p.x, p.y);
2021-12-28 16:06:23 +08:00
2013-10-09 17:53:12 +08:00
int posx, posy;
2021-12-28 16:06:23 +08:00
2013-10-09 17:53:12 +08:00
posx = CCRANDOM_0_1() * 200.0f;
posy = CCRANDOM_0_1() * 200.0f;
2021-12-28 16:06:23 +08:00
2013-10-09 17:53:12 +08:00
posx = (posx % 4) * 85;
posy = (posy % 3) * 121;
2021-12-28 16:06:23 +08:00
2013-10-09 17:53:12 +08:00
auto sp = Sprite::createWithTexture(_spriteTexture, Rect(posx, posy, 85, 121));
2021-12-28 16:06:23 +08:00
2013-10-09 17:53:12 +08:00
sp->setScale(scale);
sp->setPosition(p);
sp->addComponent(PhysicsBody::createBox(Size(48.0f, 108.0f)));
2015-09-08 09:54:01 +08:00
this->addChild(sp);
2021-12-28 16:06:23 +08:00
return sp;
2013-10-09 17:53:12 +08:00
}
void PhysicsDemo::toggleDebugCallback(Ref* /*sender*/)
2013-09-29 15:09:53 +08:00
{
toggleDebug();
2013-09-29 15:09:53 +08:00
}
namespace
{
2021-12-28 16:06:23 +08:00
const int LOGO_WIDTH = 188;
const int LOGO_HEIGHT = 35;
const int LOGO_RAW_LENGTH = 24;
const int 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};
int getPixel(int x, int y)
{
return (LOGO_IMAGE[(x >> 3) + y * LOGO_RAW_LENGTH] >> (~x & 0x7)) & 1;
}
float frand()
{
return rand() / RAND_MAX;
}
} // namespace
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Sprite* PhysicsDemo::makeBall(Vec2 point, float radius, PhysicsMaterial material)
{
2013-10-22 18:00:24 +08:00
Sprite* ball = nullptr;
if (_ball != nullptr)
ball = Sprite::createWithTexture(_ball->getTexture());
2015-09-08 09:54:01 +08:00
else
2013-10-22 18:00:24 +08:00
ball = Sprite::create("Images/ball.png");
2021-12-28 16:06:23 +08:00
2013-10-22 18:00:24 +08:00
ball->setScale(0.13f * radius);
2021-12-28 16:06:23 +08:00
ball->addComponent(PhysicsBody::createCircle(ball->getContentSize().width / 2, material));
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
ball->setPosition(Vec2(point.x, point.y));
2021-12-28 16:06:23 +08:00
return ball;
}
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Sprite* PhysicsDemo::makeBox(Vec2 point, Size size, int color, PhysicsMaterial material)
2013-10-22 18:00:24 +08:00
{
2014-02-25 15:28:13 +08:00
bool yellow = false;
if (color == 0)
{
yellow = CCRANDOM_0_1() > 0.5f;
2015-09-08 09:54:01 +08:00
}
else
2014-02-25 15:28:13 +08:00
{
yellow = color == 1;
}
2021-12-28 16:06:23 +08:00
2014-02-25 15:28:13 +08:00
auto box = yellow ? Sprite::create("Images/YellowSquare.png") : Sprite::create("Images/CyanSquare.png");
2015-09-08 09:54:01 +08:00
box->setScaleX(size.width / 100.0f);
box->setScaleY(size.height / 100.0f);
2021-12-28 16:06:23 +08:00
box->addComponent(PhysicsBody::createBox(box->getContentSize(), material));
2021-12-28 16:06:23 +08:00
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
box->setPosition(Vec2(point.x, point.y));
2021-12-28 16:06:23 +08:00
2013-10-22 18:00:24 +08:00
return box;
}
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Sprite* PhysicsDemo::makeTriangle(Vec2 point, Size size, int color, PhysicsMaterial material)
2013-10-22 18:00:24 +08:00
{
2014-02-25 15:28:13 +08:00
bool yellow = false;
if (color == 0)
{
yellow = CCRANDOM_0_1() > 0.5f;
2015-09-08 09:54:01 +08:00
}
else
2014-02-25 15:28:13 +08:00
{
yellow = color == 1;
}
2021-12-28 16:06:23 +08:00
2014-02-25 15:28:13 +08:00
auto triangle = yellow ? Sprite::create("Images/YellowTriangle.png") : Sprite::create("Images/CyanTriangle.png");
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
if (size.height == 0)
2013-10-22 18:00:24 +08:00
{
2015-09-08 09:54:01 +08:00
triangle->setScale(size.width / 100.0f);
}
else
2013-10-22 18:00:24 +08:00
{
2015-09-08 09:54:01 +08:00
triangle->setScaleX(size.width / 50.0f);
triangle->setScaleY(size.height / 43.5f);
2013-10-22 18:00:24 +08:00
}
2021-12-28 16:06:23 +08:00
Vec2 vers[] = {Vec2(0.0f, triangle->getContentSize().height / 2),
Vec2(triangle->getContentSize().width / 2, -triangle->getContentSize().height / 2),
Vec2(-triangle->getContentSize().width / 2, -triangle->getContentSize().height / 2)};
triangle->addComponent(PhysicsBody::createPolygon(vers, 3, material));
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
triangle->setPosition(Vec2(point.x, point.y));
2021-12-28 16:06:23 +08:00
2013-10-22 18:00:24 +08:00
return triangle;
}
bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
{
2014-05-02 07:55:28 +08:00
auto location = touch->getLocation();
2021-12-28 16:06:23 +08:00
auto arr = _physicsWorld->getShapes(location);
2013-11-18 21:28:40 +08:00
PhysicsBody* body = nullptr;
2013-12-07 14:28:37 +08:00
for (auto& obj : arr)
{
2013-12-07 14:28:37 +08:00
if ((obj->getBody()->getTag() & DRAG_BODYS_TAG) != 0)
{
2013-12-07 14:28:37 +08:00
body = obj->getBody();
break;
}
}
2021-12-28 16:06:23 +08:00
2013-11-18 21:28:40 +08:00
if (body != nullptr)
{
2021-12-28 16:06:23 +08:00
Node* mouse = Node::create();
2015-09-08 09:54:01 +08:00
auto physicsBody = PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY);
physicsBody->setDynamic(false);
mouse->addComponent(physicsBody);
mouse->setPosition(location);
this->addChild(mouse);
2015-09-08 09:54:01 +08:00
PhysicsJointPin* joint = PhysicsJointPin::construct(physicsBody, body, location);
2013-11-18 21:28:40 +08:00
joint->setMaxForce(5000.0f * body->getMass());
2015-09-08 09:54:01 +08:00
_physicsWorld->addJoint(joint);
_mouses.insert(std::make_pair(touch->getID(), mouse));
2021-12-28 16:06:23 +08:00
return true;
}
2021-12-28 16:06:23 +08:00
return false;
}
void PhysicsDemo::onTouchMoved(Touch* touch, Event* /*event*/)
{
auto it = _mouses.find(touch->getID());
2021-12-28 16:06:23 +08:00
if (it != _mouses.end())
{
2014-05-02 07:55:28 +08:00
it->second->setPosition(touch->getLocation());
}
}
void PhysicsDemo::onTouchEnded(Touch* touch, Event* /*event*/)
{
auto it = _mouses.find(touch->getID());
2021-12-28 16:06:23 +08:00
if (it != _mouses.end())
{
this->removeChild(it->second);
_mouses.erase(it);
}
}
2015-09-08 09:54:01 +08:00
// Implementation of PhysicsComponentDemoLogoSmash
void PhysicsDemoLogoSmash::onEnter()
{
PhysicsDemo::onEnter();
2021-12-28 16:06:23 +08:00
_physicsWorld->setGravity(Vec2(0.0f, 0.0f));
2015-09-08 09:54:01 +08:00
_physicsWorld->setUpdateRate(5.0f);
2021-12-28 16:06:23 +08:00
_ball = SpriteBatchNode::create("Images/ball.png", sizeof(LOGO_IMAGE) / sizeof(LOGO_IMAGE[0]));
addChild(_ball);
2015-09-08 09:54:01 +08:00
for (int y = 0; y < LOGO_HEIGHT; ++y)
{
2015-09-08 09:54:01 +08:00
for (int x = 0; x < LOGO_WIDTH; ++x)
{
2015-09-08 09:54:01 +08:00
if (getPixel(x, y))
{
2015-09-08 09:54:01 +08:00
float xJitter = 0.05 * frand();
float yJitter = 0.05 * frand();
2021-12-28 16:06:23 +08:00
Node* ball =
makeBall(Vec2(2 * (x - LOGO_WIDTH / 2 + xJitter) + VisibleRect::getVisibleRect().size.width / 2,
2 * (LOGO_HEIGHT - y + yJitter) + VisibleRect::getVisibleRect().size.height / 2 -
LOGO_HEIGHT / 2),
0.95f, PhysicsMaterial(0.01f, 0.0f, 0.0f));
auto physicsBody = ball->getPhysicsBody();
2015-09-08 09:54:01 +08:00
physicsBody->setMass(1.0);
physicsBody->setMoment(PHYSICS_INFINITY);
2021-12-28 16:06:23 +08:00
2013-10-22 18:00:24 +08:00
_ball->addChild(ball);
}
}
}
2021-12-28 16:06:23 +08:00
auto bullet = makeBall(Vec2(400.0f, 0.0f), 10, PhysicsMaterial(PHYSICS_INFINITY, 0, 0));
bullet->getPhysicsBody()->setVelocity(Vec2(200.0f, 0.0f));
2021-12-28 16:06:23 +08:00
bullet->setPosition(Vec2(-500.0f, VisibleRect::getVisibleRect().size.height / 2));
_ball->addChild(bullet);
}
std::string PhysicsDemoLogoSmash::title() const
{
return "Logo Smash";
}
2015-09-08 09:54:01 +08:00
// Implementation of PhysicsComponentDemoClickAdd
PhysicsDemoClickAdd::~PhysicsDemoClickAdd()
{
2015-09-08 09:54:01 +08:00
Device::setAccelerometerEnabled(false);
}
void PhysicsDemoClickAdd::onEnter()
{
PhysicsDemo::onEnter();
2021-12-28 16:06:23 +08:00
auto touchListener = EventListenerTouchAllAtOnce::create();
2015-09-08 09:54:01 +08:00
touchListener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoClickAdd::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
Device::setAccelerometerEnabled(true);
auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(PhysicsDemoClickAdd::onAcceleration, this));
_eventDispatcher->addEventListenerWithSceneGraphPriority(accListener, this);
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
auto node = Node::create();
node->addComponent(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size));
2015-09-08 09:54:01 +08:00
node->setPosition(VisibleRect::center());
this->addChild(node);
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
addGrossiniAtPosition(VisibleRect::center());
}
std::string PhysicsDemoClickAdd::subtitle() const
{
return "multi touch to add grossini";
}
void PhysicsDemoClickAdd::onTouchesEnded(const std::vector<Touch*>& touches, Event* /*event*/)
2015-09-08 09:54:01 +08:00
{
2021-12-28 16:06:23 +08:00
// Add a new body/atlas sprite at the touched location
for (auto touch : touches)
2015-09-08 09:54:01 +08:00
{
auto location = touch->getLocation();
2021-12-28 16:06:23 +08:00
addGrossiniAtPosition(location);
2015-09-08 09:54:01 +08:00
}
}
void PhysicsDemoClickAdd::onAcceleration(Acceleration* acc, Event* /*event*/)
2015-09-08 09:54:01 +08:00
{
2021-12-28 16:06:23 +08:00
static float prevX = 0, prevY = 0;
# define FILTER_FACTOR 0.05f
float accelX = (float)acc->x * FILTER_FACTOR + (1 - FILTER_FACTOR) * prevX;
float accelY = (float)acc->y * FILTER_FACTOR + (1 - FILTER_FACTOR) * prevY;
2015-09-08 09:54:01 +08:00
prevX = accelX;
prevY = accelY;
2021-12-28 16:06:23 +08:00
auto v = Vec2(accelX, accelY);
v = v * 200;
2015-09-08 09:54:01 +08:00
_physicsWorld->setGravity(v);
}
void PhysicsDemoPyramidStack::onEnter()
2013-10-09 17:53:12 +08:00
{
PhysicsDemo::onEnter();
2021-12-28 16:06:23 +08:00
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoPyramidStack::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoPyramidStack::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoPyramidStack::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
2021-12-28 16:06:23 +08:00
auto node = Node::create();
node->addComponent(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0.0f, 50.0f),
2021-12-28 16:06:23 +08:00
VisibleRect::rightBottom() + Vec2(0.0f, 50.0f)));
this->addChild(node);
2021-12-28 16:06:23 +08:00
auto ball = Sprite::create("Images/ball.png");
ball->setScale(1);
ball->setTag(100);
auto body = PhysicsBody::createCircle(10);
ball->addComponent(body);
body->setTag(DRAG_BODYS_TAG);
ball->setPosition(VisibleRect::bottom() + Vec2(0.0f, 60.0f));
this->addChild(ball);
2021-12-28 16:06:23 +08:00
scheduleOnce(CC_SCHEDULE_SELECTOR(PhysicsDemoPyramidStack::updateOnce), 3.0);
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
for (int i = 0; i < 14; i++)
{
2015-09-08 09:54:01 +08:00
for (int j = 0; j <= i; j++)
{
2015-09-08 09:54:01 +08:00
auto sp = addGrossiniAtPosition(VisibleRect::bottom() + Vec2((i / 2 - j) * 11, (14 - i) * 23 + 100), 0.2f);
sp->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
2013-11-18 10:15:50 +08:00
}
}
2013-10-09 17:53:12 +08:00
}
2015-09-08 09:54:01 +08:00
void PhysicsDemoPyramidStack::updateOnce(float /*delta*/)
2015-09-08 09:54:01 +08:00
{
auto ball = getChildByTag(100);
if (ball)
ball->setScale(ball->getScale() * 3);
}
std::string PhysicsDemoPyramidStack::title() const
2013-10-09 17:53:12 +08:00
{
return "Pyramid Stack";
2013-10-22 18:00:24 +08:00
}
2021-12-28 16:06:23 +08:00
PhysicsDemoRayCast::PhysicsDemoRayCast() : _angle(0.0f), _node(nullptr), _mode(0) {}
2013-10-22 18:00:24 +08:00
void PhysicsDemoRayCast::onEnter()
{
PhysicsDemo::onEnter();
2021-12-28 16:06:23 +08:00
auto listener = EventListenerTouchAllAtOnce::create();
2013-10-24 17:46:13 +08:00
listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
_physicsWorld->setGravity(Point::ZERO);
2021-12-28 16:06:23 +08:00
2013-10-22 18:00:24 +08:00
auto node = DrawNode::create();
2021-12-28 16:06:23 +08:00
node->addComponent(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0.0f, 50.0f),
VisibleRect::rightBottom() + Vec2(0.0f, 50.0f)));
node->drawSegment(VisibleRect::leftBottom() + Vec2(0.0f, 50.0f), VisibleRect::rightBottom() + Vec2(0.0f, 50.0f), 1,
STATIC_COLOR);
2013-10-22 18:00:24 +08:00
this->addChild(node);
2021-12-28 16:06:23 +08:00
2013-10-22 18:00:24 +08:00
MenuItemFont::setFontSize(18);
auto item = MenuItemFont::create("Change Mode(any)", CC_CALLBACK_1(PhysicsDemoRayCast::changeModeCallback, this));
2021-12-28 16:06:23 +08:00
2014-08-12 11:29:37 +08:00
auto menu = Menu::create(item, nullptr);
2013-10-22 18:00:24 +08:00
this->addChild(menu);
2015-09-08 09:54:01 +08:00
menu->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::top().y - 10));
2021-12-28 16:06:23 +08:00
2014-05-02 07:55:28 +08:00
scheduleUpdate();
2013-10-22 18:00:24 +08:00
}
void PhysicsDemoRayCast::changeModeCallback(Ref* sender)
2013-10-22 18:00:24 +08:00
{
_mode = (_mode + 1) % 3;
2021-12-28 16:06:23 +08:00
2013-10-22 18:00:24 +08:00
switch (_mode)
{
2021-12-28 16:06:23 +08:00
case 0:
((MenuItemFont*)sender)->setString("Change Mode(any)");
break;
case 1:
((MenuItemFont*)sender)->setString("Change Mode(nearest)");
break;
case 2:
((MenuItemFont*)sender)->setString("Change Mode(multiple)");
break;
default:
break;
2013-10-22 18:00:24 +08:00
}
}
bool PhysicsDemoRayCast::anyRay(PhysicsWorld& /*world*/, const PhysicsRayCastInfo& info, void* data)
2013-10-22 18:00:24 +08:00
{
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
*((Vec2*)data) = info.contact;
2013-10-22 18:00:24 +08:00
return false;
}
void PhysicsDemoRayCast::update(float /*delta*/)
2013-10-22 18:00:24 +08:00
{
2021-12-28 16:06:23 +08:00
float L = 150.0f;
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 point1 = VisibleRect::center();
Vec2 d(L * cosf(_angle), L * sinf(_angle));
Vec2 point2 = point1 + d;
2021-12-28 16:06:23 +08:00
2013-10-22 18:00:24 +08:00
removeChild(_node);
_node = DrawNode::create();
switch (_mode)
{
2021-12-28 16:06:23 +08:00
case 0:
{
Vec2 point3 = point2;
auto func = CC_CALLBACK_3(PhysicsDemoRayCast::anyRay, this);
_physicsWorld->rayCast(func, point1, point2, &point3);
_node->drawSegment(point1, point3, 1, STATIC_COLOR);
if (point2 != point3)
2013-10-22 18:00:24 +08:00
{
2021-12-28 16:06:23 +08:00
_node->drawDot(point3, 2, Color4F(1.0f, 1.0f, 1.0f, 1.0f));
2013-10-22 18:00:24 +08:00
}
2021-12-28 16:06:23 +08:00
addChild(_node);
break;
}
case 1:
{
Vec2 point3 = point2;
float friction = 1.0f;
PhysicsRayCastCallbackFunc func = [&point3, &friction](PhysicsWorld& /*world*/, const PhysicsRayCastInfo& info,
void* /*data*/) -> bool {
if (friction > info.fraction)
2013-10-22 18:00:24 +08:00
{
2021-12-28 16:06:23 +08:00
point3 = info.contact;
friction = info.fraction;
2013-10-22 18:00:24 +08:00
}
2021-12-28 16:06:23 +08:00
return true;
};
_physicsWorld->rayCast(func, point1, point2, nullptr);
_node->drawSegment(point1, point3, 1, STATIC_COLOR);
if (point2 != point3)
2013-10-22 18:00:24 +08:00
{
2021-12-28 16:06:23 +08:00
_node->drawDot(point3, 2, Color4F(1.0f, 1.0f, 1.0f, 1.0f));
}
addChild(_node);
break;
}
case 2:
{
# define MAX_MULTI_RAYCAST_NUM 5
Vec2 points[MAX_MULTI_RAYCAST_NUM];
int num = 0;
PhysicsRayCastCallbackFunc func = [&points, &num](PhysicsWorld& /*world*/, const PhysicsRayCastInfo& info,
void* /*data*/) -> bool {
if (num < MAX_MULTI_RAYCAST_NUM)
2013-10-22 18:00:24 +08:00
{
2021-12-28 16:06:23 +08:00
points[num++] = info.contact;
2013-10-22 18:00:24 +08:00
}
2021-12-28 16:06:23 +08:00
return true;
};
_physicsWorld->rayCast(func, point1, point2, nullptr);
_node->drawSegment(point1, point2, 1, STATIC_COLOR);
for (int i = 0; i < num; ++i)
{
_node->drawDot(points[i], 2, Color4F(1.0f, 1.0f, 1.0f, 1.0f));
2013-10-22 18:00:24 +08:00
}
2021-12-28 16:06:23 +08:00
addChild(_node);
break;
2013-10-22 18:00:24 +08:00
}
2021-12-28 16:06:23 +08:00
default:
break;
}
_angle += 0.25f * (float)M_PI / 180.0f;
2013-10-22 18:00:24 +08:00
}
void PhysicsDemoRayCast::onTouchesEnded(const std::vector<Touch*>& touches, Event* /*event*/)
2013-10-22 18:00:24 +08:00
{
2021-12-28 16:06:23 +08:00
// Add a new body/atlas sprite at the touched location
for (auto& touch : touches)
2013-10-22 18:00:24 +08:00
{
auto location = touch->getLocation();
2021-12-28 16:06:23 +08:00
2013-10-22 18:00:24 +08:00
float r = CCRANDOM_0_1();
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
if (r < 1.0f / 3.0f)
2013-10-22 18:00:24 +08:00
{
2015-09-08 09:54:01 +08:00
addChild(makeBall(location, 5 + CCRANDOM_0_1() * 10));
}
else if (r < 2.0f / 3.0f)
2013-10-22 18:00:24 +08:00
{
2015-09-08 09:54:01 +08:00
addChild(makeBox(location, Size(10 + CCRANDOM_0_1() * 15, 10 + CCRANDOM_0_1() * 15)));
}
else
2013-10-22 18:00:24 +08:00
{
2015-09-08 09:54:01 +08:00
addChild(makeTriangle(location, Size(10 + CCRANDOM_0_1() * 20, 10 + CCRANDOM_0_1() * 20)));
2013-10-22 18:00:24 +08:00
}
}
}
std::string PhysicsDemoRayCast::title() const
2013-10-22 18:00:24 +08:00
{
return "Ray Cast";
}
2015-09-08 09:54:01 +08:00
void PhysicsDemoActions::onEnter()
{
PhysicsDemo::onEnter();
_physicsWorld->setGravity(Vec2::ZERO);
2021-12-28 16:06:23 +08:00
auto touchListener = EventListenerTouchOneByOne::create();
2015-09-08 09:54:01 +08:00
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoActions::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoActions::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoActions::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
auto node = Node::create();
node->addComponent(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size));
2015-09-08 09:54:01 +08:00
node->setPosition(VisibleRect::center());
this->addChild(node);
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
Sprite* sp1 = addGrossiniAtPosition(VisibleRect::center());
Sprite* sp2 = addGrossiniAtPosition(VisibleRect::left() + Vec2(50.0f, 0.0f));
Sprite* sp3 = addGrossiniAtPosition(VisibleRect::right() - Vec2(20.0f, 0.0f));
Sprite* sp4 = addGrossiniAtPosition(VisibleRect::leftTop() + Vec2(50.0f, -50.0f));
sp4->getPhysicsBody()->setGravityEnable(false);
2021-12-28 16:06:23 +08:00
sp1->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
sp3->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
sp4->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
2021-12-28 16:06:23 +08:00
auto actionTo = JumpTo::create(2, Vec2(100, 100), 50, 4);
auto actionBy = JumpBy::create(2, Vec2(300, 0), 50, 4);
auto actionUp = JumpBy::create(2, Vec2(0, 50), 80, 4);
2015-09-08 09:54:01 +08:00
auto actionByBack = actionBy->reverse();
2021-12-28 16:06:23 +08:00
auto rotateBy = RotateBy::create(2, 180);
2015-09-08 09:54:01 +08:00
auto rotateByBack = RotateBy::create(2, -180);
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
sp1->runAction(RepeatForever::create(actionUp));
sp2->runAction(RepeatForever::create(Sequence::create(actionBy, actionByBack, nullptr)));
sp3->runAction(actionTo);
sp4->runAction(RepeatForever::create(Sequence::create(rotateBy, rotateByBack, nullptr)));
}
std::string PhysicsDemoActions::title() const
{
return "Actions";
}
// implementation of PhysicsDemoJoints
void PhysicsDemoJoints::onEnter()
{
PhysicsDemo::onEnter();
toggleDebug();
2021-12-28 16:06:23 +08:00
auto listener = EventListenerTouchOneByOne::create();
2015-09-08 09:54:01 +08:00
listener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
2021-12-28 16:06:23 +08:00
float width = (VisibleRect::getVisibleRect().size.width - 10) / 4;
float height = (VisibleRect::getVisibleRect().size.height - 50) / 4;
2021-12-28 16:06:23 +08:00
Node* node = Node::create();
PhysicsBody* box = PhysicsBody::create();
node->addComponent(box);
2021-12-28 16:06:23 +08:00
box->setDynamic(false);
2014-05-02 07:55:28 +08:00
node->setPosition(Point::ZERO);
this->addChild(node);
2021-12-28 16:06:23 +08:00
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
2021-12-28 16:06:23 +08:00
Vec2 offset(VisibleRect::leftBottom().x + 5 + j * width + width / 2,
VisibleRect::leftBottom().y + 50 + i * height + height / 2);
box->addShape(PhysicsShapeEdgeBox::create(Size(width, height), PHYSICSSHAPE_MATERIAL_DEFAULT, 1, offset));
2021-12-28 16:06:23 +08:00
switch (i * 4 + j)
{
2021-12-28 16:06:23 +08:00
case 0:
{
auto sp1 = makeBall(offset - Vec2(30, 0), 10);
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBall(offset + Vec2(30, 0), 10);
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
PhysicsJointPin* joint = PhysicsJointPin::construct(sp1PhysicsBody, sp2PhysicsBody, offset);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 1:
{
auto sp1 = makeBall(offset - Vec2(30.0f, 0.0f), 10);
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
PhysicsJointFixed* joint = PhysicsJointFixed::construct(sp1PhysicsBody, sp2PhysicsBody, offset);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 2:
{
auto sp1 = makeBall(offset - Vec2(30.0f, 0.0f), 10);
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
PhysicsJointDistance* joint =
PhysicsJointDistance::construct(sp1PhysicsBody, sp2PhysicsBody, Point::ZERO, Point::ZERO);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 3:
{
auto sp1 = makeBall(offset - Vec2(30.0f, 0.0f), 10);
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
PhysicsJointLimit* joint = PhysicsJointLimit::construct(sp1PhysicsBody, sp2PhysicsBody, Point::ZERO,
Point::ZERO, 30.0f, 60.0f);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 4:
{
auto sp1 = makeBall(offset - Vec2(30.0f, 0.0f), 10);
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
PhysicsJointSpring* joint = PhysicsJointSpring::construct(sp1PhysicsBody, sp2PhysicsBody, Point::ZERO,
Point::ZERO, 500.0f, 0.3f);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 5:
{
auto sp1 = makeBall(offset - Vec2(30.0f, 0.0f), 10);
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
PhysicsJointGroove* joint = PhysicsJointGroove::construct(
sp1PhysicsBody, sp2PhysicsBody, Vec2(30.0f, 15.0f), Vec2(30.0f, -15.0f), Vec2(-30.0f, 0.0f));
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 6:
{
auto sp1 = makeBox(offset - Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1PhysicsBody, box, sp1->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2PhysicsBody, box, sp2->getPosition()));
PhysicsJointRotarySpring* joint =
PhysicsJointRotarySpring::construct(sp1PhysicsBody, sp2PhysicsBody, 3000.0f, 60.0f);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 7:
{
auto sp1 = makeBox(offset - Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1PhysicsBody, box, sp1->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2PhysicsBody, box, sp2->getPosition()));
PhysicsJointRotaryLimit* joint =
PhysicsJointRotaryLimit::construct(sp1PhysicsBody, sp2PhysicsBody, 0.0f, (float)M_PI_2);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 8:
{
auto sp1 = makeBox(offset - Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1PhysicsBody, box, sp1->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2PhysicsBody, box, sp2->getPosition()));
PhysicsJointRatchet* joint =
PhysicsJointRatchet::construct(sp1PhysicsBody, sp2PhysicsBody, 0.0f, (float)M_PI_2);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 9:
{
auto sp1 = makeBox(offset - Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1PhysicsBody, box, sp1->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2PhysicsBody, box, sp2->getPosition()));
PhysicsJointGear* joint = PhysicsJointGear::construct(sp1PhysicsBody, sp2PhysicsBody, 0.0f, 2.0f);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
case 10:
{
auto sp1 = makeBox(offset - Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp1PhysicsBody = sp1->getPhysicsBody();
sp1PhysicsBody->setTag(DRAG_BODYS_TAG);
auto sp2 = makeBox(offset + Vec2(30.0f, 0.0f), Size(30.0f, 10.0f));
auto sp2PhysicsBody = sp2->getPhysicsBody();
sp2PhysicsBody->setTag(DRAG_BODYS_TAG);
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1PhysicsBody, box, sp1->getPosition()));
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2PhysicsBody, box, sp2->getPosition()));
PhysicsJointMotor* joint = PhysicsJointMotor::construct(sp1PhysicsBody, sp2PhysicsBody, (float)M_PI_2);
getPhysicsWorld()->addJoint(joint);
this->addChild(sp1);
this->addChild(sp2);
break;
}
default:
break;
}
}
}
}
std::string PhysicsDemoJoints::title() const
{
return "Joints";
}
void PhysicsDemoPump::onEnter()
{
PhysicsDemo::onEnter();
toggleDebug();
2021-12-28 16:06:23 +08:00
_distance = 0.0f;
_rotationV = 0.0f;
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoPump::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoPump::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoPump::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
scheduleUpdate();
2021-12-28 16:06:23 +08:00
auto node = Node::create();
auto nodeBody = PhysicsBody::create();
node->addComponent(nodeBody);
nodeBody->setDynamic(false);
2021-12-28 16:06:23 +08:00
PhysicsMaterial staticMaterial(PHYSICS_INFINITY, 0, 0.5f);
2021-12-28 16:06:23 +08:00
nodeBody->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(50, 0),
VisibleRect::leftTop() + Vec2(50, -130), staticMaterial, 2.0f));
nodeBody->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(190, 0),
VisibleRect::leftTop() + Vec2(100, -50), staticMaterial, 2.0f));
nodeBody->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(100, -50),
VisibleRect::leftTop() + Vec2(100, -90), staticMaterial, 2.0f));
nodeBody->addShape(PhysicsShapeEdgeSegment::create(VisibleRect::leftTop() + Vec2(50, -130),
VisibleRect::leftTop() + Vec2(100, -145), staticMaterial, 2.0f));
nodeBody->addShape(PhysicsShapeEdgeSegment::create(
VisibleRect::leftTop() + Vec2(100, -145), VisibleRect::leftBottom() + Vec2(100, 80), staticMaterial, 2.0f));
nodeBody->addShape(PhysicsShapeEdgeSegment::create(
VisibleRect::leftTop() + Vec2(150, -80), VisibleRect::leftBottom() + Vec2(150, 80), staticMaterial, 2.0f));
nodeBody->addShape(PhysicsShapeEdgeSegment::create(
VisibleRect::leftTop() + Vec2(150, -80), VisibleRect::rightTop() + Vec2(-100, -150), staticMaterial, 2.0f));
nodeBody->setCategoryBitmask(0x01);
2015-01-07 17:08:04 +08:00
node->setPosition(Vec2::ZERO);
this->addChild(node);
2021-12-28 16:06:23 +08:00
// balls
for (int i = 0; i < 6; ++i)
{
2021-12-28 16:06:23 +08:00
auto ball = makeBall(VisibleRect::leftTop() + Vec2(75 + CCRANDOM_0_1() * 90, 0.0f), 22,
PhysicsMaterial(0.05f, 0.0f, 0.1f));
ball->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
addChild(ball);
}
2021-12-28 16:06:23 +08:00
Vec2 vec[4] = {VisibleRect::leftTop() + Vec2(102, -148), VisibleRect::leftTop() + Vec2(148, -161),
VisibleRect::leftBottom() + Vec2(148, 20), VisibleRect::leftBottom() + Vec2(102, 20)};
2015-01-07 17:08:04 +08:00
// pump
2021-12-28 16:06:23 +08:00
auto pump = Node::create();
auto center = PhysicsShape::getPolygonCenter(vec, 4);
2015-01-07 17:08:04 +08:00
pump->setPosition(center);
auto pumpBody = PhysicsBody::createPolygon(vec, 4, PHYSICSBODY_MATERIAL_DEFAULT, -center);
pump->addComponent(pumpBody);
2015-01-07 17:08:04 +08:00
this->addChild(pump);
pumpBody->setCategoryBitmask(0x02);
pumpBody->setGravityEnable(false);
2021-12-28 16:06:23 +08:00
// small gear
auto sgearBody = PhysicsBody::createCircle(44);
sgearBody->setCategoryBitmask(0x04);
sgearBody->setCollisionBitmask(0x04);
sgearBody->setTag(1);
auto sgear = Node::create();
sgear->addComponent(sgearBody);
sgear->setPosition(VisibleRect::leftBottom() + Vec2(125.0f, 0.0f));
this->addChild(sgear);
2021-12-28 16:06:23 +08:00
_physicsWorld->addJoint(PhysicsJointPin::construct(nodeBody, sgearBody, sgear->getPosition()));
_physicsWorld->addJoint(PhysicsJointDistance::construct(pumpBody, sgearBody, Vec2(0.0f, 0.0f), Vec2(0.0f, -44.0f)));
2021-12-28 16:06:23 +08:00
// big gear
auto bgearBody = PhysicsBody::createCircle(100);
bgearBody->setCategoryBitmask(0x04);
auto bgear = Node::create();
bgear->addComponent(bgearBody);
bgear->setPosition(VisibleRect::leftBottom() + Vec2(275.0f, 0.0f));
this->addChild(bgear);
2021-12-28 16:06:23 +08:00
_physicsWorld->addJoint(PhysicsJointPin::construct(bgearBody, nodeBody, bgear->getPosition()));
_physicsWorld->addJoint(PhysicsJointGear::construct(sgearBody, bgearBody, (float)-M_PI_2, -2.0f));
2021-12-28 16:06:23 +08:00
// plugger
2021-12-28 16:06:23 +08:00
Vec2 seg[] = {VisibleRect::leftTop() + Vec2(75, -120), VisibleRect::leftBottom() + Vec2(75, -100)};
2015-09-08 09:54:01 +08:00
Vec2 segCenter = (seg[1] + seg[0]) / 2;
seg[1] -= segCenter;
seg[0] -= segCenter;
2021-12-28 16:06:23 +08:00
auto pluggerBody = PhysicsBody::createEdgeSegment(seg[0], seg[1], PhysicsMaterial(0.01f, 0.0f, 0.5f), 20);
pluggerBody->setDynamic(true);
pluggerBody->setMass(30);
pluggerBody->setMoment(100000);
pluggerBody->setCategoryBitmask(0x02);
auto plugger = Node::create();
plugger->addComponent(pluggerBody);
plugger->setPosition(segCenter);
this->addChild(plugger);
2021-12-28 16:06:23 +08:00
sgearBody->setCollisionBitmask(0x04 | 0x01);
2021-12-28 16:06:23 +08:00
_physicsWorld->addJoint(
PhysicsJointPin::construct(nodeBody, pluggerBody, VisibleRect::leftBottom() + Vec2(75.0f, -90.0f)));
_physicsWorld->addJoint(PhysicsJointDistance::construct(pluggerBody, sgearBody, Vec2::ZERO, Vec2(44.0f, 0.0f)));
}
void PhysicsDemoPump::update(float delta)
{
2015-09-08 09:54:01 +08:00
for (const auto& body : _physicsWorld->getAllBodies())
{
if (body->getTag() == DRAG_BODYS_TAG && body->getPosition().y < 0.0f)
{
2021-12-28 16:06:23 +08:00
if (body->getNode() != nullptr)
2015-09-08 09:54:01 +08:00
{
body->getNode()->setPosition(VisibleRect::leftTop() + Vec2(75 + CCRANDOM_0_1() * 90, 0.0f));
2015-09-08 09:54:01 +08:00
}
2021-12-28 16:06:23 +08:00
body->setVelocity(Vec2(0.0f, 0.0f));
}
}
2021-12-28 16:06:23 +08:00
2015-09-08 09:54:01 +08:00
PhysicsBody* gear = _physicsWorld->getBody(1);
2021-12-28 16:06:23 +08:00
if (gear != nullptr)
{
if (_distance != 0.0f)
{
2015-09-08 09:54:01 +08:00
_rotationV += _distance / 2500.0f;
2021-12-28 16:06:23 +08:00
if (_rotationV > 30)
_rotationV = 30.0f;
if (_rotationV < -30)
_rotationV = -30.0f;
}
2021-12-28 16:06:23 +08:00
gear->setAngularVelocity(_rotationV);
2013-11-01 16:26:03 +08:00
_rotationV *= 0.995f;
}
}
bool PhysicsDemoPump::onTouchBegan(Touch* touch, Event* event)
{
PhysicsDemo::onTouchBegan(touch, event);
2021-12-28 16:06:23 +08:00
_distance = touch->getLocation().x - VisibleRect::center().x;
2021-12-28 16:06:23 +08:00
return true;
}
void PhysicsDemoPump::onTouchMoved(Touch* touch, Event* event)
{
PhysicsDemo::onTouchMoved(touch, event);
2021-12-28 16:06:23 +08:00
_distance = touch->getLocation().x - VisibleRect::center().x;
}
void PhysicsDemoPump::onTouchEnded(Touch* touch, Event* event)
{
PhysicsDemo::onTouchEnded(touch, event);
2021-12-28 16:06:23 +08:00
_distance = 0;
}
std::string PhysicsDemoPump::title() const
{
return "Pump";
2013-10-30 15:53:58 +08:00
}
std::string PhysicsDemoPump::subtitle() const
{
return "touch screen on left or right";
}
2013-10-30 15:53:58 +08:00
void PhysicsDemoOneWayPlatform::onEnter()
{
PhysicsDemo::onEnter();
2021-12-28 16:06:23 +08:00
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoOneWayPlatform::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemoOneWayPlatform::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoOneWayPlatform::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
2021-12-28 16:06:23 +08:00
2013-10-30 15:53:58 +08:00
auto ground = Node::create();
ground->addComponent(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0.0f, 50.0f),
2021-12-28 16:06:23 +08:00
VisibleRect::rightBottom() + Vec2(0.0f, 50.0f)));
2013-10-30 15:53:58 +08:00
this->addChild(ground);
2021-12-28 16:06:23 +08:00
auto platform = makeBox(VisibleRect::center(), Size(200.0f, 50.0f));
auto platformBody = platform->getPhysicsBody();
platformBody->setDynamic(false);
platformBody->setContactTestBitmask(0xFFFFFFFF);
2013-10-30 15:53:58 +08:00
this->addChild(platform);
2021-12-28 16:06:23 +08:00
auto ball = makeBall(VisibleRect::center() - Vec2(0.0f, 50.0f), 20);
auto ballBody = ball->getPhysicsBody();
ballBody->setVelocity(Vec2(0.0f, 150.0f));
ballBody->setTag(DRAG_BODYS_TAG);
ballBody->setMass(1.0f);
ballBody->setContactTestBitmask(0xFFFFFFFF);
this->addChild(ball);
2021-12-28 16:06:23 +08:00
auto contactListener = EventListenerPhysicsContactWithBodies::create(platformBody, ballBody);
2014-01-28 14:59:34 +08:00
contactListener->onContactBegin = CC_CALLBACK_1(PhysicsDemoOneWayPlatform::onContactBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
}
2014-01-28 14:59:34 +08:00
bool PhysicsDemoOneWayPlatform::onContactBegin(PhysicsContact& contact)
{
return contact.getContactData()->normal.y < 0;
2013-10-30 15:53:58 +08:00
}
std::string PhysicsDemoOneWayPlatform::title() const
2013-10-30 15:53:58 +08:00
{
return "One Way Platform";
}
void PhysicsDemoSlice::onEnter()
{
PhysicsDemo::onEnter();
toggleDebug();
2021-12-28 16:06:23 +08:00
_sliceTag = 1;
2021-12-28 16:06:23 +08:00
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = [](Touch* /*touch*/, Event* /*event*/) -> bool { return true; };
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoSlice::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
2021-12-28 16:06:23 +08:00
auto ground = Node::create();
2021-12-28 16:06:23 +08:00
ground->addComponent(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vec2(0, 50),
VisibleRect::rightBottom() + Vec2(0.0f, 50.0f)));
this->addChild(ground);
2021-12-28 16:06:23 +08:00
auto box = Node::create();
Vec2 points[4] = {Vec2(-100.0f, -100.0f), Vec2(-100.0f, 100.0f), Vec2(100.0f, 100.0f), Vec2(100.0f, -100.0f)};
box->addComponent(PhysicsBody::createPolygon(points, 4));
box->setPosition(VisibleRect::center());
box->getPhysicsBody()->setTag(_sliceTag);
addChild(box);
}
2021-12-28 16:06:23 +08:00
bool PhysicsDemoSlice::slice(PhysicsWorld& /*world*/, const PhysicsRayCastInfo& info, void* /*data*/)
{
if (info.shape->getBody()->getTag() != _sliceTag)
{
return true;
}
2021-12-28 16:06:23 +08:00
if (!info.shape->containsPoint(info.start) && !info.shape->containsPoint(info.end))
{
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 normal = info.end - info.start;
2021-12-28 16:06:23 +08:00
normal = normal.getPerp().getNormalized();
float dist = info.start.dot(normal);
clipPoly(dynamic_cast<PhysicsShapePolygon*>(info.shape), normal, dist);
clipPoly(dynamic_cast<PhysicsShapePolygon*>(info.shape), -normal, -dist);
2021-12-28 16:06:23 +08:00
info.shape->getBody()->removeFromWorld();
}
2021-12-28 16:06:23 +08:00
return true;
}
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
void PhysicsDemoSlice::clipPoly(PhysicsShapePolygon* shape, Vec2 normal, float distance)
{
PhysicsBody* body = shape->getBody();
2021-12-28 16:06:23 +08:00
int count = shape->getPointsCount();
int pointsCount = 0;
Vec2* points = new Vec2[count + 1];
for (int i = 0, j = count - 1; i < count; j = i, ++i)
{
2021-12-28 16:06:23 +08:00
Vec2 a = body->local2World(shape->getPoint(j));
float aDist = a.dot(normal) - distance;
2021-12-28 16:06:23 +08:00
if (aDist < 0.0f)
{
points[pointsCount] = a;
++pointsCount;
}
2021-12-28 16:06:23 +08:00
Vec2 b = body->local2World(shape->getPoint(i));
float bDist = b.dot(normal) - distance;
2021-12-28 16:06:23 +08:00
if (aDist * bDist < 0.0f)
{
2021-12-28 16:06:23 +08:00
float t = std::fabs(aDist) / (std::fabs(aDist) + std::fabs(bDist));
points[pointsCount] = a.lerp(b, t);
++pointsCount;
}
}
2021-12-28 16:06:23 +08:00
Vec2 center = PhysicsShape::getPolygonCenter(points, pointsCount);
Node* node = Node::create();
PhysicsBody* polygon = PhysicsBody::createPolygon(points, pointsCount, PHYSICSBODY_MATERIAL_DEFAULT, -center);
node->setPosition(center);
node->addComponent(polygon);
polygon->setVelocity(body->getVelocityAtWorldPoint(center));
polygon->setAngularVelocity(body->getAngularVelocity());
polygon->setTag(_sliceTag);
addChild(node);
2021-12-28 16:06:23 +08:00
2013-11-05 20:02:58 +08:00
delete[] points;
}
2021-12-28 16:06:23 +08:00
void PhysicsDemoSlice::onTouchEnded(Touch* touch, Event* /*event*/)
{
auto func = CC_CALLBACK_3(PhysicsDemoSlice::slice, this);
getPhysicsWorld()->rayCast(func, touch->getStartLocation(), touch->getLocation(), nullptr);
}
std::string PhysicsDemoSlice::title() const
{
return "Slice";
}
std::string PhysicsDemoSlice::subtitle() const
{
return "click and drag to slice up the block";
}
2014-02-12 15:57:02 +08:00
void PhysicsDemoBug3988::onEnter()
{
PhysicsDemo::onEnter();
toggleDebug();
_physicsWorld->setGravity(Vec2::ZERO);
2021-12-28 16:06:23 +08:00
auto ball = Sprite::create("Images/YellowSquare.png");
ball->setPosition(VisibleRect::center() - Vec2(100.0f, 0.0f));
2015-09-08 09:54:01 +08:00
ball->setRotation(30.0f);
this->addChild(ball);
2021-12-28 16:06:23 +08:00
auto physicsBall = makeBox(VisibleRect::center() + Vec2(100.0f, 0.0f), Size(100.0f, 100.0f));
2015-09-08 09:54:01 +08:00
physicsBall->setRotation(30.0f);
this->addChild(physicsBall);
2014-02-12 15:57:02 +08:00
}
std::string PhysicsDemoBug3988::title() const
{
return "Bug3988";
}
std::string PhysicsDemoBug3988::subtitle() const
{
return "All the Rectangles should have same rotation angle";
}
2014-02-25 15:28:13 +08:00
void PhysicsContactTest::onEnter()
{
PhysicsDemo::onEnter();
_physicsWorld->setGravity(Vec2::ZERO);
auto s = VisibleRect::getVisibleRect().size;
2021-12-28 16:06:23 +08:00
_yellowBoxNum = 50;
_blueBoxNum = 50;
_yellowTriangleNum = 50;
2021-12-28 16:06:23 +08:00
_blueTriangleNum = 50;
MenuItemFont::setFontSize(65);
auto decrease1 = MenuItemFont::create(" - ", CC_CALLBACK_1(PhysicsContactTest::onDecrease, this));
2015-09-08 09:54:01 +08:00
decrease1->setColor(Color3B(0, 200, 20));
auto increase1 = MenuItemFont::create(" + ", CC_CALLBACK_1(PhysicsContactTest::onIncrease, this));
2015-09-08 09:54:01 +08:00
increase1->setColor(Color3B(0, 200, 20));
decrease1->setTag(1);
increase1->setTag(1);
2021-12-28 16:06:23 +08:00
float prevMenuPos = getSubtitleLable()->getPosition().y - getSubtitleLable()->getContentSize().height;
2021-12-28 16:06:23 +08:00
float menuStep = (getSubtitleLable()->getPosition().y - getRestartTestItem()->getPosition().y) * 0.25f;
auto menu1 = Menu::create(decrease1, increase1, nullptr);
menu1->alignItemsHorizontally();
menu1->setPosition(Vec2(s.width / 2, prevMenuPos));
addChild(menu1, 1);
2021-12-28 16:06:23 +08:00
auto label = Label::createWithTTF("yellow box", "fonts/arial.ttf", 32);
addChild(label, 1);
label->setPosition(Vec2(s.width / 2 - 150, prevMenuPos));
2021-12-28 16:06:23 +08:00
auto decrease2 = MenuItemFont::create(" - ", CC_CALLBACK_1(PhysicsContactTest::onDecrease, this));
2015-09-08 09:54:01 +08:00
decrease2->setColor(Color3B(0, 200, 20));
auto increase2 = MenuItemFont::create(" + ", CC_CALLBACK_1(PhysicsContactTest::onIncrease, this));
2015-09-08 09:54:01 +08:00
increase2->setColor(Color3B(0, 200, 20));
decrease2->setTag(2);
increase2->setTag(2);
2021-12-28 16:06:23 +08:00
2014-08-12 11:29:37 +08:00
auto menu2 = Menu::create(decrease2, increase2, nullptr);
menu2->alignItemsHorizontally();
menu2->setPosition(Vec2(s.width / 2, prevMenuPos -= menuStep));
addChild(menu2, 1);
2021-12-28 16:06:23 +08:00
label = Label::createWithTTF("blue box", "fonts/arial.ttf", 32);
addChild(label, 1);
label->setPosition(Vec2(s.width / 2 - 150, prevMenuPos));
auto decrease3 = MenuItemFont::create(" - ", CC_CALLBACK_1(PhysicsContactTest::onDecrease, this));
2015-09-08 09:54:01 +08:00
decrease3->setColor(Color3B(0, 200, 20));
auto increase3 = MenuItemFont::create(" + ", CC_CALLBACK_1(PhysicsContactTest::onIncrease, this));
2015-09-08 09:54:01 +08:00
increase3->setColor(Color3B(0, 200, 20));
decrease3->setTag(3);
increase3->setTag(3);
2021-12-28 16:06:23 +08:00
2014-08-12 11:29:37 +08:00
auto menu3 = Menu::create(decrease3, increase3, nullptr);
menu3->alignItemsHorizontally();
menu3->setPosition(Vec2(s.width / 2, prevMenuPos -= menuStep));
addChild(menu3, 1);
2021-12-28 16:06:23 +08:00
label = Label::createWithTTF("yellow triangle", "fonts/arial.ttf", 32);
addChild(label, 1);
label->setPosition(Vec2(s.width / 2 - 150, prevMenuPos));
auto decrease4 = MenuItemFont::create(" - ", CC_CALLBACK_1(PhysicsContactTest::onDecrease, this));
2015-09-08 09:54:01 +08:00
decrease4->setColor(Color3B(0, 200, 20));
auto increase4 = MenuItemFont::create(" + ", CC_CALLBACK_1(PhysicsContactTest::onIncrease, this));
2015-09-08 09:54:01 +08:00
increase4->setColor(Color3B(0, 200, 20));
decrease4->setTag(4);
increase4->setTag(4);
2021-12-28 16:06:23 +08:00
2014-08-12 11:29:37 +08:00
auto menu4 = Menu::create(decrease4, increase4, nullptr);
menu4->alignItemsHorizontally();
menu4->setPosition(Vec2(s.width / 2, prevMenuPos -= menuStep));
addChild(menu4, 1);
2021-12-28 16:06:23 +08:00
label = Label::createWithTTF("blue triangle", "fonts/arial.ttf", 32);
addChild(label, 1);
label->setPosition(Vec2(s.width / 2 - 150, prevMenuPos));
resetTest();
}
void PhysicsContactTest::onDecrease(Ref* sender)
{
switch (dynamic_cast<Node*>(sender)->getTag())
{
2021-12-28 16:06:23 +08:00
case 1:
if (_yellowBoxNum > 0)
_yellowBoxNum -= 50;
break;
case 2:
if (_blueBoxNum > 0)
_blueBoxNum -= 50;
break;
case 3:
if (_yellowTriangleNum > 0)
_yellowTriangleNum -= 50;
break;
case 4:
if (_blueTriangleNum > 0)
_blueTriangleNum -= 50;
break;
default:
break;
}
2021-12-28 16:06:23 +08:00
resetTest();
}
void PhysicsContactTest::onIncrease(Ref* sender)
{
switch (dynamic_cast<Node*>(sender)->getTag())
{
2021-12-28 16:06:23 +08:00
case 1:
_yellowBoxNum += 50;
break;
case 2:
_blueBoxNum += 50;
break;
case 3:
_yellowTriangleNum += 50;
break;
case 4:
_blueTriangleNum += 50;
break;
default:
break;
}
2021-12-28 16:06:23 +08:00
resetTest();
}
void PhysicsContactTest::resetTest()
{
removeChildByTag(10);
auto root = Node::create();
root->setTag(10);
this->addChild(root);
2021-12-28 16:06:23 +08:00
auto s = VisibleRect::getVisibleRect().size;
float prevMenuPos = getSubtitleLable()->getPosition().y - getSubtitleLable()->getContentSize().height;
2021-12-28 16:06:23 +08:00
float menuStep = (getSubtitleLable()->getPosition().y - getRestartTestItem()->getPosition().y) * 0.25f;
std::string strNum;
char buffer[10];
2021-12-28 16:06:23 +08:00
sprintf(buffer, "%d", _yellowBoxNum);
auto label = Label::createWithTTF(buffer, "fonts/arial.ttf", 32);
root->addChild(label, 1);
label->setPosition(Vec2(s.width / 2, prevMenuPos));
2021-12-28 16:06:23 +08:00
sprintf(buffer, "%d", _blueBoxNum);
label = Label::createWithTTF(buffer, "fonts/arial.ttf", 32);
root->addChild(label, 1);
2021-12-28 16:06:23 +08:00
label->setPosition(Vec2(s.width / 2, prevMenuPos -= menuStep));
sprintf(buffer, "%d", _yellowTriangleNum);
label = Label::createWithTTF(buffer, "fonts/arial.ttf", 32);
root->addChild(label, 1);
2021-12-28 16:06:23 +08:00
label->setPosition(Vec2(s.width / 2, prevMenuPos -= menuStep));
sprintf(buffer, "%d", _blueTriangleNum);
label = Label::createWithTTF(buffer, "fonts/arial.ttf", 32);
root->addChild(label, 1);
2021-12-28 16:06:23 +08:00
label->setPosition(Vec2(s.width / 2, prevMenuPos -= menuStep));
2014-02-25 15:28:13 +08:00
auto wall = Node::create();
wall->addComponent(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1, 0.0f)));
2014-02-25 15:28:13 +08:00
wall->setPosition(VisibleRect::center());
root->addChild(wall);
2021-12-28 16:06:23 +08:00
auto contactListener = EventListenerPhysicsContact::create();
2014-02-25 15:28:13 +08:00
contactListener->onContactBegin = CC_CALLBACK_1(PhysicsContactTest::onContactBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
2021-12-28 16:06:23 +08:00
2014-02-25 15:28:13 +08:00
// yellow box, will collide with itself and blue box.
for (int i = 0; i < _yellowBoxNum; ++i)
2014-02-25 15:28:13 +08:00
{
2015-09-08 09:54:01 +08:00
Size size(10 + CCRANDOM_0_1() * 10, 10 + CCRANDOM_0_1() * 10);
2021-12-28 16:06:23 +08:00
Size winSize = VisibleRect::getVisibleRect().size;
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 position = Vec2(winSize.width, winSize.height) - Vec2(size.width, size.height);
2021-12-28 16:06:23 +08:00
position.x = position.x * CCRANDOM_0_1();
position.y = position.y * CCRANDOM_0_1();
position = VisibleRect::leftBottom() + position + Vec2(size.width / 2, size.height / 2);
Vec2 velocity((float)(CCRANDOM_0_1() - 0.5) * 200, (float)(CCRANDOM_0_1() - 0.5) * 200);
2021-12-28 16:06:23 +08:00
auto box = makeBox(position, size, 1, PhysicsMaterial(0.1f, 1, 0.0f));
auto boxBody = box->getPhysicsBody();
boxBody->setVelocity(velocity);
2021-12-28 16:06:23 +08:00
boxBody->setCategoryBitmask(0x01); // 0001
boxBody->setContactTestBitmask(0x04); // 0100
boxBody->setCollisionBitmask(0x03); // 0011
root->addChild(box);
2014-02-25 15:28:13 +08:00
}
2021-12-28 16:06:23 +08:00
2014-02-25 15:28:13 +08:00
// blue box, will collide with blue box.
for (int i = 0; i < _blueBoxNum; ++i)
2014-02-25 15:28:13 +08:00
{
2015-09-08 09:54:01 +08:00
Size size(10 + CCRANDOM_0_1() * 10, 10 + CCRANDOM_0_1() * 10);
2021-12-28 16:06:23 +08:00
Size winSize = VisibleRect::getVisibleRect().size;
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 position = Vec2(winSize.width, winSize.height) - Vec2(size.width, size.height);
2021-12-28 16:06:23 +08:00
position.x = position.x * CCRANDOM_0_1();
position.y = position.y * CCRANDOM_0_1();
position = VisibleRect::leftBottom() + position + Vec2(size.width / 2, size.height / 2);
Vec2 velocity((float)(CCRANDOM_0_1() - 0.5) * 200, (float)(CCRANDOM_0_1() - 0.5) * 200);
2021-12-28 16:06:23 +08:00
auto box = makeBox(position, size, 2, PhysicsMaterial(0.1f, 1, 0.0f));
auto boxBody = box->getPhysicsBody();
boxBody->setVelocity(velocity);
2021-12-28 16:06:23 +08:00
boxBody->setCategoryBitmask(0x02); // 0010
boxBody->setContactTestBitmask(0x08); // 1000
boxBody->setCollisionBitmask(0x01); // 0001
root->addChild(box);
2014-02-25 15:28:13 +08:00
}
2021-12-28 16:06:23 +08:00
2014-02-25 15:28:13 +08:00
// yellow triangle, will collide with itself and blue box.
for (int i = 0; i < _yellowTriangleNum; ++i)
2014-02-25 15:28:13 +08:00
{
2015-09-08 09:54:01 +08:00
Size size(10 + CCRANDOM_0_1() * 10, 10 + CCRANDOM_0_1() * 10);
2021-12-28 16:06:23 +08:00
Size winSize = VisibleRect::getVisibleRect().size;
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 position = Vec2(winSize.width, winSize.height) - Vec2(size.width, size.height);
2021-12-28 16:06:23 +08:00
position.x = position.x * CCRANDOM_0_1();
position.y = position.y * CCRANDOM_0_1();
position = VisibleRect::leftBottom() + position + Vec2(size.width / 2, size.height / 2);
Vec2 velocity((float)(CCRANDOM_0_1() - 0.5) * 300, (float)(CCRANDOM_0_1() - 0.5) * 300);
2021-12-28 16:06:23 +08:00
auto triangle = makeTriangle(position, size, 1, PhysicsMaterial(0.1f, 1, 0.0f));
auto triangleBody = triangle->getPhysicsBody();
triangleBody->setVelocity(velocity);
2021-12-28 16:06:23 +08:00
triangleBody->setCategoryBitmask(0x04); // 0100
triangleBody->setContactTestBitmask(0x01); // 0001
triangleBody->setCollisionBitmask(0x06); // 0110
root->addChild(triangle);
2014-02-25 15:28:13 +08:00
}
2021-12-28 16:06:23 +08:00
2014-02-25 15:28:13 +08:00
// blue triangle, will collide with yellow box.
for (int i = 0; i < _blueTriangleNum; ++i)
2014-02-25 15:28:13 +08:00
{
2015-09-08 09:54:01 +08:00
Size size(10 + CCRANDOM_0_1() * 10, 10 + CCRANDOM_0_1() * 10);
2021-12-28 16:06:23 +08:00
Size winSize = VisibleRect::getVisibleRect().size;
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 position = Vec2(winSize.width, winSize.height) - Vec2(size.width, size.height);
2021-12-28 16:06:23 +08:00
position.x = position.x * CCRANDOM_0_1();
position.y = position.y * CCRANDOM_0_1();
position = VisibleRect::leftBottom() + position + Vec2(size.width / 2, size.height / 2);
Vec2 velocity((float)(CCRANDOM_0_1() - 0.5) * 300, (float)(CCRANDOM_0_1() - 0.5) * 300);
2021-12-28 16:06:23 +08:00
auto triangle = makeTriangle(position, size, 2, PhysicsMaterial(0.1f, 1, 0.0f));
auto triangleBody = triangle->getPhysicsBody();
triangleBody->setVelocity(velocity);
2021-12-28 16:06:23 +08:00
triangleBody->setCategoryBitmask(0x08); // 1000
triangleBody->setContactTestBitmask(0x02); // 0010
triangleBody->setCollisionBitmask(0x01); // 0001
root->addChild(triangle);
2014-02-25 15:28:13 +08:00
}
}
bool PhysicsContactTest::onContactBegin(PhysicsContact& contact)
{
2021-12-28 16:06:23 +08:00
PhysicsBody* a = contact.getShapeA()->getBody();
PhysicsBody* b = contact.getShapeB()->getBody();
2014-02-25 15:28:13 +08:00
PhysicsBody* body = (a->getCategoryBitmask() == 0x04 || a->getCategoryBitmask() == 0x08) ? a : b;
CC_ASSERT(body->getCategoryBitmask() == 0x04 || body->getCategoryBitmask() == 0x08);
2021-12-28 16:06:23 +08:00
2014-02-25 15:28:13 +08:00
return true;
}
std::string PhysicsContactTest::title() const
{
return "Contact Test";
}
std::string PhysicsContactTest::subtitle() const
{
return "should not crash";
}
void PhysicsPositionRotationTest::onEnter()
{
PhysicsDemo::onEnter();
toggleDebug();
2015-09-08 09:54:01 +08:00
_physicsWorld->setGravity(Point::ZERO);
2021-12-28 16:06:23 +08:00
auto touchListener = EventListenerTouchOneByOne::create();
2014-05-02 07:55:28 +08:00
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
2021-12-28 16:06:23 +08:00
2014-05-02 07:55:28 +08:00
auto wall = Node::create();
wall->addComponent(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size));
2014-05-02 07:55:28 +08:00
wall->setPosition(VisibleRect::center());
addChild(wall);
2021-12-28 16:06:23 +08:00
// anchor test
auto anchorNode = Sprite::create("Images/YellowSquare.png");
2021-12-28 16:06:23 +08:00
// anchorNode->setAnchorPoint(Vec2(0.1f, 0.9f));
2014-05-02 07:55:28 +08:00
anchorNode->setPosition(100, 100);
anchorNode->setScale(0.25);
anchorNode->addComponent(PhysicsBody::createBox(anchorNode->getContentSize()));
anchorNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
addChild(anchorNode);
2021-09-08 16:16:14 +08:00
anchorNode->getPhysicsBody()->setAngularVelocity(-5.0f);
2021-12-28 16:06:23 +08:00
// parent test
auto parent = Sprite::create("Images/YellowSquare.png");
2021-09-08 16:16:14 +08:00
parent->setPosition(300, 100);
parent->setScale(0.5);
parent->addComponent(PhysicsBody::createBox(parent->getContentSize()));
parent->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
2014-05-02 07:55:28 +08:00
addChild(parent);
2021-12-28 16:06:23 +08:00
2021-09-08 16:16:14 +08:00
auto leftBall = Sprite::create("Images/YellowSquare.png");
leftBall->setPosition(-50, 0);
leftBall->Node::setScale(0.5);
leftBall->addComponent(PhysicsBody::createBox(leftBall->getContentSize()));
leftBall->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
parent->addChild(leftBall);
2021-09-08 16:16:14 +08:00
parent->getPhysicsBody()->setAngularVelocity(5.0f);
2021-12-28 16:06:23 +08:00
// offset position rotation test
auto offsetPosNode = Sprite::create("Images/YellowSquare.png");
2014-05-02 07:55:28 +08:00
offsetPosNode->setPosition(100, 200);
auto body = PhysicsBody::createBox(offsetPosNode->getContentSize() / 2);
offsetPosNode->addComponent(body);
body->setPositionOffset(-Vec2(offsetPosNode->getContentSize() / 2));
body->setRotationOffset(45);
body->setTag(DRAG_BODYS_TAG);
2014-05-02 07:55:28 +08:00
addChild(offsetPosNode);
2021-09-08 16:16:14 +08:00
offsetPosNode->getPhysicsBody()->setAngularVelocity(5.0f);
2021-12-28 16:06:23 +08:00
return;
}
std::string PhysicsPositionRotationTest::title() const
{
return "Position/Rotation Test";
}
void PhysicsSetGravityEnableTest::onEnter()
{
PhysicsDemo::onEnter();
2021-12-28 16:06:23 +08:00
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
2021-12-28 16:06:23 +08:00
// wall
auto wall = Node::create();
2021-12-28 16:06:23 +08:00
wall->addComponent(
PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1.0f, 0.0f)));
wall->setPosition(VisibleRect::center());
addChild(wall);
2021-12-28 16:06:23 +08:00
// common box
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
auto commonBox = makeBox(Vec2(100, 100), Size(50, 50), 1);
commonBox->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
2017-09-19 09:41:44 +08:00
commonBox->getPhysicsBody()->setGravityEnable(true);
addChild(commonBox);
2021-12-28 16:06:23 +08:00
auto box = makeBox(Vec2(200, 100), Size(50, 50), 2);
auto boxBody = box->getPhysicsBody();
boxBody->setMass(20);
boxBody->setTag(DRAG_BODYS_TAG);
boxBody->setGravityEnable(false);
addChild(box);
2021-12-28 16:06:23 +08:00
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
auto ball = makeBall(Vec2(200, 200), 50);
ball->setTag(2);
auto ballBody = ball->getPhysicsBody();
ballBody->setTag(DRAG_BODYS_TAG);
ballBody->setGravityEnable(false);
ballBody->setMass(50);
addChild(ball);
2021-12-28 16:06:23 +08:00
scheduleOnce(CC_SCHEDULE_SELECTOR(PhysicsSetGravityEnableTest::onScheduleOnce), 1.0);
}
void PhysicsSetGravityEnableTest::onScheduleOnce(float /*delta*/)
{
auto ball = getChildByTag(2);
ball->getPhysicsBody()->setMass(200);
2021-12-28 16:06:23 +08:00
2017-09-19 09:41:44 +08:00
_physicsWorld->setGravity(Vec2(0, -98));
}
std::string PhysicsSetGravityEnableTest::title() const
{
return "Set Gravity Enable Test";
}
std::string PhysicsSetGravityEnableTest::subtitle() const
{
return "only yellow box drop down";
}
2015-09-08 09:54:01 +08:00
void PhysicsDemoBug5482::onEnter()
{
PhysicsDemo::onEnter();
2021-12-28 16:06:23 +08:00
toggleDebug();
2021-12-28 16:06:23 +08:00
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
2021-12-28 16:06:23 +08:00
_bodyInA = false;
2021-12-28 16:06:23 +08:00
// wall
auto wall = Node::create();
2021-12-28 16:06:23 +08:00
wall->addComponent(
PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1.0f, 0.0f)));
wall->setPosition(VisibleRect::center());
addChild(wall);
2021-12-28 16:06:23 +08:00
// button
MenuItemFont::setFontSize(18);
2015-09-08 09:54:01 +08:00
_button = MenuItemFont::create("Set Body To A", CC_CALLBACK_1(PhysicsDemoBug5482::changeBodyCallback, this));
2021-12-28 16:06:23 +08:00
auto menu = Menu::create(_button, nullptr);
this->addChild(menu);
2021-12-28 16:06:23 +08:00
_nodeA = Sprite::create("Images/YellowSquare.png");
_nodeA->setPosition(VisibleRect::center().x - 150, 100);
this->addChild(_nodeA);
2021-12-28 16:06:23 +08:00
_nodeB = Sprite::create("Images/YellowSquare.png");
_nodeB->setPosition(VisibleRect::center().x + 150, 100);
this->addChild(_nodeB);
2021-12-28 16:06:23 +08:00
_body = PhysicsBody::createBox(_nodeA->getContentSize());
_body->setTag(DRAG_BODYS_TAG);
_body->retain();
}
2015-09-08 09:54:01 +08:00
void PhysicsDemoBug5482::onExit()
{
2014-08-12 11:39:56 +08:00
PhysicsDemo::onExit();
_body->release();
}
void PhysicsDemoBug5482::changeBodyCallback(Ref* /*sender*/)
{
Sprite* node = _bodyInA ? _nodeB : _nodeA;
if (_bodyInA)
{
_button->setString("Set Body To A");
}
else
{
_button->setString("Set Body To B");
}
if (_body->getOwner())
{
_body->getOwner()->removeComponent(_body);
}
node->addComponent(_body);
_bodyInA = !_bodyInA;
}
2015-09-08 09:54:01 +08:00
std::string PhysicsDemoBug5482::title() const
{
return "bug 5482: setPhysicsBodyTest";
}
2015-09-08 09:54:01 +08:00
std::string PhysicsDemoBug5482::subtitle() const
{
return "change physics body to the other.";
}
2014-06-12 12:57:29 +08:00
void PhysicsFixedUpdate::onEnter()
{
PhysicsDemo::onEnter();
2021-12-28 16:06:23 +08:00
toggleDebug();
2015-09-08 09:54:01 +08:00
_physicsWorld->setGravity(Point::ZERO);
2021-12-28 16:06:23 +08:00
2014-06-12 12:57:29 +08:00
// wall
auto wall = Node::create();
wall->addComponent(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1, 0.0f)));
2014-06-12 12:57:29 +08:00
wall->setPosition(VisibleRect::center());
this->addChild(wall);
2021-12-28 16:06:23 +08:00
2014-06-12 12:57:29 +08:00
addBall();
2021-12-28 16:06:23 +08:00
scheduleOnce(CC_SCHEDULE_SELECTOR(PhysicsFixedUpdate::updateStart), 2);
2014-06-12 12:57:29 +08:00
}
void PhysicsFixedUpdate::addBall()
{
auto ball = Sprite::create("Images/ball.png");
ball->setPosition(100, 100);
auto ballBody = PhysicsBody::createCircle(ball->getContentSize().width / 2, PhysicsMaterial(0.1f, 1, 0.0f));
ball->addComponent(ballBody);
ballBody->setTag(DRAG_BODYS_TAG);
ballBody->setVelocity(Point(1000, 20));
2014-06-12 12:57:29 +08:00
this->addChild(ball);
}
void PhysicsFixedUpdate::updateStart(float /*delta*/)
2014-06-12 12:57:29 +08:00
{
addBall();
2021-12-28 16:06:23 +08:00
Squashed commit of the following: commit 36c2807d07848448c1fed86f4b119205048691bf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Feb 29 13:11:40 2016 -0800 binary file 82 commit 9b7cd003b12faefd0522d12f765c027efb001ab2 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Feb 29 12:56:39 2016 -0800 fore multiple in windows phone 8.1 too commit daea8a3ef8d69915e7a9d962fd745a1103011bab Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Feb 29 12:50:45 2016 -0800 force multiple commit 0fbabdbf99974e95de083d79c7d6ae7cf735e4b6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Feb 29 10:44:40 2016 -0800 compiles commit f817766afcd0ffc4ca93f628d55fabc4b5a96ba5 Merge: 7b2cea3 8bf3d33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Feb 29 09:37:24 2016 -0800 Merge branch 'v3' into chipmunk_v7_2 commit 7b2cea329a56ae2786428d39a499bc12ba3860e6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Sat Feb 27 01:11:13 2016 -0800 correct paths for win8.1 commit dcf1f046eb1e18474d6f94b2d7dc31fbf1b06cea Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Feb 26 22:46:58 2016 -0800 projects works on win32 commit ab5b14c25ef0fdf224480302f9cb3cc01d5c360c Merge: 04377cc 9af2f4e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Feb 26 21:57:10 2016 -0800 Merge branch 'v3' into chipmunk_v7_2 commit 04377cce7f46b2c2016ccc3855fc479c4f6aa516 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Feb 26 21:26:55 2016 -0800 trying to make it compile on win32 commit 7703946d99e1cdc6a57f2ccb2a868818c1367291 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Feb 24 10:28:12 2016 -0800 quick hack to make it compile... ...autogenerated files will fix it. commit ea35bf3a5894cb80b09ecd0a4a5408dcc19785f5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Feb 24 09:20:59 2016 -0800 Squashed commit of the following: commit 487e52a7af7cdb40135fd3bd83b2efba0dfe6265 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 20:10:13 2016 -0800 compiles.. sort of commit a7990e2888d5ef1e43b811148b23c0b899105ab3 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 19:55:24 2016 -0800 more chipmunk fixes commit 1174f6ec8201324161d7946af0f493b136a56690 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 19:36:15 2016 -0800 more chipmunk fixes commit 00ac2449ca0c35bd9184310d58e74aaa05bd1323 Merge: f942c5a 0594d3c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 19:29:27 2016 -0800 Merge branch 'chipmunk_v7_squashed' of github.com:ricardoquesada/cocos2d-x into chipmunk_v7_squashed commit f942c5a3cc7b24f3ac211d781be1b9c9d4ec0fd7 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 19:28:51 2016 -0800 correct path for chipmunk commit 0594d3c4141d390b2566ea76101e383f15ebcaaf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 18:44:05 2016 -0800 compiles on ios commit 379503399c384ad77d25d68a2f83b71a829b1c04 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 17:51:36 2016 -0800 chipmunk include fixes commit 95839fb7b406a32255853294b016d8c15aec1ed6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 17:09:04 2016 -0800 fixes for cmake + mac commit 39449ea3b7efccae2946785c1c11433d964ee480 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 16:51:25 2016 -0800 correct includes commit 98e97d7d2f00e6a78d040dcaf17e5a077e9a88c3 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 16:35:40 2016 -0800 fixes chipmunk with cmake commit 337f21f9466703722847635301d841ace9b757cb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 14:42:01 2016 -0800 updates android and linux projects commit 7111a9993b97f78cf73206804b25665c21a59ca8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 14:18:26 2016 -0800 cosmetic changes commit ce15038b8d2c0c7ebe31b4e1efe92646b7c70d1b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 06:53:41 2016 -0800 chipmunk autogenerated files commit 66c71bfa3aaab04abf25f284e2e76ce2a74a98de Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 06:47:41 2016 -0800 compatibility both in JS (to reduce space) and in C for (C++ users) commit 0f9ffa0a8ab23a19e1f10b04e6987c2288764b1f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 06:10:02 2016 -0800 mooooore fixes commit 9ab23014093677e8eda69b105f50410b8c792aa3 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 05:30:58 2016 -0800 doesn't crash with collision handler commit 69275a16817a038244bbaaff9e82a011b6729cb2 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Feb 23 05:16:29 2016 -0800 adds missing space functions commit a6b97bbc76cb9ab293356bb7451cfa858353f589 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Feb 22 22:22:27 2016 -0800 queries... better commit 3442bceca64f5a0ddbbbb1a288146a2a61d87632 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Feb 22 19:48:41 2016 -0800 adds more missing functions fixes other chipmunk JS issues commit 3797a033896d059da58a05ce0de832ed9c150eff Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Feb 22 18:30:21 2016 -0800 yep... poly works ok commit d1bb57776891605b9d3e4fb90736c6869d96b944 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Feb 22 17:28:29 2016 -0800 neede compat files commit 39ec518d4e089124d9a53fe3a8dce3aa0474c233 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Feb 22 17:28:04 2016 -0800 query working! commit 71a8001fd6bc03ac674621f5da38a3f8778430b4 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Feb 19 21:57:03 2016 -0800 almost there commit 9272fdcd06ba188db2fe5701fb2ac21476d54883 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Feb 19 21:18:46 2016 -0800 fixes return values commit 7218778da846e1e36a510c0a19a5ee76f91db032 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Feb 19 20:36:14 2016 -0800 cpMomentForSegment is manual since parameter 4 is optional in chipmunk 6, but not in 7 commit 98d5d3e23c2cdb941cc0b1f3ce8275990356e88d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Feb 19 17:29:28 2016 -0800 yeah... process the arguments correctly works both for functions and auto_classes commit ce6a4fe3593b4f3b0fcdf11d12e297d789e0d96b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Feb 19 16:49:00 2016 -0800 compiles and links fixes some issues with the queries. returns shapes instad of the queryInfo commit 3f879482dc6db86e45e4319bc802ccf19b52a46c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Feb 19 15:03:18 2016 -0800 adds removeCollisionHandler and other fixes commit 32d825fc7bd323e144db6a1badc4a62420b6ea44 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Feb 18 20:59:24 2016 -0800 compiles... doesn't work yet commit bd64de81713fb2d7652a538d667d73f05f5d9320 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Feb 17 18:17:39 2016 -0800 WIP: chipmunk bindings
2016-03-01 05:53:00 +08:00
_physicsWorld->setFixedUpdateRate(180);
2014-06-12 12:57:29 +08:00
}
void PhysicsFixedUpdate::update(float /*delta*/)
2014-06-12 12:57:29 +08:00
{
2021-12-28 16:06:23 +08:00
2014-06-12 12:57:29 +08:00
// use fixed time and calculate 3 times per frame makes physics simulate more precisely.
for (int i = 0; i < 3; ++i)
{
2021-12-28 16:06:23 +08:00
_physicsWorld->step(1 / 180.0f);
2014-06-12 12:57:29 +08:00
}
}
std::string PhysicsFixedUpdate::title() const
{
return "Fixed Update Test";
}
std::string PhysicsFixedUpdate::subtitle() const
{
return "The second ball should not run across the wall";
2014-06-12 12:57:29 +08:00
}
2021-12-28 16:06:23 +08:00
bool PhysicsTransformTest::onTouchBegan(Touch* touch, Event* /*event*/)
2014-06-27 17:30:50 +08:00
{
_parentSprite->setPosition(_rootLayer->convertTouchToNodeSpace(touch));
2014-06-27 17:30:50 +08:00
return false;
}
void PhysicsTransformTest::onEnter()
{
PhysicsDemo::onEnter();
toggleDebug();
2015-09-08 09:54:01 +08:00
_physicsWorld->setGravity(Point::ZERO);
2021-12-28 16:06:23 +08:00
auto touchListener = EventListenerTouchOneByOne::create();
2014-06-27 17:30:50 +08:00
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsTransformTest::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
2021-12-28 16:06:23 +08:00
_rootLayer = Layer::create();
addChild(_rootLayer);
2021-12-28 16:06:23 +08:00
2014-06-27 17:30:50 +08:00
auto wall = Node::create();
2021-12-28 16:06:23 +08:00
wall->addComponent(
PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size, PhysicsMaterial(0.1f, 1.0f, 0.0f)));
2014-06-27 17:30:50 +08:00
wall->setPosition(VisibleRect::center());
_rootLayer->addChild(wall);
2021-12-28 16:06:23 +08:00
// parent test
_parentSprite = Sprite::create("Images/YellowSquare.png");
_parentSprite->setPosition(200, 100);
_parentSprite->setScale(0.25);
2021-12-28 16:06:23 +08:00
_parentSprite->addComponent(
PhysicsBody::createBox(_parentSprite->getContentSize(), PhysicsMaterial(0.1f, 1.0f, 0.0f)));
_parentSprite->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
_parentSprite->setTag(1);
_rootLayer->addChild(_parentSprite);
2021-12-28 16:06:23 +08:00
2014-06-27 17:30:50 +08:00
auto leftBall = Sprite::create("Images/ball.png");
leftBall->setPosition(-30, 0);
2015-09-08 09:54:01 +08:00
leftBall->setScale(2);
2021-12-28 16:06:23 +08:00
leftBall->addComponent(
PhysicsBody::createCircle(leftBall->getContentSize().width / 2, PhysicsMaterial(0.1f, 1.0f, 0.0f)));
leftBall->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
_parentSprite->addChild(leftBall);
2021-12-28 16:06:23 +08:00
ScaleTo* scaleTo = ScaleTo::create(2.0, 0.5);
2014-06-27 17:30:50 +08:00
ScaleTo* scaleBack = ScaleTo::create(2.0, 1.0);
_parentSprite->runAction(RepeatForever::create(Sequence::create(scaleTo, scaleBack, nullptr)));
2021-12-28 16:06:23 +08:00
2014-06-27 17:30:50 +08:00
auto normal = Sprite::create("Images/YellowSquare.png");
normal->setPosition(300, 100);
normal->setScale(0.25, 0.5);
normal->addComponent(PhysicsBody::createBox(normal->getContentSize(), PhysicsMaterial(0.1f, 1.0f, 0.0f)));
normal->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
_rootLayer->addChild(normal);
2021-12-28 16:06:23 +08:00
2014-06-27 17:30:50 +08:00
auto bullet = Sprite::create("Images/ball.png");
bullet->setPosition(200, 200);
2021-12-28 16:06:23 +08:00
bullet->addComponent(
PhysicsBody::createCircle(bullet->getContentSize().width / 2, PhysicsMaterial(0.1f, 1.0f, 0.0f)));
bullet->getPhysicsBody()->setVelocity(Vec2(100.0f, 100.0f));
_rootLayer->addChild(bullet);
2021-12-28 16:06:23 +08:00
MoveBy* move = MoveBy::create(2.0f, Vec2(100.0f, 100.0f));
MoveBy* move2 = MoveBy::create(2.0f, Vec2(-200.0f, 0.0f));
MoveBy* move3 = MoveBy::create(2.0f, Vec2(100.0f, -100.0f));
ScaleTo* scale = ScaleTo::create(3.0f, 0.3f);
ScaleTo* scale2 = ScaleTo::create(3.0f, 1.0f);
2021-12-28 16:06:23 +08:00
RotateBy* rotate = RotateBy::create(6.0f, 360);
2021-12-28 16:06:23 +08:00
_rootLayer->runAction(RepeatForever::create(Sequence::create(move, move2, move3, nullptr)));
_rootLayer->runAction(RepeatForever::create(Sequence::create(scale, scale2, nullptr)));
_rootLayer->runAction(RepeatForever::create(rotate));
2014-06-27 17:30:50 +08:00
}
std::string PhysicsTransformTest::title() const
{
return "Physics transform test";
}
void PhysicsIssue9959::onEnter()
{
PhysicsDemo::onEnter();
2021-12-28 16:06:23 +08:00
auto origin = Director::getInstance()->getVisibleOrigin();
auto visibleSize = Director::getInstance()->getVisibleSize();
2021-12-28 16:06:23 +08:00
auto scale9Sprite1 = ui::Scale9Sprite::create("Images/ball.png");
2021-12-28 16:06:23 +08:00
scale9Sprite1->setPosition(origin + visibleSize / 2);
addChild(scale9Sprite1);
2021-12-28 16:06:23 +08:00
scale9Sprite1->runAction(RepeatForever::create(
Sequence::create(MoveBy::create(2.0f, Vec2(100.0f, 0.0f)), MoveBy::create(2.0f, Vec2(-100.0f, 0.0f)), NULL)));
auto scale9Sprite2 = ui::Scale9Sprite::create("Images/ball.png");
2021-12-28 16:06:23 +08:00
scale9Sprite2->setPosition(origin + visibleSize / 2 + Vec2(0.0f, 50.0f));
addChild(scale9Sprite2);
2021-12-28 16:06:23 +08:00
scale9Sprite2->runAction(
RepeatForever::create(Sequence::create(ScaleTo::create(2.0f, 1.5f), ScaleTo::create(2.0f, 1.0f), NULL)));
auto scale9Sprite3 = ui::Scale9Sprite::create("Images/ball.png");
2021-12-28 16:06:23 +08:00
scale9Sprite3->setPosition(origin + visibleSize / 2 + Vec2(0.0f, -50.0f));
addChild(scale9Sprite3);
scale9Sprite3->runAction(RepeatForever::create(RotateBy::create(2.0f, 360.0f)));
}
std::string PhysicsIssue9959::title() const
{
return "Reorder issue #9959";
}
std::string PhysicsIssue9959::subtitle() const
{
return "Test Scale9Sprite run scale/move/rotation action in physics scene";
}
//
void PhysicsIssue15932::onEnter()
{
PhysicsDemo::onEnter();
2021-12-28 16:06:23 +08:00
PhysicsBody* pb = PhysicsBody::createBox(Size(15, 5), PhysicsMaterial(0.1f, 0.0f, 1.0f));
this->addComponent(pb);
this->removeComponent(pb);
}
std::string PhysicsIssue15932::title() const
{
return "Github issue #15932";
}
std::string PhysicsIssue15932::subtitle() const
{
return "addComponent()/removeComponent() should not crash";
}
#endif