Hello Guest

# Collision Detection

• 8 Replies
• 16636 Views

#### Klinenator

• 17
##### Collision Detection
« on: November 11, 2010, 01:11:13 »
Im having a rought time with collison detection.

I have an array that is 50X50X50 and i fill it with number if im supposed to draw a cube at that position.

The problem im running into is im checking if i walk into a cube by checking my current xyz against the array to see if there is something there and if there is i move back the same distance i moved forward. That puts me right at the edge of the cube and since the position numbers are floats sometimes i can see into the cube.

#### BatKid

• 45
##### Re: Collision Detection
« Reply #1 on: November 11, 2010, 01:44:46 »
In my experience, these issues are always difficult and implementation dependent, especially when it comes to camera positions.  Maybe you can make your cube smaller but keep the boundary condition the same?

Projects:
Env3D (http://env3d.org): Learn Java in 3D
WhaleChat (http://whalechat.com): A 3D social programming experiment

#### Klinenator

• 17
##### Re: Collision Detection
« Reply #2 on: November 11, 2010, 01:47:02 »
Well then two cubes next to each other wouldnt be touching

Im toying with the idea of saying dont even bother checking for an intersection unless one of the positions x,y,z is a whole number because in a square world if youre not a whole number you can possible be intersecting

#### BatKid

• 45
##### Re: Collision Detection
« Reply #3 on: November 11, 2010, 01:55:49 »
what kind of a camera system are you using?  First person?  or Third person?
Projects:
Env3D (http://env3d.org): Learn Java in 3D
WhaleChat (http://whalechat.com): A 3D social programming experiment

#### Klinenator

• 17
##### Re: Collision Detection
« Reply #4 on: November 11, 2010, 02:05:26 »
First person. Think same concept as minecraft for everything

Im trying to make a minecraft type game but based much more on survival than mining

#### BatKid

• 45
##### Re: Collision Detection
« Reply #5 on: November 13, 2010, 07:09:58 »
I had a few moments today and started thinking about your approach, I'm not sure if I understand what you are trying to do completely, so I thought I'd try to implement something like this:

First, I use a 2 dimensional array to indicate if a location is occupied by an object.  Then, as my camera moves around, I want the camera to "collide" with the objects using the camera's location as an index into the 2 dimensional array.

This approach would work fine and is very fast also but the camera would be right at the edge of the object.  What you are missing is that the camera also has a size.

The simplest approach is to perform some kind of collision detection between the object and the camera, but you want to avoid that.  Well, in this case, you must use your camera's location as a center point and compare all 8 objects around your camera in the 2 dimensional case, I have this in my code:

Code: [Select]
`                if (map[x-1][z+1] != null || map[x][z+1] != null || map[x+1][z+1] != null ||                    map[x-1][z] != null || map[x][z] != null || map[x+1][z] != null ||                    map[x-1][z-1] != null || map[x][z-1] != null || map[x+1][z-1] != null) {                        // Collision detection here`
This way, you detect your collision much sooner.  I had a little fun with this and created the following:

http://whalechat.com/ballcraft_demo/

You can freely add and remove spheres on the plane using the space bar and the 'R' key.

Hope this helps.
Projects:
Env3D (http://env3d.org): Learn Java in 3D
WhaleChat (http://whalechat.com): A 3D social programming experiment

#### Klinenator

• 17
##### Re: Collision Detection
« Reply #6 on: November 13, 2010, 21:32:46 »
Ive tried that but 1 unit away is a little too far from the box which sucks. I think being right up against the unit is alright i just have to make it so that if you are up against something and you press forward and left or forward and right it just goes left or just goes right

#### BatKid

• 45
##### Re: Collision Detection
« Reply #7 on: November 13, 2010, 23:12:17 »
To get it more precise, you can add additional logic inside the body of the if condition -- simply use a more accurate algorithm like bounding box or bounding sphere on the 8 potential objects.
Projects:
Env3D (http://env3d.org): Learn Java in 3D
WhaleChat (http://whalechat.com): A 3D social programming experiment

#### ferrythomas

• 1
• All is well
##### Re: Collision Detection
« Reply #8 on: November 18, 2010, 08:57:48 »

Collision Detection,  its very confusing