Skip to main content
GameDev.net gamedev.net
🔒 Locked

Ray casting.

Started by GoldDiggerTh Jun 28, 2015 at 1:19 PM 0 replies 2.5k views
Original Post
GoldDiggerTh
GoldDiggerTh

A week ago or so i gotton marching cubes in my game and i thought it was quite boring with out collision, so i have been looking around on google. The information that i gotton was that i have to do a raycast so i have read alot of articles about it and code and what i gotton it from it is:


	public static float pointOnRayTriangle(vec3 ray, vec3 origin, vec3 v0, vec3 v1, vec3 v2){
		//make the vertices to smaller positions.
		vec3 v11 =vec3.sub(v1, v0, null);	
		vec3 v22 =vec3.sub(v2, v0, null);

		//creating a normal
		vec3 normal =vec3.cross(v11, v22, null);

		float d =Math.abs((vec3.dot(v0, normal)));	//d =abs(a vertex . normal) but what vertex :?

		float t =-(vec3.dot(origin, normal) +d) /vec3.dot(ray, normal);
//		T =-( dot (position, triangle normal +distance) / dot ( ray, triangle normal)

		
		return t;
				
	}

a method that returns the time and uses the direction of the ray, the orgin of the ray, and the 3 vertices of the triangle (v0, v1, v2)

What my problem is that is when i do the raycast from the charchter feet and calculate the point of the ray i just get the player's position.

So i would like to know if i'm doing this ray alright.

Thank you in advance.

ninnghazad
ninnghazad

bool rayTriangleIntersect( 
    const Vec3f &orig, const Vec3f &dir, 
    const Vec3f &v0, const Vec3f &v1, const Vec3f &v2, 
    float &t) 
{ 
    // compute plane's normal
    Vec3f v0v1 = v1 - v0; 
    Vec3f v0v2 = v2 - v0; 
    // no need to normalize
    Vec3f N = v0v1.crossProduct(v0v2); // N 
    float area2 = N.length(); 
 
    // Step 1: finding P
 
    // check if ray and plane are parallel ?
    float NdotRayDirection = N.dotProduct(dir); 
    if (fabs(NdotRayDirection) < kEpsilon) // almost 0 
        return false; // they are parallel so they don't intersect ! 
 
    // compute d parameter using equation 2
    float d = N.dotProduct(v0); 
 
    // compute t (equation 3)
    t = (N.dotProduct(orig) + d) / NdotRayDirection; 
    // check if the triangle is in behind the ray
    if (t < 0) return false; // the triangle is behind 
 
    // compute the intersection point using equation 1
    Vec3f P = orig + t * dir; 
 
    // Step 2: inside-outside test
    Vec3f C; // vector perpendicular to triangle's plane 
 
    // edge 0
    Vec3f edge0 = v1 - v0; 
    Vec3f vp0 = P - v0; 
    C = edge0.crossProduct(vp0); 
    if (N.dotProduct(C) < 0) return false; // P is on the right side 
 
    // edge 1
    Vec3f edge1 = v2 - v1; 
    Vec3f vp1 = P - v1; 
    C = edge1.crossProduct(vp1); 
    if (N.dotProduct(C) < 0)  return false; // P is on the right side 
 
    // edge 2
    Vec3f edge2 = v0 - v2; 
    Vec3f vp2 = P - v2; 
    C = edge2.crossProduct(vp2); 
    if (N.dotProduct(C) < 0) return false; // P is on the right side; 
 
    return true; // this ray hits the triangle 
} 

See here. Are you sure you are casting your ray against the correct set of triangles?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.