Terrain Collision

Started by
7 comments, last by dantheman1337 13 years, 1 month ago
I've made terrain out of a triangle mesh. Now, I found some tutorials, but they didn't seems to quite achieve the precision I wanted, or were too confusing. Can I have some examples please?
Advertisement
depends on the collision method you like. you can use the heightmap interpolation technique, or cast a ray down to the terrain from current position of the object, it's simple man, you should be able to figure it out yourself

it's simple man

Well, not necessarily :) It really depends on what the requirements are. For example, realistic collision detection (and possibly response) for a vehicle moving over irregular terrain can get pretty involved.

@The OP: What are the requirements exactly?
O yes, sorry. I thought he was about to try the simple one,
@OP : if you arent using any kind of physics engine, then maybe you want to start out simple (sphere-triangle) and if you want derive the orientation matrix from the collision normal and direction vector...
Thank you all, I think I can get it to work smoothly :)
I simply want the character to be able to walk on the terrain smoothly, not all jumpy like.
then I guess you could use the heightmap interpolation method, just google it, or download the source of tank3b bla bla I forgot the name. read the source, the collision detectio part

then I guess you could use the heightmap interpolation method, just google it, or download the source of tank3b bla bla I forgot the name. read the source, the collision detectio part


I used this method, and the character still jumps. AM I doing something wrong? Can I see an example? You see, I used the 256 character set to load the data from a file, which I then scale to 0.04. Which makes the max height approximately around 10. However, I took the player's coordinate, converted them to relative terrain coordinates. I then interpolated the coordinates using linear interpolation, and the used the true player coordinates and subtracted the, from the x and z coordinates.

I'm finding that the collision works pretty well, but at times the player goes lower than where it should actually be, and it seems jumpy. Here's my code:



float Terrain::Collision(vec3 pos){
float transx = (float)Width[0]*0.5,transz = (float)Length[0]*0.5;
int tx = (int)(pos.x + transx),tz = (int)(pos.z + transz); // Finds the relative x and z of the player and grid.
int x, z;
float fractionX = 0.0f, fractionZ = 0.0f;

if (tx <= 0.0f) {
x = 0;
fractionX = 0.0f;
} else if (tx >= (float)(Width[0] - 1)) {
x = Width[0] - 2;
fractionX = 1.0f;
} else {
x = (int)tx;
fractionX = (((pos.x + transx))) - x;
}

if (tz <= 0.0f) {
z = 0;
fractionZ = 0.0f;
} else if (tz >= (float)(Length[0] - 1)) {
z = Length[0] - 2;
fractionZ = 1.0f;
} else {
z = (int)tz;
fractionZ = (((pos.z + transz))) - z;
}
if ((fractionX + fractionZ) < 1.0f) {
return(LinearInterpolate((float)Height[z][x]*0.04,(float)Height[z+1][x]*0.04,fractionX)+
((float)Height[z][x+1]*0.04 - (float)Height[z][x]*0.04) * fractionZ);

} else {
return(LinearInterpolate((float)Height[z][x + 1]*0.04,(float)Height[z+1][x + 1]*0.04,fractionX)+
((float)Height[z + 1][x]*0.04 - (float)Height[z+1][x+1])*0.04 * (1.0 - fractionZ));
}
}

Am I doing something incorrectly? Help is appreciated!
SOLVED IT!

The last statement is apparently unnecessary.

This topic is closed to new replies.

Advertisement