Example #1
0
bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto origin = Director::getInstance()->getVisibleOrigin();



	camera = Camera::createPerspective(30, (float)visibleSize.width / visibleSize.height, 1.0, 1000);
	camera->setBackgroundBrush(CameraBackgroundBrush::createColorBrush(Color4F(0.0f, 1.0f, 1.0f, 0.5f), 1.0f));
	camera->setPosition3D(Vec3(0.0f, 50.0f, 100.0f));
	camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0, 1.0, 0.0));
	camera->setCameraFlag(CameraFlag::USER2);
	this->addChild(camera);

	/**
	//Create Plane
	Physics3DRigidBodyDes rbd_plan;
	rbd_plan.mass = 0.0f;
	rbd_plan.shape = Physics3DShape::createBox(Vec3(500, 5.0f, 500));
	auto physics_rbd_plan = Physics3DRigidBody::create(&rbd_plan);
	physics_rbd_plan->setFriction(20);
	auto component_plan = Physics3DComponent::create(physics_rbd_plan);
	_plan = Sprite3D::create("box.c3t");
	_plan->setTexture("plane.png");
	_plan->setScale(30.0f);
	_plan->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0));
	_plan->setScaleX(500);
	_plan->setScaleY(5);
	_plan->setScaleZ(500);
	_plan->setPositionZ(0);
	_plan->setPositionY(0);
	_plan->setPositionX(0);
	_plan->setGlobalZOrder(-1);
	_plan->addComponent(component_plan);
	component_plan->syncNodeToPhysics();
	component_plan->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE);
	_plan->setCameraMask((unsigned short)CameraFlag::USER2);
	this->addChild(_plan, 1);
	**/
	Terrain::DetailMap r("dirt.jpg");
	Terrain::TerrainData data("heightmap16.jpg", "alphamap.png", r, r, r, r, Size(32, 32), 5.0f, 1.0f);
	auto _terrain = Terrain::create(data, Terrain::CrackFixedType::SKIRT);
	_terrain->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0));
	_terrain->setPositionZ(0);
	_terrain->setPositionY(0);
	_terrain->setPositionX(0);
	_terrain->setLODDistance(64, 128, 192);
	_terrain->setDrawWire(false);
	_terrain->setMaxDetailMapAmount(4);
	_terrain->setSkirtHeightRatio(2);
	_terrain->setCameraMask((unsigned short)CameraFlag::USER2);
	//create terrain
	std::vector<float> heidata = _terrain->getHeightData();
	auto size = _terrain->getTerrainSize();
	Physics3DColliderDes colliderDes;
	colliderDes.shape = Physics3DShape::createHeightfield(size.width, size.height, &heidata[0], 1.0f, _terrain->getMinHeight(), _terrain->getMaxHeight(), true, false, true);
	auto collider = Physics3DCollider::create(&colliderDes);
	auto component = Physics3DComponent::create(collider);
	_terrain->addComponent(component);
	component->syncNodeToPhysics();
	component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE);
	this->addChild(_terrain);

	
	//////////////////////////////////////////////// car
	Physics3DRigidBodyDes rbd_cabine;
	rbd_cabine.disableSleep = true;
	rbd_cabine.mass = 500.0f;
	rbd_cabine.shape = Physics3DShape::createBox(Vec3(4, 2.0f, 8));
	physics_rbd_cabine = Physics3DRigidBody::create(&rbd_cabine);
	auto cabine_component = Physics3DComponent::create(physics_rbd_cabine);
	car_cabine = Sprite3D::create("T-90.c3t");
	car_cabine->setTexture("Main Body 2.png");
	car_cabine->setScale(3);
	//car_cabine->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0));
	car_cabine->setPositionX(0);
	car_cabine->setPositionY(10);
	car_cabine->setPositionZ(0);
	car_cabine->setGlobalZOrder(-1);
	car_cabine->addComponent(cabine_component);
	cabine_component->syncNodeToPhysics();
	cabine_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
	car_cabine->setCameraMask((unsigned short)CameraFlag::USER2);
	this->addChild(car_cabine, 1);


	Physics3DRigidBodyDes rbd_wheel1;
	rbd_wheel1.disableSleep = true;
	rbd_wheel1.mass = 15.0f;
	//rbd_wheel1.shape = Physics3DShape::createCylinder(3, 1);
	rbd_wheel1.shape = Physics3DShape::createSphere(1.5);
	auto physics_rbd_wheel1 = Physics3DRigidBody::create(&rbd_wheel1);
	auto wheel1_component = Physics3DComponent::create(physics_rbd_wheel1);
	wheel1 = Sprite3D::create();
	//wheel1->setTexture("Gun.png");
	//wheel1->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0));
	wheel1->setPositionX(0);
	wheel1->setPositionY(10);
	wheel1->setPositionZ(0);
	wheel1->setGlobalZOrder(-1);
	wheel1->addComponent(wheel1_component);
	wheel1_component->syncNodeToPhysics();
	wheel1_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
	wheel1->setCameraMask((unsigned short)CameraFlag::USER2);
	this->addChild(wheel1, 1);

	Physics3DRigidBodyDes rbd_wheel2;
	rbd_wheel2.disableSleep = true;
	rbd_wheel2.mass = 15.0f;
	//rbd_wheel2.shape = Physics3DShape::createCylinder(3, 1);
	rbd_wheel2.shape = Physics3DShape::createSphere(1.5);
	auto physics_rbd_wheel2 = Physics3DRigidBody::create(&rbd_wheel2);
	auto wheel2_component = Physics3DComponent::create(physics_rbd_wheel2);
	wheel2 = Sprite3D::create();
	//wheel2->setTexture("Gun.png");
	//wheel2->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0));
	wheel2->setPositionX(0);
	wheel2->setPositionY(10);
	wheel2->setPositionZ(0);
	wheel2->setGlobalZOrder(-1);
	wheel2->addComponent(wheel2_component);
	wheel2_component->syncNodeToPhysics();
	wheel2_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
	wheel2->setCameraMask((unsigned short)CameraFlag::USER2);
	this->addChild(wheel2, 1);

	Physics3DRigidBodyDes rbd_wheel3;
	rbd_wheel3.disableSleep = true;
	rbd_wheel3.mass = 15.0f;
	rbd_wheel3.shape = Physics3DShape::createSphere(1.5);
	//rbd_wheel3.shape = Physics3DShape::createCylinder(3, 1);
	physics_rbd_wheel3 = Physics3DRigidBody::create(&rbd_wheel3);
	auto wheel3_component = Physics3DComponent::create(physics_rbd_wheel3);
	wheel3 = Sprite3D::create();
	//wheel3->setTexture("Gun.png");
	//wheel3->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0));
	wheel3->setPositionX(0);
	wheel3->setPositionY(10);
	wheel3->setPositionZ(0);
	wheel3->setGlobalZOrder(-1);
	wheel3->addComponent(wheel3_component);
	wheel3_component->syncNodeToPhysics();
	wheel3_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
	wheel3->setCameraMask((unsigned short)CameraFlag::USER2);
	this->addChild(wheel3, 1);

	Physics3DRigidBodyDes rbd_wheel4;
	rbd_wheel4.disableSleep = true;
	rbd_wheel4.mass = 15.0f;
	//rbd_wheel4.shape = Physics3DShape::createCylinder(3, 1);
	rbd_wheel4.shape = Physics3DShape::createSphere(1.5);
	physics_rbd_wheel4 = Physics3DRigidBody::create(&rbd_wheel4);
	auto wheel4_component = Physics3DComponent::create(physics_rbd_wheel4);
	wheel4 = Sprite3D::create();
	//wheel3->setTexture("Gun.png");
	//wheel4->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0));
	wheel4->setPositionX(0);
	wheel4->setPositionY(10);
	wheel4->setPositionZ(0);
	wheel4->setGlobalZOrder(-1);
	wheel4->addComponent(wheel4_component);
	wheel4_component->syncNodeToPhysics();
	wheel4_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
	wheel4->setCameraMask((unsigned short)CameraFlag::USER2);
	this->addChild(wheel4, 1);

	Physics3DRigidBodyDes rbd_wheel_bar;
	rbd_wheel_bar.disableSleep = true;
	rbd_wheel_bar.mass = 400.0f;
	rbd_wheel_bar.shape = Physics3DShape::createBox(Vec3(2, 2.0f, 6));
	physics_rbd_wheel_bar = Physics3DRigidBody::create(&rbd_wheel_bar);
	auto car_wheel_bar_component = Physics3DComponent::create(physics_rbd_wheel_bar);
	car_wheel_bar = Sprite3D::create();
	//car_wheel_bar->setTexture("Gun.png");
	car_wheel_bar->setScale(0.01);
	car_wheel_bar->setPositionX(0);
	car_wheel_bar->setPositionY(10);
	car_wheel_bar->setPositionZ(0);
	//car_wheel_bar->setPosition3D(Vec3(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y, 0));
	car_wheel_bar->setGlobalZOrder(-1);
	car_wheel_bar->addComponent(car_wheel_bar_component);
	car_wheel_bar_component->syncNodeToPhysics();
	car_wheel_bar_component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
	car_wheel_bar->setCameraMask((unsigned short)CameraFlag::USER2);
	this->addChild(car_wheel_bar, 1);

	auto constraint = Physics3DHingeConstraint::create(physics_rbd_cabine, physics_rbd_wheel1, Vec3(0.0f, 0.0, 6.0f), Vec3(0.f, -3.0f, 0.f), Vec3(1.0f, 0.0, 0.0f), Vec3(0.f, 1.0f, 0.f));
	psychics_scene->getPhysics3DWorld()->addPhysics3DConstraint(constraint);
	auto constraint2 = Physics3DHingeConstraint::create(physics_rbd_cabine, physics_rbd_wheel2, Vec3(0.0f, 0.0, 6.0f), Vec3(0.f, 3.0f, 0.f), Vec3(1.0f, 0.0, 0.0f), Vec3(0.f, 1.0f, 0.f));
	psychics_scene->getPhysics3DWorld()->addPhysics3DConstraint(constraint2);


	constraint5 = Physics3DHingeConstraint::create(physics_rbd_cabine, physics_rbd_wheel_bar, Vec3(0.0f, 0.0, -6.0f), Vec3(0.f, 0.0f, 0.f), Vec3(0.0f, 1.0, 0.0f), Vec3(0.f, 1.0f, 0.f));
	constraint5->setLimit(CC_DEGREES_TO_RADIANS(-0.5), CC_DEGREES_TO_RADIANS(0.5));
	psychics_scene->getPhysics3DWorld()->addPhysics3DConstraint(constraint5);


	auto constraint3 = Physics3DHingeConstraint::create(physics_rbd_wheel_bar, physics_rbd_wheel3, Vec3(0.0f, 0.0, -3.0f), Vec3(0.f, -3.0f, 0.f), Vec3(1.0f, 0.0, 0.0f), Vec3(0.f, 1.0f, 0.f));
	psychics_scene->getPhysics3DWorld()->addPhysics3DConstraint(constraint3);
	auto constraint4 = Physics3DHingeConstraint::create(physics_rbd_wheel_bar, physics_rbd_wheel4, Vec3(0.0f, 0.0, -3.0f), Vec3(0.f, 3.0f, 0.f), Vec3(1.0f, 0.0, 0.0f), Vec3(0.f, 1.0f, 0.f));
	psychics_scene->getPhysics3DWorld()->addPhysics3DConstraint(constraint4);


	//add a point light
	auto light = PointLight::create(Vec3(0, 50, 0), Color3B(255, 255, 255), 150);
	addChild(light);
	//set the ambient light 
	auto ambient = AmbientLight::create(Color3B(55, 55, 55));
	addChild(ambient);


	/**
	Physics3DRigidBodyDes boxesrbDes;
	boxesrbDes.mass = 1.f;
	boxesrbDes.shape = Physics3DShape::createBox(Vec3(5, 5, 5));
	float start_x = 10- ARRAY_SIZE_X / 2;
	float start_y = 0 + 5.0f;
	float start_z = 10 - ARRAY_SIZE_Z / 2;

	for (int k = 0; k<ARRAY_SIZE_Y; k++)
	{
		for (int i = 0; i<ARRAY_SIZE_X; i++)
		{
			for (int j = 0; j<ARRAY_SIZE_Z; j++)
			{
				float x = 1.0*i + start_x;
				float y = 5.0 + 1.0*k + start_y;
				float z = 1.0*j + start_z;
				boxesrbDes.originalTransform.setIdentity();
				boxesrbDes.originalTransform.translate(x, y, z);

				auto sprite = PhysicsSprite3D::create("box.c3t", &boxesrbDes);
				sprite->setTexture("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();

				sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
			}
		}
	}

	**/



	auto listener = EventListenerTouchAllAtOnce::create();
	listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
	listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
	listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);


	//psychics_scene->setPhysics3DDebugCamera(camera); 
	schedule(schedule_selector(HelloWorld::myupdate), .005);
	//this->scheduleUpdate();
	
    return true;
}
Example #2
0
bool Physics3DTerrainDemo::init()
{
    if (!Physics3DTestDemo::init())
        return false;

    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);
    terrain->setMaxDetailMapAmount(4);
    terrain->setCameraMask(2);
    terrain->setDrawWire(false);
    
    terrain->setSkirtHeightRatio(3);
    terrain->setLODDistance(64,128,192);
    terrain->setCameraMask((unsigned short)CameraFlag::USER1);

    //create terrain
    Physics3DRigidBodyDes rbDes;
    rbDes.mass = 0.0f;
    std::vector<float> heidata = terrain->getHeightData();
    auto size = terrain->getTerrainSize();
    rbDes.shape = Physics3DShape::createHeightfield(size.width, size.height, &heidata[0], 1.0f, terrain->getMinHeight(), terrain->getMaxHeight(), true, false, true);
    auto rigidBody = Physics3DRigidBody::create(&rbDes);
    auto component = Physics3DComponent::create(rigidBody);
    terrain->addComponent(component);
    this->addChild(terrain);
    component->syncNodeToPhysics();
    component->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE);


    //create several spheres
    rbDes.mass = 1.f;
    rbDes.shape = Physics3DShape::createSphere(0.5f);
    float start_x = START_POS_X - ARRAY_SIZE_X/2 + 5.0f;
    float start_y = START_POS_Y + 20.0f;
    float start_z = START_POS_Z - ARRAY_SIZE_Z/2;

    for (int k=0;k<ARRAY_SIZE_Y;k++)
    {
        for (int i=0;i<ARRAY_SIZE_X;i++)
        {
            for(int j = 0;j<ARRAY_SIZE_Z;j++)
            {
                float x = 1.0*i + start_x;
                float y = 5.0+1.0*k + start_y;
                float z = 1.0*j + start_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);
                sprite->setPosition3D(Vec3(x, y, z));
                this->addChild(sprite);
                sprite->syncNodeToPhysics();
                sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE);
            }
        }
    }

    //create mesh
    std::vector<Vec3> trianglesList = Bundle3D::getTrianglesList("Sprite3DTest/boss.c3b");

    rbDes.mass = 0.0f;
    rbDes.shape = Physics3DShape::createMesh(&trianglesList[0], (int)trianglesList.size() / 3);
    rigidBody = Physics3DRigidBody::create(&rbDes);
    component = Physics3DComponent::create(rigidBody);
    auto sprite = Sprite3D::create("Sprite3DTest/boss.c3b");
    sprite->addComponent(component);
    sprite->setRotation3D(Vec3(-90.0f, 0.0f, 0.0f));
    sprite->setPosition3D(Vec3(0.0f, 15.0f, 0.0f));
    sprite->setCameraMask(2);
    this->addChild(sprite);

    std::vector<std::pair<Physics3DShape*, Mat4> > shapeList;
    {
        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);
        localTrans.m[12] = -1.5f; localTrans.m[13] = 2.5f; localTrans.m[14] = -2.5f;
        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);
        localTrans.m[12] = 2.0f; localTrans.m[13] = 2.5f; localTrans.m[14] = 1.f;
        shapeList.push_back(std::make_pair(rhandshape, localTrans));

        rbDes.mass = 10.0f;
        rbDes.shape = Physics3DShape::createCompoundShape(shapeList);
        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;
}