基于Irrlicht引擎的3D游戏实例v0.7
这一版实现了某大型游戏的第一人称视角UI,包括以下功能:
- 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 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;}
}
基于Irrlicht引擎的3D游戏实例v0.7相关推荐
- python扫雷的代码及原理_基于Python实现的扫雷游戏实例代码
摘要:这篇Python开发技术栏目下的"基于Python实现的扫雷游戏实例代码",介绍的技术点是"Python实现.Python.实例代码.扫雷游戏.扫雷.游戏" ...
- 如何用GameMakerStudio开发基于物理引擎的平台游戏 | Lynda教程 中文字幕
GameMakerStudio教程之如何用GML开发基于物理引擎的平台游戏 | Lynda教程 中文字幕 Building a Physics-Based Platformer in GameMake ...
- 基于Java的开源3D游戏引擎jMonkeyEngine
jMonkeyEngine简介 jMonkeyEngine是一款纯Java语言编写的游戏引擎,继承了Java应用跨平台的特性,而且是开放源代码的,遵循BSD开源协议,BSD开源协议用一句简单的话概括就 ...
- 3D游戏 java_基于Java的开源3D游戏引擎jMonkeyEngine
jMonkeyEngine简介 jMonkeyEngine是一款纯Java语言编写的游戏引擎,继承了Java应用跨平台的特性,而且是开放源代码的,遵循BSD开源协议,BSD开源协议用一句简单的话概括就 ...
- 从零到设计、建模和实现游戏引擎的3D游戏角色模型制作
从零到设计.建模和实现游戏引擎的游戏角色创建方法的分步指南. 1.. 概念艺术 首先,我们根据描述和风格参考创建粗略的概念艺术.在这个阶段,主要目标是创建角色的总体形状和感觉.所以它可以是黑白的. 如 ...
- 【转】探索基础元素---基于WebGL的H5 3D游戏引擎BabylonJS
原文地址:https://blog.csdn.net/AceWay/article/details/51472823 介绍 在本教程, 我们将学习如何使用Babylon.js创建基础元素, 比如盒子, ...
- python游戏脚本实例-基于Python实现的扫雷游戏实例代码
本文实例借鉴mvc模式,核心数据为model,维护1个矩阵,0表无雷,1表雷,-1表已经检测过. 本例使用python的tkinter做gui,由于没考虑可用性问题,因此UI比较难看,pygame更有 ...
- python扫雷代码文件_基于Python实现的扫雷游戏实例代码
# -*- coding: utf-8 -*- import random import sys from Tkinter import * class Model: ""&quo ...
- 基于Unity3d 引擎的Android游戏优化
更新不透明贴图的压缩格式为ETC 4bit,因为android市场的手机中的GPU有多种,每家的GPU支持不同的压缩格式,但他们都兼容ETC格式, 对于透明贴图,我们只能选择RGBA 16bit 或者 ...
最新文章
- 目前154万AI开发者
- 工业大数据分析平台的应用价值探讨
- 递归解决CSDN论坛上的小朋友分苹果问题
- 句柄 matlab_matlab 整车仿真
- iOS NSTextAttachment - 图文混排
- 宠粉老鱼皮带你 “入坑” GitHub!
- SQL COALESCE函数和NULL
- 国内流行的两大开源.net微信公众平台SDK对比分析
- 公交换乘 详解(C++)
- 日志查找常用命令-超实用
- 多个pdf怎么合并成一个pdf?如何将几个pdf文件合并成一个pdf?
- 浅析资产配置的几种方法
- fatal: You have not concluded your merge (MERGE_HEAD exists). Please, commit your changes before you
- GBASE 8C——SQL参考 5 全文检索
- python 围棋按照坐标查找棋子_python 实现围棋游戏(纯tkinter gui)
- 物联网毕设选题 - 单片机智能远程宠物喂养系统(物联网 esp8266 stm32)
- Oracle数据库,停止正在执行的package
- 牛客每日练习----​​​​​​​最长回文,Alice和Bob赌糖果,N阶汉诺塔变形
- html图片中绝对定位,CSS中绝对定位
- Amazon 云服务 名词解释