2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +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-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +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 "Physics3DTest.h"
|
|
|
|
|
|
|
|
#include "3d/CCTerrain.h"
|
|
|
|
#include "3d/CCBundle3D.h"
|
|
|
|
#include "physics3d/CCPhysics3D.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
USING_NS_CC;
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
IDC_NEXT = 100,
|
|
|
|
IDC_BACK,
|
|
|
|
IDC_RESTART
|
|
|
|
};
|
|
|
|
|
|
|
|
#define START_POS_X -0.5
|
|
|
|
#define START_POS_Y -2.5
|
|
|
|
#define START_POS_Z -0.5
|
|
|
|
|
|
|
|
#define ARRAY_SIZE_X 4
|
|
|
|
#define ARRAY_SIZE_Y 3
|
|
|
|
#define ARRAY_SIZE_Z 4
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static cocos2d::Scene* physicsScene = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Physics3DTests::Physics3DTests()
|
|
|
|
{
|
|
|
|
#if CC_USE_3D_PHYSICS == 0
|
|
|
|
ADD_TEST_CASE(Physics3DDemoDisabled);
|
|
|
|
#else
|
|
|
|
ADD_TEST_CASE(BasicPhysics3DDemo);
|
|
|
|
ADD_TEST_CASE(Physics3DConstraintDemo);
|
|
|
|
ADD_TEST_CASE(Physics3DKinematicDemo);
|
|
|
|
ADD_TEST_CASE(Physics3DCollisionCallbackDemo);
|
|
|
|
ADD_TEST_CASE(Physics3DColliderDemo);
|
|
|
|
ADD_TEST_CASE(Physics3DTerrainDemo);
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#if CC_USE_3D_PHYSICS == 0
|
|
|
|
void Physics3DDemoDisabled::onEnter()
|
|
|
|
{
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 16);
|
|
|
|
auto label = Label::createWithTTF(ttfConfig, "Should define CC_USE_3D_PHYSICS\n to run this test case");
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
label->setPosition(Vec2(size.width / 2, size.height / 2));
|
|
|
|
|
|
|
|
addChild(label);
|
|
|
|
|
|
|
|
TestCase::onEnter();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
std::string Physics3DTestDemo::title() const
|
|
|
|
{
|
|
|
|
return "Physics3D Test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Physics3DTestDemo::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DTestDemo::init()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!TestCase::init())
|
|
|
|
return false;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (initWithPhysics())
|
|
|
|
{
|
|
|
|
getPhysics3DWorld()->setDebugDrawEnable(false);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
physicsScene = this;
|
|
|
|
Size size = Director::getInstance()->getWinSize();
|
|
|
|
_camera = Camera::createPerspective(30.0f, size.width / size.height, 1.0f, 1000.0f);
|
|
|
|
_camera->setPosition3D(Vec3(0.0f, 50.0f, 100.0f));
|
|
|
|
_camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
this->addChild(_camera);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(Physics3DTestDemo::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(Physics3DTestDemo::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Physics3DTestDemo::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 10);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto label = Label::createWithTTF(ttfConfig, "DebugDraw OFF");
|
|
|
|
auto menuItem = MenuItemLabel::create(label, [=](Ref* /*ref*/) {
|
|
|
|
if (getPhysics3DWorld()->isDebugDrawEnabled())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
getPhysics3DWorld()->setDebugDrawEnable(false);
|
|
|
|
label->setString("DebugDraw OFF");
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
getPhysics3DWorld()->setDebugDrawEnable(true);
|
|
|
|
label->setString("DebugDraw ON");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
auto menu = Menu::create(menuItem, nullptr);
|
|
|
|
menu->setPosition(Vec2::ZERO);
|
|
|
|
menuItem->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
2021-12-31 12:12:40 +08:00
|
|
|
menuItem->setPosition(Vec2(VisibleRect::left().x, VisibleRect::top().y - 50));
|
2019-11-23 20:27:39 +08:00
|
|
|
this->addChild(menu);
|
|
|
|
|
|
|
|
_angle = 0.0f;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
physicsScene = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Physics3DTestDemo::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_needShootBox = true;
|
|
|
|
event->stopPropagation();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Physics3DTestDemo::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (touches.size() && _camera)
|
|
|
|
{
|
|
|
|
auto touch = touches[0];
|
|
|
|
auto delta = touch->getDelta();
|
|
|
|
|
|
|
|
_angle -= CC_DEGREES_TO_RADIANS(delta.x);
|
|
|
|
_camera->setPosition3D(Vec3(100.0f * sinf(_angle), 50.0f, 100.0f * cosf(_angle)));
|
|
|
|
_camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
|
|
|
|
if (delta.lengthSquared() > 16)
|
|
|
|
{
|
|
|
|
_needShootBox = false;
|
|
|
|
}
|
|
|
|
event->stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Physics3DTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!_needShootBox)
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!touches.empty())
|
|
|
|
{
|
|
|
|
auto location = touches[0]->getLocationInView();
|
|
|
|
|
|
|
|
Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
|
|
|
|
nearP = _camera->unproject(nearP);
|
|
|
|
farP = _camera->unproject(farP);
|
|
|
|
Vec3 dir(farP - nearP);
|
|
|
|
shootBox(_camera->getPosition3D() + dir * 10.0f);
|
|
|
|
event->stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Physics3DTestDemo::Physics3DTestDemo() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Physics3DTestDemo::update(float /*delta*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Physics3DTestDemo::~Physics3DTestDemo() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Physics3DTestDemo::shootBox(const cocos2d::Vec3& des)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Physics3DRigidBodyDes rbDes;
|
|
|
|
Vec3 linearVel = des - _camera->getPosition3D();
|
|
|
|
linearVel.normalize();
|
|
|
|
linearVel *= 100.0f;
|
|
|
|
rbDes.originalTransform.translate(_camera->getPosition3D());
|
|
|
|
rbDes.mass = 1.f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(0.5f, 0.5f, 0.5f));
|
|
|
|
auto sprite = PhysicsSprite3D::create("Sprite3DTest/box.c3t", &rbDes);
|
|
|
|
sprite->setTexture("Images/Icon.png");
|
|
|
|
|
|
|
|
auto rigidBody = static_cast<Physics3DRigidBody*>(sprite->getPhysicsObj());
|
|
|
|
rigidBody->setLinearFactor(Vec3::ONE);
|
|
|
|
rigidBody->setLinearVelocity(linearVel);
|
|
|
|
rigidBody->setAngularVelocity(Vec3::ZERO);
|
|
|
|
rigidBody->setCcdMotionThreshold(0.5f);
|
|
|
|
rigidBody->setCcdSweptSphereRadius(0.4f);
|
|
|
|
|
|
|
|
this->addChild(sprite);
|
|
|
|
sprite->setPosition3D(_camera->getPosition3D());
|
|
|
|
sprite->setScale(0.5f);
|
|
|
|
sprite->syncNodeToPhysics();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// optimize, only sync node to physics
|
|
|
|
sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); // sync node to physics
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BasicPhysics3DDemo::subtitle() const
|
|
|
|
{
|
|
|
|
return "Basic Physics3D";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BasicPhysics3DDemo::init()
|
|
|
|
{
|
|
|
|
if (!Physics3DTestDemo::init())
|
|
|
|
return false;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create floor
|
2019-11-23 20:27:39 +08:00
|
|
|
Physics3DRigidBodyDes rbDes;
|
|
|
|
rbDes.mass = 0.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(60.0f, 1.0f, 60.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto floor = PhysicsSprite3D::create("Sprite3DTest/box.c3t", &rbDes);
|
|
|
|
floor->setTexture("Sprite3DTest/plane.png");
|
|
|
|
floor->setScaleX(60);
|
|
|
|
floor->setScaleZ(60);
|
|
|
|
this->addChild(floor);
|
|
|
|
floor->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
floor->syncNodeToPhysics();
|
2021-12-31 12:12:40 +08:00
|
|
|
// static object sync is not needed
|
2019-11-23 20:27:39 +08:00
|
|
|
floor->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// create several boxes using PhysicsSprite3D
|
2019-11-23 20:27:39 +08:00
|
|
|
rbDes.mass = 1.f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(0.8f, 0.8f, 0.8f));
|
2021-12-31 12:12:40 +08:00
|
|
|
float start_x = START_POS_X - ARRAY_SIZE_X / 2;
|
2019-11-23 20:27:39 +08:00
|
|
|
float start_y = START_POS_Y;
|
2021-12-31 12:12:40 +08:00
|
|
|
float start_z = START_POS_Z - ARRAY_SIZE_Z / 2;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int k = 0; k < ARRAY_SIZE_Y; k++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int i = 0; i < ARRAY_SIZE_X; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int j = 0; j < ARRAY_SIZE_Z; j++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
float x = 1.0 * i + start_x;
|
|
|
|
float y = 5.0 + 1.0 * k + start_y;
|
|
|
|
float z = 1.0 * j + start_z;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sprite = PhysicsSprite3D::create("Sprite3DTest/box.c3t", &rbDes);
|
|
|
|
sprite->setTexture("Images/CyanSquare.png");
|
|
|
|
sprite->setPosition3D(Vec3(x, y, z));
|
|
|
|
sprite->syncNodeToPhysics();
|
|
|
|
sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
sprite->setScale(0.8f);
|
|
|
|
this->addChild(sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
physicsScene->setPhysics3DDebugCamera(_camera);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Physics3DConstraintDemo::subtitle() const
|
|
|
|
{
|
|
|
|
return "Physics3D Constraint";
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string Physics3DKinematicDemo::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "Physics3D Kinematic";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DKinematicDemo::init()
|
|
|
|
{
|
|
|
|
if (!Physics3DTestDemo::init())
|
|
|
|
return false;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create floor
|
2019-11-23 20:27:39 +08:00
|
|
|
Physics3DRigidBodyDes rbDes;
|
|
|
|
rbDes.mass = 0.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(60.0f, 1.0f, 60.0f));
|
|
|
|
auto floor = PhysicsSprite3D::create("Sprite3DTest/box.c3t", &rbDes);
|
|
|
|
floor->setTexture("Sprite3DTest/plane.png");
|
|
|
|
floor->setScaleX(60);
|
|
|
|
floor->setScaleZ(60);
|
|
|
|
floor->setPosition3D(Vec3(0.f, -1.f, 0.f));
|
|
|
|
this->addChild(floor);
|
|
|
|
floor->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
floor->syncNodeToPhysics();
|
2021-12-31 12:12:40 +08:00
|
|
|
// static object sync is not needed
|
2019-11-23 20:27:39 +08:00
|
|
|
floor->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create Kinematics
|
2019-11-23 20:27:39 +08:00
|
|
|
for (unsigned int i = 0; i < 3; ++i)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
rbDes.mass = 0.f; // kinematic objects. zero mass so that it can not be affected by other dynamic objects
|
2019-11-23 20:27:39 +08:00
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(2.0f, 2.0f, 2.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sprite = PhysicsSprite3D::create("Sprite3DTest/box.c3t", &rbDes);
|
|
|
|
sprite->setTexture("Images/CyanSquare.png");
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
auto rigidBody = static_cast<Physics3DRigidBody*>(sprite->getPhysicsObj());
|
|
|
|
rigidBody->setKinematic(true);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
this->addChild(sprite);
|
|
|
|
|
|
|
|
sprite->setScale(2.0f);
|
|
|
|
sprite->setPosition3D(Vec3(-15.0f, 0.0f, 15.0f - 15.0f * i));
|
|
|
|
auto moveby = MoveBy::create(2.0f + i, Vec3(30.0f, 0.0f, 0.0f));
|
|
|
|
sprite->runAction(RepeatForever::create(Sequence::create(moveby, moveby->reverse(), nullptr)));
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create Dynamic
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// create several spheres
|
2019-11-23 20:27:39 +08:00
|
|
|
rbDes.mass = 1.f;
|
|
|
|
rbDes.shape = Physics3DShape::createSphere(0.5f);
|
2021-12-31 12:12:40 +08:00
|
|
|
float start_x = START_POS_X - ARRAY_SIZE_X / 2;
|
2019-11-23 20:27:39 +08:00
|
|
|
float start_y = START_POS_Y + 5.0f;
|
2021-12-31 12:12:40 +08:00
|
|
|
float start_z = START_POS_Z - ARRAY_SIZE_Z / 2;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int k = 0; k < ARRAY_SIZE_Y; k++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int i = 0; i < ARRAY_SIZE_X; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int j = 0; j < ARRAY_SIZE_Z; j++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
float x = 1.0 * i + start_x;
|
|
|
|
float y = 5.0 + 1.0 * k + start_y;
|
|
|
|
float z = 1.0 * j + start_z;
|
2019-11-23 20:27:39 +08:00
|
|
|
rbDes.originalTransform.setIdentity();
|
|
|
|
rbDes.originalTransform.translate(x, y, z);
|
|
|
|
|
|
|
|
auto sprite = PhysicsSprite3D::create("Sprite3DTest/sphere.c3b", &rbDes);
|
|
|
|
sprite->setTexture("Sprite3DTest/plane.png");
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
sprite->setScale(1.0f / sprite->getContentSize().width);
|
|
|
|
this->addChild(sprite);
|
|
|
|
sprite->setPosition3D(Vec3(x, y, z));
|
|
|
|
sprite->syncNodeToPhysics();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
physicsScene->setPhysics3DDebugCamera(_camera);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DConstraintDemo::init()
|
|
|
|
{
|
|
|
|
if (!Physics3DTestDemo::init())
|
|
|
|
return false;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// PhysicsSprite3D = Sprite3D + Physics3DComponent
|
2019-11-23 20:27:39 +08:00
|
|
|
Physics3DRigidBodyDes rbDes;
|
|
|
|
rbDes.disableSleep = true;
|
2021-12-31 12:12:40 +08:00
|
|
|
// create box
|
2019-11-23 20:27:39 +08:00
|
|
|
auto sprite = Sprite3D::create("Sprite3DTest/orc.c3b");
|
|
|
|
rbDes.mass = 10.f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(5.0f, 5.0f, 5.0f));
|
|
|
|
auto rigidBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
Quaternion quat;
|
|
|
|
Quaternion::createFromAxisAngle(Vec3(0.f, 1.f, 0.f), CC_DEGREES_TO_RADIANS(180), &quat);
|
|
|
|
auto component = Physics3DComponent::create(rigidBody, Vec3(0.f, -3.f, 0.f), quat);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->addComponent(component);
|
|
|
|
addChild(sprite);
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
sprite->setScale(0.4f);
|
|
|
|
sprite->setPosition3D(Vec3(-20.f, 5.f, 0.f));
|
2021-12-31 12:12:40 +08:00
|
|
|
// sync node position to physics
|
2019-11-23 20:27:39 +08:00
|
|
|
component->syncNodeToPhysics();
|
2021-12-31 12:12:40 +08:00
|
|
|
// physics controlled, we will not set position for it, so we can skip sync node position to physics
|
2019-11-23 20:27:39 +08:00
|
|
|
component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
physicsScene->setPhysics3DDebugCamera(_camera);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// create point to point constraint
|
2019-11-23 20:27:39 +08:00
|
|
|
Physics3DConstraint* constraint = Physics3DPointToPointConstraint::create(rigidBody, Vec3(2.5f, 2.5f, 2.5f));
|
|
|
|
physicsScene->getPhysics3DWorld()->addPhysics3DConstraint(constraint);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// create hinge constraint
|
2019-11-23 20:27:39 +08:00
|
|
|
rbDes.mass = 1.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(8.0f, 8.0f, 1.f));
|
|
|
|
rigidBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
component = Physics3DComponent::create(rigidBody);
|
|
|
|
sprite = Sprite3D::create("Sprite3DTest/box.c3t");
|
|
|
|
sprite->setTexture("Sprite3DTest/plane.png");
|
|
|
|
sprite->setScaleX(8.f);
|
|
|
|
sprite->setScaleY(8.f);
|
|
|
|
sprite->setPosition3D(Vec3(5.f, 0.f, 0.f));
|
|
|
|
sprite->addComponent(component);
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(sprite);
|
|
|
|
component->syncNodeToPhysics();
|
2021-12-31 12:12:40 +08:00
|
|
|
rigidBody->setAngularVelocity(Vec3(0, 3, 0));
|
2019-11-23 20:27:39 +08:00
|
|
|
constraint = Physics3DHingeConstraint::create(rigidBody, Vec3(4.f, 4.f, 0.5f), Vec3(0.f, 1.f, 0.f));
|
|
|
|
physicsScene->getPhysics3DWorld()->addPhysics3DConstraint(constraint);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// create slider constraint
|
2019-11-23 20:27:39 +08:00
|
|
|
rbDes.mass = 1.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(3.0f, 2.0f, 3.f));
|
|
|
|
rigidBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
component = Physics3DComponent::create(rigidBody);
|
|
|
|
sprite = Sprite3D::create("Sprite3DTest/box.c3t");
|
|
|
|
sprite->setTexture("Sprite3DTest/plane.png");
|
|
|
|
sprite->setScaleX(3.f);
|
|
|
|
sprite->setScaleZ(3.f);
|
|
|
|
sprite->setPosition3D(Vec3(30.f, 15.f, 0.f));
|
|
|
|
sprite->addComponent(component);
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(sprite);
|
|
|
|
component->syncNodeToPhysics();
|
2021-12-31 12:12:40 +08:00
|
|
|
rigidBody->setLinearVelocity(Vec3(0, 3, 0));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
rbDes.mass = 0.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(3.0f, 3.0f, 3.f));
|
|
|
|
auto rigidBodyB = Physics3DRigidBody::create(&rbDes);
|
|
|
|
component = Physics3DComponent::create(rigidBodyB);
|
|
|
|
sprite = Sprite3D::create("Sprite3DTest/box.c3t");
|
|
|
|
sprite->setTexture("Sprite3DTest/plane.png");
|
|
|
|
sprite->setScale(3.f);
|
|
|
|
sprite->setPosition3D(Vec3(30.f, 5.f, 0.f));
|
|
|
|
sprite->addComponent(component);
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(sprite);
|
|
|
|
component->syncNodeToPhysics();
|
|
|
|
|
|
|
|
Mat4 frameInA, frameInB;
|
|
|
|
Mat4::createRotationZ(CC_DEGREES_TO_RADIANS(90), &frameInA);
|
|
|
|
frameInB = frameInA;
|
|
|
|
frameInA.m[13] = -5.f;
|
|
|
|
frameInB.m[13] = 5.f;
|
|
|
|
constraint = Physics3DSliderConstraint::create(rigidBody, rigidBodyB, frameInA, frameInB, false);
|
|
|
|
physicsScene->getPhysics3DWorld()->addPhysics3DConstraint(constraint);
|
|
|
|
((Physics3DSliderConstraint*)constraint)->setLowerLinLimit(-5.f);
|
|
|
|
((Physics3DSliderConstraint*)constraint)->setUpperLinLimit(5.f);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// create ConeTwist constraint
|
2019-11-23 20:27:39 +08:00
|
|
|
rbDes.mass = 1.f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(3.f, 3.f, 3.f));
|
|
|
|
rigidBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
component = Physics3DComponent::create(rigidBody);
|
|
|
|
sprite = Sprite3D::create("Sprite3DTest/box.c3t");
|
|
|
|
sprite->setTexture("Sprite3DTest/plane.png");
|
|
|
|
sprite->setScale(3.f);
|
|
|
|
sprite->setPosition3D(Vec3(-10.f, 5.f, 0.f));
|
|
|
|
sprite->addComponent(component);
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(sprite);
|
|
|
|
component->syncNodeToPhysics();
|
|
|
|
|
|
|
|
Mat4::createRotationZ(CC_DEGREES_TO_RADIANS(90), &frameInA);
|
|
|
|
frameInA.m[12] = 0.f;
|
|
|
|
frameInA.m[13] = -10.f;
|
|
|
|
frameInA.m[14] = 0.f;
|
|
|
|
constraint = Physics3DConeTwistConstraint::create(rigidBody, frameInA);
|
|
|
|
physicsScene->getPhysics3DWorld()->addPhysics3DConstraint(constraint, true);
|
2021-12-31 12:12:40 +08:00
|
|
|
((Physics3DConeTwistConstraint*)constraint)
|
|
|
|
->setLimit(CC_DEGREES_TO_RADIANS(10), CC_DEGREES_TO_RADIANS(10), CC_DEGREES_TO_RADIANS(40));
|
|
|
|
|
|
|
|
// create 6 dof constraint
|
2019-11-23 20:27:39 +08:00
|
|
|
rbDes.mass = 1.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(3.0f, 3.0f, 3.f));
|
|
|
|
rigidBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
component = Physics3DComponent::create(rigidBody);
|
|
|
|
sprite = Sprite3D::create("Sprite3DTest/box.c3t");
|
|
|
|
sprite->setTexture("Sprite3DTest/plane.png");
|
|
|
|
sprite->setScale(3.f);
|
|
|
|
sprite->setPosition3D(Vec3(30.f, -5.f, 0.f));
|
|
|
|
sprite->addComponent(component);
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(sprite);
|
|
|
|
component->syncNodeToPhysics();
|
|
|
|
frameInA.setIdentity();
|
|
|
|
constraint = Physics3D6DofConstraint::create(rigidBody, frameInA, false);
|
|
|
|
physicsScene->getPhysics3DWorld()->addPhysics3DConstraint(constraint);
|
2021-12-31 12:12:40 +08:00
|
|
|
((Physics3D6DofConstraint*)constraint)->setAngularLowerLimit(Vec3(0, 0, 0));
|
|
|
|
((Physics3D6DofConstraint*)constraint)->setAngularUpperLimit(Vec3(0, 0, 0));
|
|
|
|
((Physics3D6DofConstraint*)constraint)->setLinearLowerLimit(Vec3(-10, 0, 0));
|
|
|
|
((Physics3D6DofConstraint*)constraint)->setLinearUpperLimit(Vec3(10, 0, 0));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Physics3DConstraintDemo::onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// ray trace
|
|
|
|
if (_camera)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocationInView();
|
|
|
|
Vec3 nearP(location.x, location.y, 0.0f), farP(location.x, location.y, 1.0f);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
_camera->unproject(size, &nearP, &nearP);
|
|
|
|
_camera->unproject(size, &farP, &farP);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Physics3DWorld::HitResult result;
|
|
|
|
bool ret = physicsScene->getPhysics3DWorld()->rayCast(nearP, farP, &result);
|
|
|
|
if (ret && result.hitObj->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY)
|
|
|
|
{
|
|
|
|
auto mat = result.hitObj->getWorldTransform().getInversed();
|
|
|
|
Vec3 position;
|
|
|
|
mat.transformPoint(result.hitPosition, &position);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
_constraint =
|
|
|
|
Physics3DPointToPointConstraint::create(static_cast<Physics3DRigidBody*>(result.hitObj), position);
|
2019-11-23 20:27:39 +08:00
|
|
|
physicsScene->getPhysics3DWorld()->addPhysics3DConstraint(_constraint, true);
|
|
|
|
_pickingDistance = (result.hitPosition - nearP).length();
|
|
|
|
event->stopPropagation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Physics3DTestDemo::onTouchesBegan(touches, event);
|
|
|
|
_needShootBox = false;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
void Physics3DConstraintDemo::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_constraint)
|
|
|
|
{
|
|
|
|
auto p2pConstraint = ((Physics3DPointToPointConstraint*)_constraint);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocationInView();
|
|
|
|
Vec3 nearP(location.x, location.y, 0.0f), farP(location.x, location.y, 1.0f);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
_camera->unproject(size, &nearP, &nearP);
|
|
|
|
_camera->unproject(size, &farP, &farP);
|
|
|
|
auto dir = (farP - nearP).getNormalized();
|
|
|
|
p2pConstraint->setPivotPointInB(nearP + dir * _pickingDistance);
|
|
|
|
event->stopPropagation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Physics3DTestDemo::onTouchesMoved(touches, event);
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
void Physics3DConstraintDemo::onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_constraint)
|
|
|
|
{
|
|
|
|
physicsScene->getPhysics3DWorld()->removePhysics3DConstraint(_constraint);
|
|
|
|
_constraint = nullptr;
|
|
|
|
event->stopPropagation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Physics3DTestDemo::onTouchesEnded(touches, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DTerrainDemo::init()
|
|
|
|
{
|
|
|
|
if (!Physics3DTestDemo::init())
|
|
|
|
return false;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Terrain::DetailMap r("TerrainTest/dirt.jpg"), g("TerrainTest/Grass2.jpg", 10), b("TerrainTest/road.jpg"),
|
|
|
|
a("TerrainTest/GreenSkin.jpg", 20);
|
|
|
|
|
|
|
|
Terrain::TerrainData data("TerrainTest/heightmap129.jpg", "TerrainTest/alphamap.png", r, g, b, a, Size(32, 32),
|
|
|
|
20.0f, 1.0f);
|
|
|
|
auto terrain = Terrain::create(data, Terrain::CrackFixedType::SKIRT);
|
2019-11-23 20:27:39 +08:00
|
|
|
terrain->setMaxDetailMapAmount(4);
|
|
|
|
terrain->setCameraMask(2);
|
|
|
|
terrain->setDrawWire(false);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
terrain->setSkirtHeightRatio(3);
|
2021-12-31 12:12:40 +08:00
|
|
|
terrain->setLODDistance(64, 128, 192);
|
2019-11-23 20:27:39 +08:00
|
|
|
terrain->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create terrain
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<float> heidata = terrain->getHeightData();
|
|
|
|
auto size = terrain->getTerrainSize();
|
|
|
|
Physics3DColliderDes colliderDes;
|
2021-12-31 12:12:40 +08:00
|
|
|
colliderDes.shape =
|
|
|
|
Physics3DShape::createHeightfield(size.width, size.height, &heidata[0], 1.0f, terrain->getMinHeight(),
|
|
|
|
terrain->getMaxHeight(), true, false, true);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto collider = Physics3DCollider::create(&colliderDes);
|
|
|
|
auto component = Physics3DComponent::create(collider);
|
|
|
|
terrain->addComponent(component);
|
|
|
|
this->addChild(terrain);
|
|
|
|
component->syncNodeToPhysics();
|
|
|
|
component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create several spheres
|
2019-11-23 20:27:39 +08:00
|
|
|
Physics3DRigidBodyDes rbDes;
|
|
|
|
rbDes.mass = 1.f;
|
|
|
|
rbDes.shape = Physics3DShape::createSphere(0.5f);
|
2021-12-31 12:12:40 +08:00
|
|
|
float start_x = START_POS_X - ARRAY_SIZE_X / 2 + 5.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
float start_y = START_POS_Y + 20.0f;
|
2021-12-31 12:12:40 +08:00
|
|
|
float start_z = START_POS_Z - ARRAY_SIZE_Z / 2;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int k = 0; k < ARRAY_SIZE_Y; k++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int i = 0; i < ARRAY_SIZE_X; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
for (int j = 0; j < ARRAY_SIZE_Z; j++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
float x = 1.0 * i + start_x;
|
|
|
|
float y = 5.0 + 1.0 * k + start_y;
|
|
|
|
float z = 1.0 * j + start_z;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto sprite = PhysicsSprite3D::create("Sprite3DTest/sphere.c3b", &rbDes);
|
|
|
|
sprite->setTexture("Sprite3DTest/plane.png");
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
sprite->setScale(1.0f / sprite->getContentSize().width);
|
|
|
|
sprite->setPosition3D(Vec3(x, y, z));
|
|
|
|
this->addChild(sprite);
|
|
|
|
sprite->syncNodeToPhysics();
|
|
|
|
sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// create mesh
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<Vec3> trianglesList = Bundle3D::getTrianglesList("Sprite3DTest/boss.c3b");
|
|
|
|
|
|
|
|
colliderDes.shape = Physics3DShape::createMesh(&trianglesList[0], (int)trianglesList.size() / 3);
|
|
|
|
|
|
|
|
auto sprite = PhysicsSprite3D::createWithCollider("Sprite3DTest/boss.c3b", &colliderDes);
|
|
|
|
sprite->setRotation3D(Vec3(-90.0f, 0.0f, 0.0f));
|
|
|
|
sprite->setPosition3D(Vec3(0.0f, 15.0f, 0.0f));
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
this->addChild(sprite);
|
|
|
|
sprite->syncNodeToPhysics();
|
|
|
|
sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::vector<std::pair<Physics3DShape*, Mat4>> shapeList;
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Mat4 localTrans;
|
|
|
|
auto bodyshape = Physics3DShape::createBox(Vec3(2.0f, 4.0f, 2.0f));
|
|
|
|
Mat4::createTranslation(0.0f, 2.0f, 0.0f, &localTrans);
|
|
|
|
shapeList.push_back(std::make_pair(bodyshape, localTrans));
|
|
|
|
auto headshape = Physics3DShape::createSphere(1.5f);
|
|
|
|
Mat4::createTranslation(0.6f, 5.0f, -1.5f, &localTrans);
|
|
|
|
shapeList.push_back(std::make_pair(headshape, localTrans));
|
|
|
|
auto lhandshape = Physics3DShape::createBox(Vec3(1.0f, 3.0f, 1.0f));
|
|
|
|
Mat4::createRotation(Vec3(1.0f, 0.0f, 0.0f), CC_DEGREES_TO_RADIANS(15.0f), &localTrans);
|
2021-12-31 12:12:40 +08:00
|
|
|
localTrans.m[12] = -1.5f;
|
|
|
|
localTrans.m[13] = 2.5f;
|
|
|
|
localTrans.m[14] = -2.5f;
|
2019-11-23 20:27:39 +08:00
|
|
|
shapeList.push_back(std::make_pair(lhandshape, localTrans));
|
|
|
|
auto rhandshape = Physics3DShape::createBox(Vec3(1.0f, 3.0f, 1.0f));
|
|
|
|
Mat4::createRotation(Vec3(1.0f, 0.0f, 0.0f), CC_DEGREES_TO_RADIANS(-15.0f), &localTrans);
|
2021-12-31 12:12:40 +08:00
|
|
|
localTrans.m[12] = 2.0f;
|
|
|
|
localTrans.m[13] = 2.5f;
|
|
|
|
localTrans.m[14] = 1.f;
|
2019-11-23 20:27:39 +08:00
|
|
|
shapeList.push_back(std::make_pair(rhandshape, localTrans));
|
|
|
|
|
|
|
|
rbDes.mass = 10.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createCompoundShape(shapeList);
|
|
|
|
auto rigidBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
component = Physics3DComponent::create(rigidBody);
|
|
|
|
auto sprite = Sprite3D::create("Sprite3DTest/orc.c3b");
|
|
|
|
sprite->addComponent(component);
|
|
|
|
sprite->setRotation3D(Vec3(0.0f, 180.0f, 0.0f));
|
|
|
|
sprite->setPosition3D(Vec3(-5.0f, 20.0f, 0.0f));
|
|
|
|
sprite->setScale(0.4f);
|
|
|
|
sprite->setCameraMask(2);
|
|
|
|
this->addChild(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
physicsScene->setPhysics3DDebugCamera(_camera);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string Physics3DTerrainDemo::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "Physics3D Terrain";
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string Physics3DCollisionCallbackDemo::subtitle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return "Physics3D CollisionCallback";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DCollisionCallbackDemo::init()
|
|
|
|
{
|
|
|
|
if (!Physics3DTestDemo::init())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
{
|
|
|
|
Physics3DRigidBodyDes rbDes;
|
|
|
|
|
|
|
|
float scale = 2.0f;
|
|
|
|
std::vector<Vec3> trianglesList = Bundle3D::getTrianglesList("Sprite3DTest/boss.c3b");
|
2021-12-31 12:12:40 +08:00
|
|
|
for (auto& it : trianglesList)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
it *= scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
rbDes.mass = 0.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createMesh(&trianglesList[0], (int)trianglesList.size() / 3);
|
|
|
|
auto rigidBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
auto component = Physics3DComponent::create(rigidBody);
|
|
|
|
auto sprite = Sprite3D::create("Sprite3DTest/boss.c3b");
|
|
|
|
sprite->addComponent(component);
|
|
|
|
sprite->setRotation3D(Vec3(-90.0f, 0.0f, 0.0f));
|
|
|
|
sprite->setScale(scale);
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(sprite);
|
2021-12-31 12:12:40 +08:00
|
|
|
// preload
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
2021-12-31 12:12:40 +08:00
|
|
|
rigidBody->setCollisionCallback([=](const Physics3DCollisionInfo& ci) {
|
|
|
|
if (!ci.collisionPointList.empty())
|
|
|
|
{
|
|
|
|
if (ci.objA->getMask() != 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto ps = PUParticleSystem3D::create("Particle3D/scripts/mp_hit_04.pu");
|
|
|
|
ps->setPosition3D(ci.collisionPointList[0].worldPositionOnB);
|
|
|
|
ps->setScale(0.05f);
|
|
|
|
ps->startParticleSystem();
|
|
|
|
ps->setCameraMask(2);
|
|
|
|
this->addChild(ps);
|
2021-12-31 12:12:40 +08:00
|
|
|
ps->runAction(Sequence::create(DelayTime::create(1.0f),
|
|
|
|
CallFunc::create([=]() { ps->removeFromParent(); }), nullptr));
|
2019-11-23 20:27:39 +08:00
|
|
|
ci.objA->setMask(0);
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
// CCLOG("------------BoxB Collision Info------------");
|
|
|
|
// CCLOG("Collision Point Num: %d", ci.collisionPointList.size());
|
|
|
|
// for (auto &iter : ci.collisionPointList){
|
|
|
|
// CCLOG("Collision Position On A: (%.2f, %.2f, %.2f)", iter.worldPositionOnA.x, iter.worldPositionOnA.y,
|
|
|
|
// iter.worldPositionOnA.z); CCLOG("Collision Position On B: (%.2f, %.2f, %.2f)",
|
|
|
|
// iter.worldPositionOnB.x, iter.worldPositionOnB.y, iter.worldPositionOnB.z); CCLOG("Collision Normal
|
|
|
|
// On B: (%.2f, %.2f, %.2f)", iter.worldNormalOnB.x, iter.worldNormalOnB.y, iter.worldNormalOnB.z);
|
|
|
|
// }
|
|
|
|
// CCLOG("------------BoxB Collision Info------------");
|
2019-11-23 20:27:39 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
physicsScene->setPhysics3DDebugCamera(_camera);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Physics3DColliderDemo::subtitle() const
|
|
|
|
{
|
|
|
|
return "Physics3D Trigger";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Physics3DColliderDemo::init()
|
|
|
|
{
|
|
|
|
if (!Physics3DTestDemo::init())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Physics3DRigidBodyDes rbDes;
|
|
|
|
rbDes.mass = 1.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(3.0f, 3.0f, 3.f));
|
|
|
|
auto playerBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
auto component = Physics3DComponent::create(playerBody);
|
|
|
|
playerBody->setKinematic(true);
|
|
|
|
auto sprite = Sprite3D::create("Sprite3DTest/box.c3t");
|
|
|
|
sprite->setTexture("Sprite3DTest/plane.png");
|
|
|
|
sprite->setScale(3.f);
|
|
|
|
sprite->setPosition3D(Vec3(0.0f, 0.f, 30.f));
|
|
|
|
sprite->addComponent(component);
|
|
|
|
sprite->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
auto moveby = MoveBy::create(5.0f, Vec3(0.0f, 0.0f, -60.0f));
|
|
|
|
sprite->runAction(RepeatForever::create(Sequence::create(moveby, moveby->reverse(), nullptr)));
|
|
|
|
this->addChild(sprite);
|
|
|
|
|
|
|
|
{
|
|
|
|
Physics3DColliderDes colliderDes;
|
|
|
|
colliderDes.shape = Physics3DShape::createSphere(10.0f);
|
|
|
|
colliderDes.isTrigger = true;
|
|
|
|
auto collider = Physics3DCollider::create(&colliderDes);
|
|
|
|
auto component = Physics3DComponent::create(collider);
|
|
|
|
auto node = Node::create();
|
|
|
|
node->addComponent(component);
|
|
|
|
node->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(node);
|
|
|
|
|
|
|
|
Physics3DRigidBodyDes rbDes;
|
|
|
|
rbDes.mass = 1.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(10.0f, 10.0f, 1.f));
|
|
|
|
auto rigidBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
component = Physics3DComponent::create(rigidBody);
|
|
|
|
rigidBody->setKinematic(true);
|
|
|
|
auto doorLeft = Sprite3D::create("Sprite3DTest/box.c3t");
|
|
|
|
doorLeft->setTexture("Sprite3DTest/plane.png");
|
|
|
|
doorLeft->setScaleX(10.0f);
|
|
|
|
doorLeft->setScaleY(10.0f);
|
|
|
|
doorLeft->setScaleZ(1.0f);
|
|
|
|
doorLeft->setPosition3D(Vec3(-5.0f, 0.0f, 0.0f));
|
|
|
|
doorLeft->addComponent(component);
|
|
|
|
doorLeft->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
node->addChild(doorLeft);
|
|
|
|
|
|
|
|
rbDes.mass = 1.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createBox(Vec3(10.0f, 10.0f, 1.f));
|
|
|
|
rigidBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
component = Physics3DComponent::create(rigidBody);
|
|
|
|
rigidBody->setKinematic(true);
|
|
|
|
auto doorRight = Sprite3D::create("Sprite3DTest/box.c3t");
|
|
|
|
doorRight->setTexture("Sprite3DTest/plane.png");
|
|
|
|
doorRight->setScaleX(10.0f);
|
|
|
|
doorRight->setScaleY(10.0f);
|
|
|
|
doorRight->setScaleZ(1.0f);
|
|
|
|
doorRight->setPosition3D(Vec3(5.0f, 0.0f, 0.0f));
|
|
|
|
doorRight->addComponent(component);
|
|
|
|
doorRight->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
node->addChild(doorRight);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
collider->onTriggerEnter = [=](Physics3DObject* otherObject) {
|
|
|
|
if (otherObject == playerBody)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto moveby = MoveBy::create(1.0f, Vec3(-5.0f, 0.0f, 0.0f));
|
|
|
|
doorLeft->runAction(moveby);
|
|
|
|
doorRight->runAction(moveby->reverse());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
collider->onTriggerExit = [=](Physics3DObject* otherObject) {
|
|
|
|
if (otherObject == playerBody)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto moveby = MoveBy::create(1.0f, Vec3(5.0f, 0.0f, 0.0f));
|
|
|
|
doorLeft->runAction(moveby);
|
|
|
|
doorRight->runAction(moveby->reverse());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
physicsScene->setPhysics3DDebugCamera(_camera);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|