学习记录

摄像机平滑移动

摄像机移动大概的思路:/*
         场景新位置 = 场景当前位置 *(1-smooth) + 场景目标位置*smooth
         每一帧 都调用这个 公式 修正 场景的位置即可
         smooth 是0 -1 的数字 用于 控制场景移动的 光滑程度
         */
//更新镜头
        Vec2 vec2 = GetCameraPostion(_pCameraTarget->getPosition());
        float smooth = 0.1f;
        Vec2 newPos = getPosition()*(1-smooth) + vec2*smooth;



Unity3d 中得摄像机移动很不错,实现在:

Vector3::SmoothDamp

我把Vector3搬到了c2dx中


使用实例:

//===================================
    float damping = 0.1;
    float lookAheadFactor = 3;
    float lookAheadReturnSpeed = 680.5f;
    float lookAheadMoveThreshold = 0.1f;
    
    float m_OffsetZ;
    Vector3 m_LastTargetPosition;
    Vector3 m_CurrentVelocity;
    Vector3 m_LookAheadPos;

<span style="font-family: Arial, Helvetica, sans-serif;">//目的地位置</span>
Vec2 vec2 = GetCameraPostion(_pCameraTarget->getPosition());
        Vector3 position = Vector3::ConvertVec2(vec2);
        
        if (getPosition().x > position.x) {
            m_CurrentVelocity.x = -GM()->m_fCameraSpeed;
            if (getPosition().x + m_CurrentVelocity.x < position.x) {
                m_CurrentVelocity.x = position.x - getPosition().x;
            }
        }
        else
        {
            m_CurrentVelocity.x = GM()->m_fCameraSpeed;
            if (getPosition().x + m_CurrentVelocity.x > position.x) {
                m_CurrentVelocity.x = position.x - getPosition().x;
            }
        }
        if (getPosition().y > position.y) {
            m_CurrentVelocity.y = -GM()->m_fCameraSpeed;
            if (getPosition().y + m_CurrentVelocity.y < position.y) {
                m_CurrentVelocity.y = position.y - getPosition().y;
            }
        }
        else
        {
            m_CurrentVelocity.y = GM()->m_fCameraSpeed;
            if (getPosition().y + m_CurrentVelocity.y > position.y) {
                m_CurrentVelocity.y = position.y - getPosition().y;
            }
        }
        
        // only update lookahead pos if accelerating or changed direction
        float xMoveDelta = (position - m_LastTargetPosition).x;
        
        bool updateLookAheadTarget = abs(xMoveDelta) > lookAheadMoveThreshold;
        
        if (updateLookAheadTarget)
        {
            m_LookAheadPos = lookAheadFactor*Vector3::right()*signbit(xMoveDelta);
        }
        else
        {
            
            m_LookAheadPos = Vector3::MoveTowards(m_LookAheadPos, Vector3::ZERO, dt*lookAheadReturnSpeed);
        }
        Vector3 transformPos = Vector3::ConvertVec2(getPosition());
        
        Vector3 aheadTargetPos = position;// + m_LookAheadPos + Vector3()*m_OffsetZ;
        Vector3 newPos = Vector3::SmoothDamp(transformPos, aheadTargetPos,m_CurrentVelocity, damping,GM()->m_fCameraSpeed*10,dt);
//        log("newPos(%f,%f,%f)",newPos.x,newPos.y,newPos.z);
        float speedX = newPos.x - getPositionX();
        float speedY = newPos.y - getPositionY();
        //更新镜头
        setPosition(Vec2(newPos.x, newPos.y));
      
        //记录上一个目标位置  
        m_LastTargetPosition = Vector3::ConvertVec2(GetCameraPostion(_pCameraTarget->getPosition()));



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yanjunmu/article/details/46819933
个人分类: C++ cocos2d-x
上一篇判断两条线段是否相交(三种算法)
下一篇将Texture Packer制作的.pvr.ccz和.plist文件还原为多个原图
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭