您的位置:首页 > 其它

摄像机平滑移动

2015-07-10 18:26 573 查看
摄像机移动大概的思路:/*
场景新位置 = 场景当前位置 *(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()));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: