Ejemplo n.º 1
0
//Throwing Object
void HelloWorld::shootBoxfunc(const cocos2d::Vec3 &des)
{
	//Vec3 linearVel = des -camera->getPosition3D();
	//linearVel.normalize();
	//linearVel *= 100.0f;

	Physics3DRigidBodyDes rbDes;
	rbDes.originalTransform.translate(car_cabine->getPosition3D());
	rbDes.mass = 1.f;
	rbDes.shape = Physics3DShape::createSphere(0.25);
	auto sprite = PhysicsSprite3D::create("box.c3t", &rbDes);
	sprite->setTexture("Gun.png");

	auto rigidBody = static_cast<Physics3DRigidBody*>(sprite->getPhysicsObj());
	rigidBody->setLinearFactor(Vec3::ONE);
	rigidBody->setLinearVelocity(des*15);
	rigidBody->setAngularVelocity(Vec3::ZERO);
	rigidBody->setCcdMotionThreshold(0.5f);
	rigidBody->setCcdSweptSphereRadius(0.4f);

	sprite->setPosition3D(Vec3(car_cabine->getPosition3D().x, car_cabine->getPosition3D().y+4, car_cabine->getPosition3D().z));
	sprite->setScale(0.5f);
	sprite->syncNodeToPhysics();
	sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); 
	sprite->setCameraMask((unsigned short)CameraFlag::USER2);
	this->addChild(sprite,1);
}
Ejemplo n.º 2
0
void Physics3DTestDemo::shootBox( const cocos2d::Vec3 &des )
{
    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();
    
    //optimize, only sync node to physics
    sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE); //sync node to physics
    
    sprite->setCameraMask((unsigned short)CameraFlag::USER1);
}
Ejemplo n.º 3
0
PhysicsRigidBody* PhysicsMgr::createRigidBodyMesh(Mesh* mesh, Matrix44* transform)
{
		auto rig = new PhysicsRigidBody();
		btScalar	mass(0.0f);
		btTransform startTransform;
		btTriangleMesh * meshInterface = new btTriangleMesh();
		auto idxSize = mesh->getIndicesSize();
		for(auto i =0; i < idxSize; i+=3)
		{
			auto v1 = mesh->getVertex(i);
			auto v2 = mesh->getVertex(i + 1);
			auto v3 = mesh->getVertex(i + 2);
			meshInterface->addTriangle(btVector3(v1.m_pos.x, v1.m_pos.y, v1.m_pos.z), btVector3(v2.m_pos.x, v2.m_pos.y, v2.m_pos.z), btVector3(v3.m_pos.x, v3.m_pos.y, v3.m_pos.z));
		}
		auto tetraShape = new btBvhTriangleMeshShape(meshInterface, true, true);

		startTransform.setIdentity();
		if (transform)
		{
			startTransform.setFromOpenGLMatrix(transform->data());
		}
		auto btRig = shared()->createRigidBodyInternal(mass, startTransform, tetraShape, btVector4(1, 0, 0, 1));
		btRig->setContactProcessingThreshold(BT_LARGE_FLOAT);
		btRig->setFriction (btScalar(0.9));
		btRig->setCcdMotionThreshold(.1);
		btRig->setCcdSweptSphereRadius(0);
		rig->setRigidBody(btRig);
		rig->genUserIndex();
		btRig->setUserIndex(rig->userIndex());
		btRig->setUserPointer(rig);
		return rig;
}
Ejemplo n.º 4
0
bool Physics3DCollider::init(Physics3DColliderDes *info)
{
    _physics3DShape = info->shape;
    _physics3DShape->retain();
    _btGhostObject = new btCollider(this);
    _btGhostObject->setCollisionShape(_physics3DShape->getbtShape());
    
    setTrigger(info->isTrigger);
    setFriction(info->friction);
    setRollingFriction(info->rollingFriction);
    setRestitution(info->restitution);
    setHitFraction(info->hitFraction);
    setCcdSweptSphereRadius(info->ccdSweptSphereRadius);
    setCcdMotionThreshold(info->ccdMotionThreshold);
    
    _type = Physics3DObject::PhysicsObjType::COLLIDER;
    return true;
}