I am attempting to engineer a collision detection algorithm for a custom Minecraft client I'm making. Basically, the entire world is made up of cubes, and the player (or camera) needs to be able to stand on and move against these cubes. The result I want is illustrated in this image:
The green line is the player's movement vector. When the player is brushing up against a plane of one of the cubes, I want the vector to change to one that is perpendicular with the plane. The vector should, however, keep all of it's velocity in the plane's direction, yet lose all velocity towards the plane.
I hope I've made my question clear. What is the best and most efficient way to implement a collision detection system like this? Also, will a system like this allow for a simple gravity component?
EDIT: Forgot to mention, the cubes are stored in a three-dimensional array, [x,y,z].
A simple approach to implementing this would be to detect the collision of the ball and the plane. Calculate the penetration depth, this is how far the ball has actually gone past the plane, and push the ball back in the direction of the plane's normal.
This will have the effect of putting the ball on the surface of the plane. If you do this for each frame, the ball will effectively slide along the plane, assuming of course that the ball's velocity vector is not not parallel to the plane's normal.
The field of collision detection is big and complex, and depending on your game you have to determine what is sufficient for your requirements in terms of the level of realism you require and the performance requirements. You should always go for the simplest solution that give a realistic enough feedback, depending on the game it often does not have to be perfect.
Basically you should break your collision detection into 2 phases typically known as broad phase and narrow phase.
The broad phase could be as simple as perform a quick bounding box check to determine potential collisions and then submit those potential collision to the narrow phase collision detection to do the more detailed checks where you determine if there really was a collision and the collision depth. If you have many objects then the broad phase might use some kind of quadtree indexing to select only the blocks in the vicinity of your object to perform the collision detection against.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With