# 3D Collision Box

I have tried to create a Collision box that would calculate if an variable is between two points and then set the variable outside it. Its simple in 2D but in 3D… Ahh!!! So I now wonder, cause I want a box that makes the x,y,z variables not to be in it, is there a more simple way to do this?

If you’re just looking for a box, go through the same procedure as in 2D to test if a box collides: position+width/2 >= point2-width/2 then…
You can do the same thing with the z coordinate. Sorry I don’t have to write an example.

Yeah, I know the box things in 2D, and I had luck when doing it in 3D, managed to stop a moving ellipse in both y and x. I plan to create a function where you set x,y,z and width,height and depht. But problem is that (maybe cause i am lazy) it is kind of much math.

``````function CreateColBox(x,y,z,w,h,d)
if Player.x>x and Player.x<x+w and Player.z>z and
Player.z<z+d and Player.y>y and Player.y<y+h then
print("Hit!")
end
end
``````

Now I have created a cube, but to create a real Collision box, I would have to calculate 6 cudes, each with a function that would keep the player out of the box. Now instead of this, is there another way?

Mmmmm… dave? (private joke )

I just want to show something that I tried, but failed on, here:

``````function Create:Wallx(x,y,z,w,h)
pushMatrix()
translate(x,y,z)
rotate(90,0,1,0)
rect(0,0,w,h)
popMatrix()
if Player.x<x+1 and Player.x>x-2 and
Player.y>y and Player.y<y+h and
Player.z<z and Player.z>z-w then
Player.x = Player.x + Player.speed
end
end
``````

What does ‘fail’ here?
I assume you mean the player is not correctly recognized as inside the box?
But what are the player coordinate value? How do you compute them?

Actually now did I see what i thought I did wrong. And yes, @Jmv38, there was nothing wrong, just me that made the player coordinates come outside the collision box, it wasnt recognized. The Player variables are: Player.x=100 and Player.y=50 and Player.z=-50. I added the speed of the player (Player.speed=2) to the Player.x every frame. I just instead of calculating every box in those 6 directions (x,y,z,-x,-y,-z if you know what i mean) I thought there would be an easier way to preventing the player to go through the wall. @Ignatz succeded to create a collision wall in his 3D scene. How did he manage to do that?

If you want to go beyond boxes, look here:
“Mathematics and Physics for Programmers” by John P. Flynt and Danny Kodicek Chapter 19
This is the best book to get formulas and learn how to do thinks. It’s more than worth what you pay for if you make complex games.
You learn how to test colliding worlds, footballs, boxes, cans, etc.