简体   繁体   English

使用three.js进行FPS游戏中的碰撞检测

[英]Collision Detection in FPS Game using three.js

I've been recently trying to fix my collision detection in my first person shooter three.js game but there are a few issues left I have not even the slightest idea on how to fix... 我最近一直试图在我的第一人称射击游戏three.js游戏中修复我的碰撞检测,但是还有一些问题我对如何修复没有任何想法......

  • The camera can view inside walls 相机可以查看内墙
  • The collisions regularly push the player outside of the map 碰撞会定期将玩家推到地图之外

I have a jsfiddle available here: http://jsfiddle.net/sxv5fwL4/95/ 我在这里有一个jsfiddle: http//jsfiddle.net/sxv5fwL4/95/

And I have also received a little advice on the three.js subreddit under the post... 而且我也收到了关于帖子下的three.js subreddit的一些建议......

"Use of the "stemkoski" collision detection?"

I thank you in advance for your time, and thanks to /u/stovenn for his help in my reddit post. 我提前感谢你的时间,感谢/ u / stovenn在我的reddit帖子中的帮助。

i think /u/stovenn talked about this git http://stemkoski.github.io/Three.js/ 我想/你/ stovenn谈到了这个git http://stemkoski.github.io/Three.js/

According to the thread How to detect collision in three.js? 根据线程如何检测three.js中的碰撞? in the stemkoski git is a working example for a collision detection in three.js 在stemkoski git中是一个在three.js中进行碰撞检测的工作示例

This jsfiddle0 (see end of answer for updated versions) is a hacked version of the one cited. 这个jsfiddle0 (参见最新版本的答案结尾)是被引用的黑客版本。 There was a problem with the Map which I have addressed by reading it as [u][v] and mapping Edit: (u-->z and v-->x). 通过将其读作[u] [v]并映射编辑:(u - > z和v - > x),我解决了Map的问题。 Now the layout of the map in the code is the same as the layout of the map in the simulation. 现在,代码中的地图布局与模拟中的地图布局相同。

I have used a very simple "(2D) Point in Bounding Box" collision test. 我使用了一个非常简单的“(2D)Point in Bounding Box”碰撞测试。 It is based on testing whether the proposed new position of the player at each animation step locates inside the x-range and z-range of a wall cube. 它基于测试每个动画步骤中玩家的建议新位置是否位于墙立方体的x范围和z范围内。 If it does then the proposed position is rejected and the (stored) previous position of the player is re-instated. 如果确实如此,则拒绝建议的位置并重新设置玩家的(存储的)先前位置。

Here is the core code:- 以下是核心代码: -

if (   tile_x_min <= player_pos_x && player_pos_x <= tile_x_max
    && tile_z_min <= player_pos_z && player_pos_z <= tile_z_max )                              
{
collision_flag = true;
player.velocity.x = 0;
player.velocity.z = 0; 
Message = "IN Wall Cell [" + x + "," + z +  "]" +
"(x:" + tile_pos_x + ", z:" + tile_pos_z + ")";                                           
}   

I have used a little helper cube ("eddie") at the player position and moved the camera back a bit to make it visible. 我在玩家位置使用了一个小帮手立方体(“eddie”)并将相机向后移动一点以使其可见。 This helped a lot in troubleshooting glitches. 这有助于解决故障。

Anyway give it a try if you like and let me know how it goes. 无论如何,如果你愿意,试试看,让我知道它是怎么回事。

Edit(1) jsfiddle1 Adds simple rotation of the player/camera. 编辑(1) jsfiddle1添加播放器/摄像机的简单旋转。 Use keys numpad_7 and numpad_9 to rotate left and right. 使用键numpad_7和numpad_9左右旋转。

Edit (2) jsfiddle2 responds to multiple keys being pressed at the same time. 编辑(2) jsfiddle2响应同时按下的多个键。 Also the eddie cube is hidden by using eddie.visible = false. 此外,使用eddie.visible = false隐藏了eddie立方体。

Edit(3) jsfiddle3 Added independent camera rotation: up-centre-down (use Numpad keys 2,5,8). 编辑(3) jsfiddle3增加了独立的摄像机旋转:向上中心向下(使用Numpad键2,5,8)。 Player+camera horizontal rotation is by Numpad keys 4,6. 玩家+摄像机水平旋转是由Numpad键4,6。

I am using Ammo.js , an emscripten port of the great Bullet Physics Library . 我正在使用Ammo.js ,这是一个伟大的Bullet物理库的emscripten端口。 This is a professional, open source, collection detection library. 这是一个专业的开源集合检测库。

Here is a sample I created: physics.autodesk.io 这是我创建的示例: physics.autodesk.io

Could be useful to somebody. 可能对某人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM