摄像机平滑移动

摄像机移动大概的思路:/*
         场景新位置 = 场景当前位置 *(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()));



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值