Jump to content
• Advertisement

# OpenGL collision for the camrea? Last requst for my game!

This topic is 4158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Ok, I got everything from the feet to the torso for my game. Now I just need to make the head which is the collision detection for my camrea! So how do I have it so all of my walls A.K.A (OpenGL objects) have collision so the player A.K.A (camera) cannot go through walls! Please: Do not suggest Open source codes I will not know what I would be looking for exactly! Not unless you will be happy to direct me to what I should look at in the file. [Edited by - ajm113 on July 25, 2007 1:07:34 PM]

#### Share this post

##### Share on other sites
Advertisement
I suppose that you just need to do ray/objects intersection (with the bounding boxes, I would say). For each frame you shoot a ray from the camera position into the scene, perform ray/object intersection with the geometry (applying culling eventually) and calculating the first intersection (that is the object closest to the camera). If the distance is under a certain value, that depend on the units you use in your game, you prevent the camera from moving further in that direction. You may take advantage of temporal coherence and say, for example, that if the first intersection in 5 meters away and the camera is moving, let's say, at 1meter/sec, then for 4 seconds you won't need to perform the test again.
I'm not an expert on this subject, so there may be better way to do this, nor I know the implementation details for this in a real time application.

#### Share this post

##### Share on other sites
Ok, thanks for you post. Good info, aren't their any other help? I'm pretty sure that people know this pretty well from what I see from people that go on here.

#### Share this post

##### Share on other sites
Unfortunately I cannot point you toward internet pages or even books. I would say that the first step would be setting up the collision between a ray and the geometry. There is plenty of informations about this topic, and a lot of different algorithms (at least 4-5 from the top of my head)...

#### Share this post

##### Share on other sites
Can't you just PM me sources then if you can't show them to everyone? I did do a google search, but most of them explained how it works, but not how to do it!

#### Share this post

##### Share on other sites
hey

Gah, acidently hit refresh and lost everything I wrote :\.

Firstly I was curious as to wether you have already implemented any functions of Collision Detection in your program? and if you understand the theory and such behind Ray/Plane and Ray/Triangle collision detection and response?

If not then I advise you checkout Basic Collision Detection by Kurt Miller.

Hmm, imagine the Ray as being your camera's velocity vector, as your camera velocity vector will indicate where your camera will travel next. Ohh, and obviously the Plane or Triangle is that based on a face of your environment meshes.

So basically, for each mesh of your environment, you loop through each face, and firstly from each vertex point of the face you calculate it's Plane. You then check if your camera's velocity vector (ray) relative to your camera's position (position ray originates from) intersects / passes through the Plane. If the camera's velocity vector (ray) does intersect, then you must get the point of intersection, and check if it is within the bounds of your polygon, which I would assume to be a triangle. Anyhows all the details of this are explained in the article I refered to earlier.

So yea, for starters your camera's collision response could be just to set to the point of intersection, but I'm not sure how this would work, but it's a start and something I think you could logically progress from.

Anyhows I hope all thise makes sense :).

edit
Another thing I was wondering, is if you already have any scene culling routines in place? as I think these are very important for performing realtime collision detection. As you don't want to be checking for Ray/Plane and Ray/Triangle collisions of every face in your environment. You should at the least check if there is a collision between each of the environments/worlds meshes as a whole before checking against individual faces of a mesh.

Hmm, for this you would need to define/calculate bounding volumes for each of your meshes, these volumes could be a sphere, ellipsoid, or a cube. All have their advantages and disadventages, but to get started I would recommened just using Sphere Volumes, as Ray/Sphere collisions are quite easy to handle.

Otherwise, if your environment/world is constructed from one large mesh, or very few large meshes, then you should look into splitting them. For starters you may just like to divide them manually, or you may like to write a basic routine to divide them up into equal volumes. To furher improve on this you could order them into a quad-tree or octree.

I think there are already many tutorials about such things, which I could probably dig up for you if you like.

cyas

[Edited by - yosh64 on July 26, 2007 1:59:58 AM]

#### Share this post

##### Share on other sites
The following code is what I use for my raytracer. It works, but may not be the best solution for a game. As I said, I cannot help you very much because I'm not really into game programming...

The following method belongs to my polygon class and makes use of my math classes. It performs the intersection between the ray r and the polygon defined by the three vertex (defined elsewere). The parameter d is the max distance to be checked against, and the returned value is the distance between the origin of the ray and the intersection point. If no intersection was found or the intersection was farer than the parameter d, INF is returned.
If you insert the returned value in the ray parametric formula, you get the actual intersection point coordinates.

If you want I can send you the files, but be warned that I'm redesigning them right now because they are a bit messy.
The code here should be enaugh to help you writing your routine. The only thing to note is that vertices are kept in an array of vector3.
Good Luck

flat polygon::Intersect(const ray &r, float d, bool backfaces) const {	vector3 origin(r.GetOrigin());	vector3 dir(r.GetDir());		float dist = -((origin - v[0]) ^ planenormal) / (dir ^ planenormal);	if(dist < 0.0 || dist > d) return INF; 			vector3 C(dir * B);	float n = A ^ C;	if(IsZero(n)) return INF;	float q = 1.0 / n;	vector3 E(origin - v[0]);	float u = (E ^ C) * q;	if(u < 0.0 || u > d) return INF;	E = E * A;	float s = (dir ^ E) * q;	if(s < 0.0 || (u + s) > 1.0) return INF;	float t = (B ^ E) * q; 	return dist;}

#### Share this post

##### Share on other sites
Wow you guys gave me something to work on for a while! More like ah summer homework I guest. Thats what I would refer it. If you mean culling as in don't render the other side of the wall then yes. I haven't had any recent codes for this ordeal. I wouldn't even know where to start. If I need any help with this "homework" I'll let you guys know. Until then I have to wait before I can do this. Because the new PC I got had the wrong connector to the monitor.

Thanks!

#### Share this post

##### Share on other sites
hey

Just out of interest, what type of monitor ya got? and connector? It's just that the gfxcard on my newer computer only has a DVI connector, I think for an LCD monitor... and I have a CRT monitor, which has a VGA connector... anyhows my gfxcard came with an adaptor to convert the DVI connector to VGA. So I was just thinking you might have the same issue? but I dunno.

cya

#### Share this post

##### Share on other sites
I have a e machines 12 in height and 10 in width monitor. Bulk That is. The connector i don't know... The monitor is pretty old so I forgot it's name. All that I am doing is giving the thing a new tower and thats it. The new connector for the monitor to the video card should be in today so I mite get this going very soon. Then I have my own haven on the weekdays with out anyone bother me about their emails while I am working! Ha! :D Sorry not to be much of a help.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5
• Advertisement

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633710
• Total Posts
3013486
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!