コード例 #1
0
void Physics3DConstraintDemo::onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event  *event)
{
    //ray trace
    if(_camera)
    {
        auto touch = touches[0];
        auto location = touch->getLocationInView();
        Vec3 nearP(location.x, location.y, 0.0f), farP(location.x, location.y, 1.0f);
        
        auto size = Director::getInstance()->getWinSize();
        _camera->unproject(size, &nearP, &nearP);
        _camera->unproject(size, &farP, &farP);
        
        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);
            
            _constraint = Physics3DPointToPointConstraint::create(static_cast<Physics3DRigidBody*>(result.hitObj), position);
            physicsScene->getPhysics3DWorld()->addPhysics3DConstraint(_constraint, true);
            _pickingDistance = (result.hitPosition - nearP).length();
            return;
        }
    }
    Physics3DTestDemo::onTouchesBegan(touches, event);
    _needShootBox = false;
}
コード例 #2
0
ファイル: MainLayer.cpp プロジェクト: wakqaz4/Bomber
void MainLayer::onSingleClick()
{
	log("on single click");
	Global *global = Global::getInstance();
	Vec3 nearP(_singleClickLocation.x, _singleClickLocation.y, 0.0f);
	Vec3 farP(_singleClickLocation.x, _singleClickLocation.y, 1.0f);
	auto size = Director::getInstance()->getWinSize();
	global->_camera->unproject(size, &nearP, &nearP);
	global->_camera->unproject(size, &farP, &farP);
	Physics3DWorld::HitResult result;
	global->_physics3DWorld->rayCast(nearP, farP, &result);
	global->_playerObj->moveAgent(result.hitPosition);
}
コード例 #3
0
void Physics3DTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event  *event)
{
    if (!_needShootBox) return;
    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);
    }
}
コード例 #4
0
void Physics3DConstraintDemo::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event  *event)
{
    if (_constraint)
    {
        auto p2pConstraint = ((Physics3DPointToPointConstraint*)_constraint);
        
        auto touch = touches[0];
        auto location = touch->getLocationInView();
        Vec3 nearP(location.x, location.y, 0.0f), farP(location.x, location.y, 1.0f);
        
        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);
        return;
    }
    Physics3DTestDemo::onTouchesMoved(touches, event);
}
コード例 #5
0
void ABBTest::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
{
    for (auto touch: touches)
    {
        auto location = touch->getLocationInView();
        Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
        auto size = Director::getInstance()->getWinSize();
        _camera->unproject(size, &nearP, &nearP);
        _camera->unproject(size, &farP, &farP);
        
        Vec3 dir(farP - nearP);
        dir.normalize();
        Ray ray(_camera->getPosition3D(), dir);
        
        if(ray.intersects(_aabb))
        {
            _pick = true;
            return;
        }
    }
    _pick = false;
}
コード例 #6
0
ファイル: Camera3DTest.cpp プロジェクト: QC-git/MyLab
void Camera3DTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event  *event)
{
    for ( auto &item: touches )
    {
        auto touch = item;
        auto location = touch->getLocationInView();
        if(_camera)
        {
            if(_sprite3D && _cameraType==CameraType::ThirdPerson && _bZoomOut == false && _bZoomIn == false && _bRotateLeft == false && _bRotateRight == false)
            {
                Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
                
                auto size = Director::getInstance()->getWinSize();
                nearP = _camera->unproject(nearP);
                farP = _camera->unproject(farP);
                Vec3 dir(farP - nearP);
                float dist=0.0f;
                float ndd = Vec3::dot(Vec3(0,1,0),dir);
                if(ndd == 0)
                    dist=0.0f;
                float ndo = Vec3::dot(Vec3(0,1,0),nearP);
                dist= (0 - ndo) / ndd;
                Vec3 p =   nearP + dist *  dir;
                
                if( p.x > 100)
                    p.x = 100;
                if( p.x < -100)
                    p.x = -100;
                if( p.z > 100)
                    p.z = 100;
                if( p.z < -100)
                    p.z = -100;
                
                _targetPos=p;
            }
        }
    }
}
コード例 #7
0
bool HelloWorld::isTouchElement( Touch *touch )
{
#if 0
    _drawNode->clear();
#endif
    std::vector<std::pair<int, Sprite3D *>> sortedElements;
    for (int i = 0; i < MAX_CAPACITY_NUM; ++i)
    {
        if (_elements[i])
        {
            Vec3 posInView;
            _camera->getViewMatrix().transformPoint(_elements[i]->getPosition3D(), &posInView);
            _elements[i]->setGlobalZOrder(-posInView.z);
            sortedElements.push_back(std::pair<int, Sprite3D *>(i, _elements[i]));
        }
    }

    std::sort(sortedElements.begin(), sortedElements.end(), [](std::pair<int, Sprite3D *> left, std::pair<int, Sprite3D *> right){
        return left.second->getGlobalZOrder() < right.second->getGlobalZOrder();
    });

    bool isTouch = false;
    for (auto iter : sortedElements)
    {
#if 0
        const AABB &degaabb = iter.second->getAABB();
        Vec3 degcorners[8];
        degaabb.getCorners(degcorners);
        _drawNode->drawCube(degcorners, Color4F(0.0f, 1.0f, 0.0f, 1.0f));
#endif

        auto location = touch->getLocationInView();
        Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
        auto size = Director::getInstance()->getWinSize();
        _camera->unproject(size, &nearP, &nearP);
        _camera->unproject(size, &farP, &farP);
        auto ray = Ray(nearP, (farP - nearP).getNormalized());
        const AABB &aabb = iter.second->getAABB();
        if (ray.intersects(aabb))
        {
            if (iter.first != _touchElements[0])
            {
                _elements[iter.first]->setColor(Color3B(255, 0, 0));
                _touchElements[0] == TOUCH_NONE? _touchElements[0] = iter.first: _touchElements[1] = iter.first;
                isTouch = true;
            }
            break;
        }
    }

    if (_touchElements[0] != TOUCH_NONE && _touchElements[1] != TOUCH_NONE)
    {
        if (checkNeedEliminate())
        {
            _elements[_touchElements[0]]->getParent()->removeChild(_elements[_touchElements[0]]);
            _elements[_touchElements[1]]->getParent()->removeChild(_elements[_touchElements[1]]);
            _elements[_touchElements[0]] = _elements[_touchElements[1]] = nullptr;
            //_drawNode->clear();
        }
        else
        {
            _elements[_touchElements[0]]->setColor(Color3B(255, 255, 255));
            _elements[_touchElements[1]]->setColor(Color3B(255, 255, 255));
        }
        _touchElements[0] = _touchElements[1] = TOUCH_NONE;
    }

    return isTouch;
}