基于Irrlicht引擎的3D游戏实例v0.7

来源:互联网 发布:管家婆软件那三个 编辑:程序博客网 时间:2024/05/16 05:34

这一版实现了某大型游戏的第一人称视角UI,包括以下功能:


- WS前进后退

- AD左右转向

- 鼠标左键控制视角旋转

- 鼠标右键控制物体旋转

- 鼠标滚轮控制视距


还加入了简单爆炸效果和音效播放。


附上第一人称视角主要代码:

//! animates a scene nodevoid 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 properlyCamera->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 movingcore::vector3df mouseRot;mouseRot.Y = DiffCursorPos.X * 100; // TODO:Change speed By user inputmouseRot.X = DiffCursorPos.Y * 100; // TODO:Change speed By user input// fix bug: up/down is also controled by ZmouseRot.Z = DiffCursorPos.Y * 100; // TODO:Change speed By user inputf32 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 cameraf32 judgeDirection = ( oldObjPos.X - oldCameraPos.X ) * Direction.X + (oldObjPos.Z - oldCameraPos.Z) * Direction.Z;if( judgeDirection > 0 ){// the same directionmouseRot.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 vectorcore::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/DOWNnewVectorTC = oldVectorTC;newVectorTC.rotateXZBy( mouseRot.Y );}// third, update the posresetCameraPosition(node, newVectorTC);if( KeyMouseMagicStatus.S_mouse_down_right && ( DiffCursorPos.X != 0 || DiffCursorPos.Y != 0 ) ){// reset the rotation to match the camararesetObjRotation(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;}}


0 0
原创粉丝点击