Terrain Collisions
I am using a model for my terrain and have been working on allowing the camera to move accross it smoothly.
At the moment i test for collisions with the terrain, when one occurs i adjust the camera height to the heighest point in the inpolygon that the camera collided with. The problem is that this results in a very jerky bobbing effect as you move. Also this method only allows the camera to move up hill because when going downhill the camera doesn''t clooide with the terrain.
I know that with height mapped terrain you can write a function that can return the height of the terrian at any particular point but this can''t work with a model can it?
Any thoughts on how to do this better would be appreciated.
Don''t actually have the camera collide with the terrain, rather, get the z (height) for its (x,y) location in the world map, add some constant distance (height off the ground) to that location, and set the map there. Every time the camera''s x and y location change, update the z.
Brendan
Brendan
Brendan"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss
could you not just find the lowest vertex of the model and check that? i''ve never done it myself, but that just came to mind... it''s probaby be the first think i''d try ''til i found something better.
------------------------------------------------------------
Email
Website
"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------
------------------------------------------------------------
Website
"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------
You have to determine where in the polygon you collided and use the corresponding Z value. You can''t just use the highest Z point on a polygon else you will get those jumps.
use a bounding volume and then bilinear filtering to get the height of an exact point. i found this bililnear code on flipcode in the forum, and modified it to work for my heightmaps (it was not a class at the time):
it was just a test function, i eventually replaced it with plane/tri intersection on bounding volumes, so i''m not totally sure how it works (rather cryptic). xf and yf are the percentage into the terrain square, t* are the points of the terrain square, and the rest is figuring out which 4 points to use. the last function is bilinear filtering on the 4 poitns and the fractional value into the square.
float checkHeight(float x, float y){ float xf, yf, t0, t1, t2, t3; int temp, fx, fy, cx, cy; temp = (int)x / terrain_x; x -= temp * terrain_x; temp = (int)y / terrain_y; y -= temp * terrain_y; if(x < 0.0f) x += terrain_x; if(y < 0.0f) y += terrain_y; fx = (int)x; xf = x - fx; cx = (fx + 1) % terrain_x; fy = (int)y; yf = y - fy; cy = (fy + 1) % terrain_y; t0 = terrain_HeightMap[fx][fy]; t1 = terrain_HeightMap[cx][fy]; t2 = terrain_HeightMap[fx][cy]; t3 = terrain_HeightMap[cx][cy]; return t3 * xf * yf + t2 * (1.0f - xf) * yf + t0 * (1.0f - xf) * (1.0f - yf) + t1 * xf * (1.0f - yf);}
it was just a test function, i eventually replaced it with plane/tri intersection on bounding volumes, so i''m not totally sure how it works (rather cryptic). xf and yf are the percentage into the terrain square, t* are the points of the terrain square, and the rest is figuring out which 4 points to use. the last function is bilinear filtering on the 4 poitns and the fractional value into the square.
My terrain is not height mapped it is read from a model file and i was working under the assumption that you needed to be working with height mapped random terrain to do this technique.
I thought about altering the direction of my ray to test for intersections with the terrain and sending the ray out at maybe a steep angle in order to get and intersection before i normally would. Any more thoughts?
I thought about altering the direction of my ray to test for intersections with the terrain and sending the ray out at maybe a steep angle in order to get and intersection before i normally would. Any more thoughts?
oh, whoops. sorry. you can use the plane/tri intersection method, its what i switched to after that bilinear filtering. here is how it works:
-take the vertices from a triangle, make a plane out of them to get the plane that goes through the triangle.
-check if the camera intersects the plane, if not, go on to the next triangle
-if it does, do collision with the triangle itself (http://www.magic-software.com/Intersection3D.html).
-if it is intersecting that triangle, find the intersection depth of the camera into the plane. multiply that by the planes normal, and add that to the position of the camera. that will move it out of the triangle, and also give that ''slide'' effect.
-take the vertices from a triangle, make a plane out of them to get the plane that goes through the triangle.
-check if the camera intersects the plane, if not, go on to the next triangle
-if it does, do collision with the triangle itself (http://www.magic-software.com/Intersection3D.html).
-if it is intersecting that triangle, find the intersection depth of the camera into the plane. multiply that by the planes normal, and add that to the position of the camera. that will move it out of the triangle, and also give that ''slide'' effect.
The problem with waiting for the camera to collidie with the terrain before adjusting the height is that the camera will move a couple of frames then colide and jerk above the terrain, move another couple of frame and jerk up again. Also when going downhill it doesn''t collide with the terrain at all.
I really feel that i am tackling this problem wrong. Is there a way that i can determine at what height the camera should be at for a given position on the terrain noting that the terrain is not heightmapped or random it is read from a model file.
I really feel that i am tackling this problem wrong. Is there a way that i can determine at what height the camera should be at for a given position on the terrain noting that the terrain is not heightmapped or random it is read from a model file.
January 03, 2003 10:02 AM
Well, i have an idea, but maybe it''s too cumbersome:
You have to determine the 3 (if terrain is rendered as triangles, 0r 4 if quads) nearest (to you) points of the terrin, and using some simple geometry calculate what height is the point just under you at.
You have to determine the 3 (if terrain is rendered as triangles, 0r 4 if quads) nearest (to you) points of the terrin, and using some simple geometry calculate what height is the point just under you at.
I have now altered my collision function to return the nearest polygon to my ray even if there was not a collision. I then set the camera height to the heighest point in this polygon. It does look better but the camera is still jerking about as i move. Anyoone tackled this problem b4 and know how to make the movement smoother?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement