Jump to content
  • Advertisement
Sign in to follow this  
dantheman1337

Terrain Collision

This topic is 2843 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

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

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!