基于Irrlicht引擎的3D游戏实例v0.7
2015-10-24 20:38
295 查看
这一版实现了某大型游戏的第一人称视角UI,包括以下功能:
- WS前进后退
- AD左右转向
- 鼠标左键控制视角旋转
- 鼠标右键控制物体旋转
- 鼠标滚轮控制视距
还加入了简单爆炸效果和音效播放。
附上第一人称视角主要代码:
- WS前进后退
- AD左右转向
- 鼠标左键控制视角旋转
- 鼠标右键控制物体旋转
- 鼠标滚轮控制视距
还加入了简单爆炸效果和音效播放。
附上第一人称视角主要代码:
//! animates a scene node void CSceneNodeAnimatorMagicFPS::animateNode(ISceneNode* node, u32 timeMs) { bool bTargetNeedUpdate = false; bool bRotNeedUpdate = false; if (!node) return; if(FirstUpdate) { if (CursorControl ) { CursorControl->setPosition(0.5f, 0.5f); CursorPos = LastCursorPos = CursorControl->getRelativePosition(); } LastAnimationTime = StartTime; FirstUpdate = false; } u32 diffTime = timeMs - LastAnimationTime; if (diffTime != 0) { if(bWindowActive) animateMouseMove(); updateRotateSpeed(); core::vector3df oldObjRot = node->getRotation(); core::vector3df newObjRot = oldObjRot; core::vector3df oldObjPos = node->getPosition(); core::vector3df newObjPos = oldObjPos; if( MovingRotateSpeed != core::vector3df(0,0,0) ) { newObjRot += MovingRotateSpeed; newObjRot = rotationMod(newObjRot); node->setRotation(newObjRot); updateDirection(newObjRot); } updateMovingStraightDiret(); core::vector3df rel = MovingStraightDiret * diffTime * StraightSpeed; newObjPos += rel; node->setPosition(newObjPos); // move the camema, current version doesn't change 'Y' if(Camera) { core::vector3df oldCameraPos = Camera->getPosition(); core::vector3df newCameraPos = oldCameraPos; core::vector3df oldCameraRot = Camera->getRotation(); core::vector3df newCameraRot = oldCameraRot; core::vector3df oldTarget = Camera->getTarget(); core::vector3df newTarget = oldTarget; { bTargetNeedUpdate = true; bRotNeedUpdate = true; //! update camera position /* if( !KeyMouseMagicStatus.S_mouse_down_right ) { if(newObjPos.Y > -90 && newObjRot.Y < 90) { newCameraPos.X = newObjPos.X - Distance * sin(newObjRot.Y / 180.0 * M_PI); newCameraPos.Z = newObjPos.Z - Distance * cos(newObjRot.Y / 180.0 * M_PI); } else { newCameraPos.X = newObjPos.X - Distance * sin((180.0 - newObjRot.Y) / 180.0 * M_PI); newCameraPos.Z = newObjPos.Z + Distance * cos((180.0 - newObjRot.Y) / 180.0 * M_PI); } newCameraPos.Y = newObjPos.Y;// + Hight; //deleted 2015/02/16: hight is not nessary for FPS //? why the point is shaking? because target is not set properly Camera->setPosition(newCameraPos); } else */ if( MovingMagicStatus.S_camara_move_with_mouse ) { //! right click to reset position and rotation and target // first, get the rotation of mouse moving core::vector3df mouseRot; mouseRot.Y = DiffCursorPos.X * 100; // TODO:Change speed By user input mouseRot.X = DiffCursorPos.Y * 100; // TODO:Change speed By user input // fix bug: up/down is also controled by Z mouseRot.Z = DiffCursorPos.Y * 100; // TODO:Change speed By user input f32 diffR = DiffCursorPos.Y * (-2); // TODO:Change speed By user input // fix bug: when up/down, the rotation is changed with face or back of obj and camera f32 judgeDirection = ( oldObjPos.X - oldCameraPos.X ) * Direction.X + (oldObjPos.Z - oldCameraPos.Z) * Direction.Z; if( judgeDirection > 0 ) { // the same direction mouseRot.X *= -1; mouseRot.Z *= -1; } else if( judgeDirection == 0) { // 90 degree // !!!!!!!!!!! TODO !!!!!!!!!!!! why the speed near 90 degree is 0? } else { // the negtive direction } // second, update the vector core::vector3df oldVectorTC = oldCameraPos - oldObjPos; core::vector3df newVectorTC = oldVectorTC; //! magic algorithm to caculate x,y,z { f32 sin_r0 = oldVectorTC.Y / Distance; f32 cos_r0 = sqrt( 1 - sin_r0 * sin_r0 ); f32 cos_r = cos( diffR ); f32 sin_r = sin( diffR ); newVectorTC.Y = Distance*( sin_r0*cos_r + cos_r0*sin_r ); f32 alpha; if( cos_r0 == 0 ) { alpha = cos_r; } else { alpha = cos_r - (sin_r0*sin_r)/cos_r0; } newVectorTC.X = alpha*oldVectorTC.X; newVectorTC.Z = alpha*oldVectorTC.Z; newVectorTC.rotateXZBy( mouseRot.Y ); } core::vector3df midPoint = (oldVectorTC+newVectorTC)/2; f32 smallDis = Distance / 20; if( ( midPoint.Y - Distance >=0 && midPoint.Y - Distance < smallDis ) || ( midPoint.Y - Distance <=0 && midPoint.Y - Distance > -smallDis ) || ( midPoint.Y + Distance >=0 && midPoint.Y + Distance < smallDis ) || ( midPoint.Y + Distance <=0 && midPoint.Y + Distance > -smallDis ) || ( newVectorTC.Y - Distance >=0 && newVectorTC.Y - Distance < smallDis ) || ( newVectorTC.Y - Distance <=0 && newVectorTC.Y - Distance > -smallDis ) || ( newVectorTC.Y + Distance >=0 && newVectorTC.Y + Distance < smallDis ) || ( newVectorTC.Y + Distance <=0 && newVectorTC.Y + Distance > -smallDis ) ) { // deal with 90 degree UP/DOWN newVectorTC = oldVectorTC; newVectorTC.rotateXZBy( mouseRot.Y ); } // third, update the pos resetCameraPosition(node, newVectorTC); if( KeyMouseMagicStatus.S_mouse_down_right && ( DiffCursorPos.X != 0 || DiffCursorPos.Y != 0 ) ) { // reset the rotation to match the camara resetObjRotation(node); } bTargetNeedUpdate = false; bRotNeedUpdate = false; } else if( MovingStraightDiret != core::vector3df(0,0,0) || MovingRotateSpeed != core::vector3df(0,0,0) ) { core::vector3df newVectorTC = core::vector3df(oldCameraPos.X - oldObjPos.X,oldCameraPos.Y - oldObjPos.Y,oldCameraPos.Z - oldObjPos.Z); if( MovingRotateSpeed != core::vector3df(0,0,0) ) { newVectorTC.rotateXZBy( -MovingRotateSpeed.Y ); newVectorTC.rotateYZBy( MovingRotateSpeed.X ); } resetCameraPosition(node, newVectorTC); } else if( MovingMagicStatus.S_distance_changed ) { core::vector3df oldVectorTC = oldCameraPos - oldObjPos; resetCameraPosition(node, oldVectorTC); MovingMagicStatus.S_distance_changed = false; } //! update camera rotation } } LastAnimationTime = timeMs; } }
相关文章推荐
- LightOJ 1148 - Mad Counting【水题+规律】
- 10.24杂项
- 不可思议的闪电奇缘
- javascript 实现table展开折叠
- 扩展欧几里德算法
- 数组中重复次数最多的元素
- hdu5496
- Codeforces 588 C. Duff and Weight Lifting
- C#开发中使用Npoi操作excel实例代码
- hdu5496
- php引用传值
- android 控件不同状态 selector----drawable
- 安卓软件第一次打开时引导界面以及打开后停留界面
- php.ini 核心配置选项说明
- 执行shell脚本方法总结
- C++有符号和无符号之间的转换
- 把eclipse大括号向VS一样上下对齐
- 学习笔记三:类和数据抽象
- 解析jQuery的链式编程
- ASP导出为Word或Excel的最简单方法