摄像机平滑移动

原创 2015年07月10日 18:26:13

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



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Unity 3D--摄像机平滑跟随(方法一)

using UnityEngine; using System.Collections; public class SmoothFollowerObj : MonoBehaviour { ...

在Unity3D中如何让摄像机进行平滑的透视(perspective)和正视(orthographic)角度变换(就像编辑器中点击Gizmos一样的效果)

在Unity3D中如何让摄像机进行平滑的透视(perspective)和正视(orthographic)角度变换(就像编辑器中点击Gizmos一样的效果) How to make a smooth ...

Unity3D 摄像机平滑跟随角色

摄像机平滑跟随角色移动的解决方案

Unity3d摄像机的平滑过渡

#pragma strict var cam01:GameObject; var cam01Pos:GameObject; var cam02Pos:GameObject; // 用于判断摄像...

让图象平滑移动无闪烁感

  • 2004-05-28 00:00
  • 181KB
  • 下载

Unity3D摄像机跟随物体移动的代码控制

摄像机跟随物体方法一是把摄像机设置为物体Player的子物体,给Player添加移动脚步就可以摄像机跟随Player移动。移动的简单脚步using UnityEngine; using System....

Unity3D让摄像机随player移动

代码很简单直接看: using UnityEngine; using System.Collections; public class CameraController : MonoBehavio...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)