Modeled Terrain Collisions

Started by
7 comments, last by jonbell 20 years, 11 months ago
I am using a model (not random / height mapped) for my terrain and was wondering whats the best way to handle collisions accross it. At the moment I pass my camera''s position / look vectors to my collision routine which will return either the collided triangle or if no collision the nearest triangle to the camera. I then set the camera position to the heighest vertex of the triangle. While this works to some extent the movement is jerky and well, just nasty. Can I improve this or should i be looking at another method altogether? I thouight maybe something along the lines of altering my cameras direction vector according to what angle the nearest triangle could be making with the Y axis? I''m pretty stuck as you have probably already gathered so any help would be much appreciated.
Advertisement
you''ll need some space partitioning. Since you are using a terrain, with no heightmaps (but it''s still a terrain I suppose), you can use a quad tree or an octree, to get the triangles under the camera. You''ll have to surround the camera with a sphere/box, and do proper triangle/sphere/box collision detection.

If you don''t want to go that far, you can cast a ray from your camera position all the way down, find the triangle that intersects, and the intersection point, and check if the camera is above that point or not. For that, you''ll need a triangle-ray intersection routine (simple and easy to find on the net). Here again, a quadtree would make the algorithm much faster.

Everything is better with Metal.

I am currently using a ray / triangle intersection routine to find the nearest triangle on the terrain but my problem is how to smoothly alter the cameras Y value to follow the terrain. What value should I be giving Y?
if you use a ray that you shout downwards, and find the triangle intersected, then you can find the point of collision on that triangle. That should be pretty easy, since you''ve got that ray-triangle intersection routine working.

Say you find the point P as the point of intersection under your camera. What you can do is, if you''re camera''s y component is < (P.y + radius), then set your camera''s y position to (P.y + radius), where radius is a value you use to make the camera hover at a given altitude above the terrain. If you want your camera to always stay close to the terrain (not allowing flying), always set your camera''s Y value to (P.y + radius).

Everything is better with Metal.

to make it smoother, if P.y changes abruptly from frame to frame, here is what you can do.

Say C is your camera's position.

P is the point of intersection with the camera's ray (going down) and a triangle on your terrain.

I is the point where the camera should be placed to make it hover above the terrain.

therefore

fAltitude = 5.0f; // we want the camera to hover 5 meters above the ground
I = (P.x, P.y + fAltitude, P.z)

then you can do

fSpeed = 0.3f; // How fast the camera will jump to it's position above the ground. 1.0f will make the camera jump straight away, 0.0f won't move the camera.
C.x += (I.x - C.x) * fSpeed;
C.y += (I.y - C.y) * fSpeed;
C.z += (I.z - C.z) * fSpeed;



[edited by - oliii on May 10, 2003 1:11:08 PM]

Everything is better with Metal.

Thats the method i am using at the moment and its too jerky. Having thought about this i need to alter my cameras direction vector to match the angle of the terrain at any given point, the camera will then move perfectly smoothly accross any terrain (i could also set limits on the angles to stop people walking through unpassable parts.

The problems i now have is exactly how to measure the angles and also how to tell if the user is going up or down hill. Any ideas.
well, it should be pretty smooth.
The problem is, you set the camera to the highest vertex of the triangle, where you should set it to the point of intersection.

for the orientation of the camera, this is quite similar to the algorithm I detailed in that thread.

http://www.gamedev.net/community/forums/topic.asp?topic_id=156028

so you need to orientate the camera''s up vector according to the normal (N) at the point of intersection.


  Vector Axis.CrossProduct(Up, N);float sina = Axis.GetLength();float cosa = Up.Dot(N);float angle = atan2(sina, cosa);Axis.Normalise();float a = angle * 0.3f; // smooth the movement a bitUp.RotateAroundAxis(a, Axis);  // rotate up vector of cameraDir.RotateAroundAxis(a, Axis); // rotate Dir vector of cameraRight.RotateAroundAxis(a, Axis); // rotate right vector of camera  


if you want an even smoother movement, you can use linear interpolation between the vertex normals and vertex positions to make the movement rounder. A bit like Phong shading.

Everything is better with Metal.

for the user to be going up or down, you check the y value at the Dir component of the camera. If the Dir.y is negative, the user goes down, if positive, the user goes up. if Dir.y greater than, say, 0.3f, then the slope is too steep.

[edited by - oliii on May 10, 2003 10:48:53 PM]

Everything is better with Metal.

Thx a lot, last night i managed to get it working correctly. The solution is pretty simple actually. Just add the intersected point to the intersected trinagles normal and you get the new camera position. This method also stops the player walking through walls etc because the walls normal will have the result of pushing the player back.

This topic is closed to new replies.

Advertisement