摄像机平滑移动
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()));
相关文章推荐
- android 图片过大如何处理?压缩后图片有多大? 分辨率压缩、质量压缩
- HTC T328W刷机包 仿三星S5 UI美化 精简 S5落下
- python并行处理任务时 该用多进程?还是该用多线程?
- 《LINUX设备驱动》学习总结(基础篇)
- lua面向对象
- gcd
- jquery ajax参数加点号状态200进error
- Java调用net的webservice故障排除实战分享
- 说说 Objective-C 里的 @()
- find your present (2) 2095
- MySQL数据类型和常用字段属性总结
- BZOJ 4176 Lucas的数论 莫比乌斯反演
- [leedcode 42] Trapping Rain Water
- Android Studio开发JNI
- QT学习相关
- 利用mysql的inet_aton()和inet_ntoa()函数存储IP地址的方法分享
- DBeaver通用数据库工具
- 辨析nyoj42和hdu1116(欧拉图有向与无向区分)
- HTML各种引(尼)号(玛)的问题
- MFC创建多级目录