Collision detection by sliding relative to the plane in XNA

I am trying to develop a collision detection algorithm for the Minecraft user client that I am doing. In principle, the whole world consists of cubes, and the player (or camera) must be able to stand and move against these cubes. The result that I want is illustrated in this image: image

The green line is the player’s movement vector. When a player is brushing against the plane of one of the cubes, I want the vector to change to one that is perpendicular to the plane. However, the vector must maintain all its speed in the direction of the plane, but still lose all speed to the plane.

I hope I put my question clear. What is the best and most efficient way to implement a collision detection system? Also, would such a system allow a simple component of gravity?

EDIT: cubes are saved in the three-dimensional array [x, y, z].

+6
c # xna collision-detection
source share
2 answers

A simple approach to implementing this would be to detect collisions between a ball and a plane. Calculate the penetration depth, this is how far the ball flew past the plane and pushed the ball back in the plane direction.

This will lead to the position of the ball on the surface of the plane. If you do this for each frame, the ball will effectively glide across the plane, assuming, of course, that the ball's velocity vector is not parallel to the normal plane.

The collision detection field is large and complex, and depending on your game, you must determine what is sufficient for your requirements in terms of the required level of realism and performance requirements. You should always go for the simplest solution, which gives a fairly realistic feedback, depending on the game, which often does not have to be perfect.

Basically, you should break your collision detection into 2 phases, commonly called wide phase and narrow phase.

The wide phase can be as simple as performing a quick check of the bounding box to identify potential collisions, and then sending these potential collisions to a narrow collision detection to do more detailed checks where you determine if the collision really happened and the collision depth. If you have many objects, then the wide phase can use some kind of quadrant indexing to select only blocks near your object to perform collision detection.

+1
source share

In the world of Axis Aligned cubes, it is very simple. It is easy to think that you need something complicated, but in fact it is really simple. This is from experience after writing my own clone clone.

Here's how:

position.X += velocity.X; if(colliding()) position.X -= velocity.X; position.Y += velocity.Y; if(colliding()) position.Y -= velocity.Y; position.Z += velocity.Z; if(colliding()) position.Z -= velocity.Z; 

Here is the code to find out if you come across or not:

 bool colliding() { int minX = Position.X - size.X / 2; int minY = Position.Y - size.Y / 2; int minZ = Position.Z - size.Z / 2; int maxX = Position.X + size.X / 2; int maxY = Position.Y + size.Y / 2; int maxZ = Position.Z + size.Z / 2; for (int x = minX; x <= maxX; x++) for (int y = minY; y <= maxY; y++) for (int z = minZ; z <= maxZ; z++) { if(blockType[x, y, z] != 0) return true; } return false; } 
0
source share

All Articles