🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Collision Detection

Started by
4 comments, last by davidsporn 18 years ago
I need some help about collision detection, I was doing a project about 3D world walk through with First Person View, but i cannot find any sample of the collision coding, and i cannot understand what the nehe tutorial taugh, pls some1 help!!! Like: Run & Jump through a large 3D world featuring ramps, stairs, gaps and a moving hand made model [Edited by - nightangel on June 17, 2006 5:14:35 AM]
Advertisement
what type of collision detection do you want? plane collisions, box collisions, or bounding sphere collisions?

for bounding sphere collisions, first off i use this function:
double findDistance3d(double x1, double x2, double y1, double y2, double z1, double z2){       return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)) + ((z1 - z2) * (z1 - z2)));}

then to check for a collision, you just find out the radii of each object and use this code....
//object coordinatesfloat object1[3] = {5,5,5};float object2[3] = {-5,-5,-5};//we'll say that the radiuses of 2 objects are 3 and 4float combinedRadii = radius1 + radius2; //where radius1 = 3, radius2 = 4

now in your main loop you can just call this:
if(findDistance3d(object1[0],object2[0],object1[1],object2[1],object1[2],object2[2]) < combinedRadii){    //collision response}

since ive only made 2d games up to this point (i'm going to make a 3d game for my next game) i dont have any other 3d collision detection, but i do have a box collision 2d function, and i havent really looked into plane collisions too much. sorry....

hope that helps
--nathan
Thanks for nathan,
I want plane collisions and box collisions, since i'm doing a 3D world walk through, i create a hall include walls, stages, stairs, tables, chairs and computer.

and the example of bounding sphere collisions i read before, but that is for 2D right?

anyone can help?
My first problem is how to calculate...or how to know the Camera (Player) position? when find out the camera position, then how to know the camera collide with the table that i create.

I trying to use Function to create my table, e.g: GLvoid createTable(x,y,z,scalex,scaley,scalez), so inside my function need to include calboundingbox?
Quote: Original post by nightangel
My first problem is how to calculate...or how to know the Camera (Player) position? when find out the camera position, then how to know the camera collide with the table that i create.


Hummm, I don't know how you store your universe in memory, but I would have a map in a global coordinate system, storing the position of each entity (table, plane, player-aka-camera and so on, as long as it is appliable for a particular object)
With this referential data, you can check for collisions. For rendering, you compute a transformation matrix from the position and the orientation of the player and compute the objects coordinate relative to this local coordinate system, and then draw the scene.

Sorry for my bad english
for davidsporn, not really can understand what is that, what is global coordinate system? can show your coding and give some explanation?
it seen your idea can help on my project~ thanks!
I have no time now (at the office...), I'll post an answer later. However I have no actual code (for now I use 2D, but it's the same principles).
But the underlying principle is : choose your "point zero" ("ground zero"?), i.e. the point that will have [0,0,0] coordinate. Then express any coordinate relative to this point : objects and player. Then use the formula to compute the distance between player and objects (sqrt((Xplayer - Xobject)^2 + (Yplayer - Yobject)^2 + (Zplayer - Zobject)^2)).

This topic is closed to new replies.

Advertisement