Jump to content
  • Advertisement
Sign in to follow this  

raycasting unit cube

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

lets say i have a unit cube, i have a point inside the unit cube. From this point, given a direction, i want to cast a ray to find where it intersects with the cube. currently i've got a list of 6 planes and im doing plane-ray intersection tests
float GetBoxIntersection(float3 rayDir, float3 rayPos, inout float3 intersectPoint)
	float closestTime = 1.0;
	for (int i = 0; i < 6; ++i)
		float rayDirDotNormal = dot(gBoxPlanes.xyz, rayDir);
		// skip parallel planes
		// why break and not continue you ask? well if parallel time will always be 1.0 ;)
		if (rayDirDotNormal == 0.0)
		float3 pointOnPlane = -gBoxPlanes.xyz * gBoxPlanes.w;
		float time = dot(gBoxPlanes.xyz, pointOnPlane - rayPos) / rayDirDotNormal;
		// hrmm can we fix the shimmering somehow?
		if (time <= 0.0001 || time >= closestTime)
		closestTime = time;

	intersectPoint = rayPos + (rayDir * closestTime);
	return closestTime;
but this is very expensive, so im guessing there has got to be a cheaper way to do this considering its a unit cube

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!