Advertisement
Guest User

Untitled

a guest
Oct 8th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. #include "HelloWorldScene.h"
  2. #include "SimpleAudioEngine.h"
  3. #include "physics3d/CCPhysics3D.h"
  4.  
  5. USING_NS_CC;
  6.  
  7. static Scene* physicsScene = NULL;
  8.  
  9. Scene* HelloWorld::createScene()
  10. {
  11.     // 'scene' is an autorelease object
  12.     auto scene = Scene::create();
  13.    
  14.     physicsScene = scene;
  15.    
  16.     // 'layer' is an autorelease object
  17.     auto layer = HelloWorld::create();
  18.  
  19.     // add layer as a child to scene
  20.     scene->addChild(layer);
  21.  
  22.  
  23.     // return the scene
  24.     return scene;
  25. }
  26.  
  27. // on "init" you need to initialize your instance
  28. bool HelloWorld::init()
  29. {
  30.     //////////////////////////////
  31.     // 1. super init first
  32.     if ( !Layer::init() )
  33.     {
  34.         return false;
  35.     }
  36.    
  37.     auto visibleSize = Director::getInstance()->getVisibleSize();
  38.     Vec2 origin = Director::getInstance()->getVisibleOrigin();
  39.    
  40.     Camera* _camera;
  41.    
  42.     if (physicsScene->initWithPhysics())
  43.     {
  44.         physicsScene->getPhysics3DWorld()->setDebugDrawEnable(true);
  45.        
  46.         Size size = Director::getInstance()->getWinSize();
  47.         _camera = Camera::createPerspective(60.0f, size.width / size.height, 1.0f, 10000.0f);
  48.         _camera->setPosition3D(Vec3(0.0f, 0.0f, 10.0f));
  49.         _camera->lookAt(Vec3(1.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
  50.         _camera->setCameraFlag(CameraFlag::USER1);
  51.         physicsScene->addChild(_camera);
  52.        
  53.         physicsScene->setPhysics3DDebugCamera(_camera);
  54.        
  55.         TTFConfig ttfConfig("fonts/arial.ttf", 10);
  56.         auto label = Label::createWithTTF(ttfConfig,"DebugDraw OFF");
  57.         auto menuItem = MenuItemLabel::create(label, [=](Ref *ref){
  58.            
  59.             if (physicsScene->getPhysics3DWorld()->isDebugDrawEnabled()){
  60.                 physicsScene->getPhysics3DWorld()->setDebugDrawEnable(false);
  61.                 label->setString("DebugDraw OFF");
  62.             }else{
  63.                 physicsScene->getPhysics3DWorld()->setDebugDrawEnable(true);
  64.                 label->setString("DebugDraw ON");
  65.             }
  66.            
  67.         });
  68.        
  69.         auto menu = Menu::create(menuItem, nullptr);
  70.         menu->setPosition(Vec2::ZERO);
  71.         menuItem->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
  72.         menuItem->setPosition( Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2.0+origin.y-50) );
  73.         this->addChild(menu);
  74.     }
  75.    
  76.     shootBox(Vec3(0.0f, 0.0f, 0.0f));
  77.    
  78.     return true;
  79. }
  80.  
  81. void HelloWorld::shootBox( const cocos2d::Vec3 &des )
  82. {
  83.     auto visibleSize = Director::getInstance()->getVisibleSize();
  84.     Vec2 origin = Director::getInstance()->getVisibleOrigin();
  85.    
  86.     physicsScene->getPhysics3DWorld()->setGravity(Vec3(0.0f, 0.0f, 0.0f));
  87.    
  88.     Physics3DRigidBodyDes rbDes;
  89.    
  90.     Vec3 initPos = Vec3(visibleSize.width/2 + origin.x + des.x, visibleSize.height/2 + origin.y, 0.0f);
  91.    
  92.     initPos = Vec3(0.0f, 0.0f, 0.0f);
  93.    
  94.     Vec3 linearVel = Vec3(0.0f, 0.0f, 0.0f);
  95.     linearVel.normalize();
  96.    
  97.     rbDes.originalTransform.translate(Vec3(initPos));
  98.     rbDes.mass = 1.f;
  99.     rbDes.shape = Physics3DShape::createBox(Vec3(1, 1, 1));
  100.     auto sprite = PhysicsSprite3D::create("cube.c3t", &rbDes);
  101.     //  sprite->setTexture("1.jpg");
  102.    
  103.     auto rigidBody = static_cast<Physics3DRigidBody*>(sprite->getPhysicsObj());
  104.     rigidBody->setLinearFactor(Vec3::ONE);
  105.     rigidBody->setAngularFactor(Vec3(100,100,100));
  106.     rigidBody->setLinearVelocity(linearVel * 200.0f);
  107.     rigidBody->setAngularVelocity(Vec3::ZERO);
  108.     rigidBody->setCcdMotionThreshold(0.5f);
  109.     rigidBody->setCcdSweptSphereRadius(0.4f);
  110.     rigidBody->setFriction(1.0f);
  111.     rigidBody->setRestitution(1.0f);
  112.    
  113.     this->addChild(sprite);
  114.     sprite->setPosition3D(initPos);
  115.     sprite->setScale(0.5f);
  116.     sprite->syncNodeToPhysics();
  117.    
  118.     //optimize, only sync node to physics
  119.     sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); //sync node to physics
  120.    
  121.     //sprite->setVisible(false);
  122.    
  123.     sprite->setCameraMask((unsigned short)CameraFlag::USER1);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement