Collision Detection in FPS Game using three.js

Andreas Elia

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...

  • 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/

And I have also received a little advice on the three.js subreddit under the post...

"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.

steveOw

This jsfiddle0 (see end of answer for updated versions) is a hacked version of the one cited. 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). 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. 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. 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. 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. Use keys numpad_7 and numpad_9 to rotate left and right.

Edit (2) jsfiddle2 responds to multiple keys being pressed at the same time. Also the eddie cube is hidden by using eddie.visible = false.

Edit(3) jsfiddle3 Added independent camera rotation: up-centre-down (use Numpad keys 2,5,8). Player+camera horizontal rotation is by Numpad keys 4,6.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Collision Detection in FPS Game using three.js

From Dev

Problem with collision detection is JS canvas game

From Dev

Issue with collision detection Three.js

From Dev

Three.js collision detection optimization with raycasting

From Dev

Three.js collision detection issue

From Dev

Collision detection in javascript game

From Dev

Three.js - Accurate ray casting for collision detection

From Dev

Javascript Canvas Game - Collision Detection

From Dev

collision detection fabrics js

From Dev

Collision detection in java 2d game

From Dev

Collision detection in javascript canvas tanks game

From Dev

Java Game Physics - Determine intersection and collision detection

From Dev

Trying to use quadtrees for collision detection in a game

From Dev

Game score via collision detection increment

From Dev

Trying to use quadtrees for collision detection in a game

From Dev

Collision Detection for fast moving game object in Unity

From Dev

Experiencing something odd when using THREE.Raycaster for collision detection (r68)

From Dev

Using Angular for a game (60 fps)

From Dev

HTML Canvas game: 2D collision detection

From Dev

game viewport focus vs collision detection on y axis against gravity

From Dev

Collision detection in 2D game without tiles XNA

From Dev

if statement in JavaScript game not working as desired to allow for collision detection

From Dev

Phonegap game development collision detection regarding different screen sizes

From Dev

Collision detection in 2D game without tiles XNA

From Dev

game viewport focus vs collision detection on y axis against gravity

From Dev

rect collision detection d3js

From Dev

Paddle collision side detection using Graphics

From Dev

Collision Detection In Sprite Kit using Swift

From Dev

Collision detection using kineticJS (getIntersection function not working)

Related Related

  1. 1

    Collision Detection in FPS Game using three.js

  2. 2

    Problem with collision detection is JS canvas game

  3. 3

    Issue with collision detection Three.js

  4. 4

    Three.js collision detection optimization with raycasting

  5. 5

    Three.js collision detection issue

  6. 6

    Collision detection in javascript game

  7. 7

    Three.js - Accurate ray casting for collision detection

  8. 8

    Javascript Canvas Game - Collision Detection

  9. 9

    collision detection fabrics js

  10. 10

    Collision detection in java 2d game

  11. 11

    Collision detection in javascript canvas tanks game

  12. 12

    Java Game Physics - Determine intersection and collision detection

  13. 13

    Trying to use quadtrees for collision detection in a game

  14. 14

    Game score via collision detection increment

  15. 15

    Trying to use quadtrees for collision detection in a game

  16. 16

    Collision Detection for fast moving game object in Unity

  17. 17

    Experiencing something odd when using THREE.Raycaster for collision detection (r68)

  18. 18

    Using Angular for a game (60 fps)

  19. 19

    HTML Canvas game: 2D collision detection

  20. 20

    game viewport focus vs collision detection on y axis against gravity

  21. 21

    Collision detection in 2D game without tiles XNA

  22. 22

    if statement in JavaScript game not working as desired to allow for collision detection

  23. 23

    Phonegap game development collision detection regarding different screen sizes

  24. 24

    Collision detection in 2D game without tiles XNA

  25. 25

    game viewport focus vs collision detection on y axis against gravity

  26. 26

    rect collision detection d3js

  27. 27

    Paddle collision side detection using Graphics

  28. 28

    Collision Detection In Sprite Kit using Swift

  29. 29

    Collision detection using kineticJS (getIntersection function not working)

HotTag

Archive