Reputation: 3462
I have been looking for example of terrain collision that is in C++ but i haven't found anything. I found one example that uses .RAW file it is in toymaker.info so don't mention it.
How do you detect collision between object can terrain against every triangle. I know it will be slow. How you detect collision between terrain and .X file?
Upvotes: 3
Views: 450
Reputation: 509
If your .X file means DirectX format file, use DirectX Library to load .X meshes. Collision methods are also provided like D3DXIntersect.
Additional information : http://msdn.microsoft.com/en-us/library/windows/desktop/bb172882(v=vs.85).aspx
Upvotes: 1
Reputation: 62333
Here is one method:
Draw a line from the origin through the center of a sphere you wish to test collision of. Now do a ray triangle intersection using the line, or ray, you have just created. You now know if the ray has passed through the triangle or not. If it has passed through the triangle then get the point it intersected the triangle and do a simple distance check with the sphere's center. If the distance is greater than the radius of the sphere a collision has not occurred. If it less then the sphere is intersecting the triangle.
Its also worth checking which side of the triangle the sphere's center is on. This is a simple dot product between the triangle's face normal and the sphere's center. a value of 0 indicate the sphere's center lies in the triangle and a positive result indicates its on one side and negative, the other.
You can also perform this calculation for a range of times. So if you know you are simulating a second and you know the velocity the sphere is moving at, then you can calculate the point in that second that the sphere intersected the triangle. This is pretty simple to calculate. If at the start the sphere is 4 units from the triangle and after a second it is 1 unit through the triangle. So in 1 second it has travelled 5 unit. Therefore the intersection must have happened 0.8 seconds into the movement. This way you can react to collisions of particles even when they are travelling so fast they would otherwise pass straight through the item they are colliding with.
Upvotes: 2