Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "HelloWorldScene.h"
- #include "SimpleAudioEngine.h"
- #include "physics3d/CCPhysics3D.h"
- USING_NS_CC;
- static Scene* physicsScene = NULL;
- Scene* HelloWorld::createScene()
- {
- // 'scene' is an autorelease object
- auto scene = Scene::create();
- physicsScene = scene;
- // 'layer' is an autorelease object
- auto layer = HelloWorld::create();
- // add layer as a child to scene
- scene->addChild(layer);
- // return the scene
- return scene;
- }
- // on "init" you need to initialize your instance
- bool HelloWorld::init()
- {
- //////////////////////////////
- // 1. super init first
- if ( !Layer::init() )
- {
- return false;
- }
- auto visibleSize = Director::getInstance()->getVisibleSize();
- Vec2 origin = Director::getInstance()->getVisibleOrigin();
- Camera* _camera;
- if (physicsScene->initWithPhysics())
- {
- physicsScene->getPhysics3DWorld()->setDebugDrawEnable(true);
- Size size = Director::getInstance()->getWinSize();
- _camera = Camera::createPerspective(60.0f, size.width / size.height, 1.0f, 10000.0f);
- _camera->setPosition3D(Vec3(0.0f, 0.0f, 10.0f));
- _camera->lookAt(Vec3(1.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
- _camera->setCameraFlag(CameraFlag::USER1);
- physicsScene->addChild(_camera);
- physicsScene->setPhysics3DDebugCamera(_camera);
- TTFConfig ttfConfig("fonts/arial.ttf", 10);
- auto label = Label::createWithTTF(ttfConfig,"DebugDraw OFF");
- auto menuItem = MenuItemLabel::create(label, [=](Ref *ref){
- if (physicsScene->getPhysics3DWorld()->isDebugDrawEnabled()){
- physicsScene->getPhysics3DWorld()->setDebugDrawEnable(false);
- label->setString("DebugDraw OFF");
- }else{
- physicsScene->getPhysics3DWorld()->setDebugDrawEnable(true);
- label->setString("DebugDraw ON");
- }
- });
- auto menu = Menu::create(menuItem, nullptr);
- menu->setPosition(Vec2::ZERO);
- menuItem->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
- menuItem->setPosition( Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2.0+origin.y-50) );
- this->addChild(menu);
- }
- shootBox(Vec3(0.0f, 0.0f, 0.0f));
- return true;
- }
- void HelloWorld::shootBox( const cocos2d::Vec3 &des )
- {
- auto visibleSize = Director::getInstance()->getVisibleSize();
- Vec2 origin = Director::getInstance()->getVisibleOrigin();
- physicsScene->getPhysics3DWorld()->setGravity(Vec3(0.0f, 0.0f, 0.0f));
- Physics3DRigidBodyDes rbDes;
- Vec3 initPos = Vec3(visibleSize.width/2 + origin.x + des.x, visibleSize.height/2 + origin.y, 0.0f);
- initPos = Vec3(0.0f, 0.0f, 0.0f);
- Vec3 linearVel = Vec3(0.0f, 0.0f, 0.0f);
- linearVel.normalize();
- rbDes.originalTransform.translate(Vec3(initPos));
- rbDes.mass = 1.f;
- rbDes.shape = Physics3DShape::createBox(Vec3(1, 1, 1));
- auto sprite = PhysicsSprite3D::create("cube.c3t", &rbDes);
- // sprite->setTexture("1.jpg");
- auto rigidBody = static_cast<Physics3DRigidBody*>(sprite->getPhysicsObj());
- rigidBody->setLinearFactor(Vec3::ONE);
- rigidBody->setAngularFactor(Vec3(100,100,100));
- rigidBody->setLinearVelocity(linearVel * 200.0f);
- rigidBody->setAngularVelocity(Vec3::ZERO);
- rigidBody->setCcdMotionThreshold(0.5f);
- rigidBody->setCcdSweptSphereRadius(0.4f);
- rigidBody->setFriction(1.0f);
- rigidBody->setRestitution(1.0f);
- this->addChild(sprite);
- sprite->setPosition3D(initPos);
- sprite->setScale(0.5f);
- sprite->syncNodeToPhysics();
- //optimize, only sync node to physics
- sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); //sync node to physics
- //sprite->setVisible(false);
- sprite->setCameraMask((unsigned short)CameraFlag::USER1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement