simple collision with terrain

Started by
1 comment, last by Gumgo 16 years ago
The most simple collision detection with a terrain I know is getting the current Height value using the camera coordinates. This method suits me pretty good and it works just great if my heightmap scale is 1. (I scale my each vertex value by a constant factor on heightmap creation). The movement is smooth. Now when I set the scaling factor to 100 for example the heightmap is of course 100 bigger on screen. The pixel data is still the same. I simply get the Height like this: NewPos = Height(CamX/100,CamZ/100)*100 + SomeValueForCameraHeight; Before trying it I would guess that this is choppy. And of course it is choppy when walking over the terrain. The Collision is correct. Well, I'm not really interested in implementing some advanced collision method. Isn't there a simple "hack" to stop the chopyness? I could scale the whole heightmap by the factor and run a gaussian blur over it. But that would use up huge amounts of memory and I dont like it. Has anyone got a better idea to make this interpolation a bit more smooth? thanks.
Advertisement
Instead of moving to the new height immediately, try moving it by a small amount up or down each frame until it reaches the correct height. That way when you come to a stop, it will take a small bit of time for the camera to "settle" onto the new height. This should keep things smoother.
Try something like this (you may have to switch it depending on whether the triangles go the other way):

// float cam_x, float cam_y are variables already// assuming each pixel represents a vertex, distance// would be how far spaced the vertices areint x_pixel = (int)floor( cam_x/distance ); // maybe you don't need floorint y_pixel = (int)floor( cam_y/distance );float x_percent = cam_x/distance - (float)x_pixel; // 0.0 - 1.0float y_percent = cam_y/distance - (float)y_pixel; // 0.0 - 1.0float c_TL = Height( x_pixel, y_pixel ); // arguments are in pixelsfloat c_TR = Height( x_pixel+1, y_pixel );float c_BL = Height( x_pixel, y_pixel+1 );float c_BR = Height( x_pixel+1, y_pixel+1 );float hgt;if (x_percent > y_percent)    hgt = c_TL+(c_TR-c_TL)*x_percent + (c_BR-c_TR)*y_percent;else    hgt = c_TL+(c_BL-c_TL)*y_percent + (c_BR-c_BL)*x_percent;return hgt;

I think this will work, but I'm very tired so maybe it won't... =b Just make it into a function. It isn't optimized or anything.

This topic is closed to new replies.

Advertisement