Showing posts with label Triangle. Show all posts
Showing posts with label Triangle. Show all posts

Monday, November 23, 2009

Ray-Triangle intersection test code

I noticed that most of the blog visitors come here for Ray-Triangle intersection test so I decide to put the code here.

I hope it is useful for you.

//! Checks if the ray and triangle intersects and store the intersection point in the Out parameter.
CollisionType IntersectRay(Ray* pRay, kge::math::Vector& Out)
{
Edge1 = Point2 - Point1;
Edge2 = Point3 - Point1;
Normal.Cross(Edge1, Edge2);
float fGama = -(pRay->Direction * Normal);

if (fGama < fEpsilon)
return ECT_NotIntersect;

Vector b = pRay->Position - Point1;
float fLanda = (b * Normal) / fGama;

if (fLanda >= 0.0f && fLanda <= 1.0f)
{
Vector u;
u.Cross(b, pRay->Direction);
float u1, u2;
u1 = (Edge2 * u) / fGama;
u2 = (-(Edge1 * u)) / fGama;
if (u1 + u2 <= 1.0f && u1 >= 0.0f && u2 >= 0.0f)
{
Out = pRay->Position + (pRay->Direction * fLanda);
return ECT_Intersect;
}
}

return ECT_NotIntersect;

} // IntersectRay

Sunday, November 1, 2009

Edit terrain

Hi all

After coding for triangle-ray intersection I start to create a class for getting the clicked triangle. I called this class DynamicTriangleMesh.

DynamicTriangleMesh works slow for now and I need to partition the terrain and add AABB(Axis Aligned Bounding Box) to it then I check the ray collision with AABB if it collide then check for triangle intersection.

I added some functions for editing the tiles and height of the terrain.

I also added a flood fill algorithm to fill the tiles and finding the tiles borders for blending them.

I test my idea about blending and tiling it works good and soon the first version of terrain will be ready.

I also added a screen shot for showing the fill and finding the tile borders with flood fill algorithm.

you can get the latest codes from SVN.
https://kge.svn.sourceforge.net/svnroot/kge

Saturday, October 24, 2009

Ray-Triangle intersection test

I added Ray-Triangle intersection test to the engine. First I want to use PhysX for doing this but this library does not support dynamic triangle meshes it means I can’t change the mesh data very frequently with a reasonable speed so I decide to write my own Ray-Triangle intersection test.

I find an algorithm for do this issue on Collision Detection in Interactive 3D Environments Book it says Ray-Triangle test but when I test the algorithm it works as a line-triangle intersection test. The more interesting thing is that the next chapter is talking about line-triangle intersection test but it works good and the performance is very good.

The next thing I worked on this week was on getting the ray from screen coordinates and this part work good too.