Advertisement Jump to content
Sign in to follow this  

Issue : Selecting 3D object from click (casting ray)

This topic is 1831 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 must be able to select a 3D object in a simple seen from a click. To do that I use Processing and OBJLoader (to load the models) but my issue is in one of the algorithm. Every model has a simple BoundingBox always aligned with the world.

This is how I get the ray : 

public Ray getRayFromClick(int mouse_x, int mouse_y){ //3d normalized device coordinates 
float x = (2.0f * mouse_x) / width - 1.0f; float y = 1.0f - (2.0f * mouse_y) / height; 
float z = 1.0f; PVector ray_nds = new PVector(x, y, z);
//4d Homogeneous Clip Coordinates
float[] ray_clip = new float[4];

//4d Eye (Camera) Coordinates
float[] ray_eye = new float[4];
PMatrix proj_inv = p3d.projection.get();

//4d World Coordinates
float[] ray_wor4d = new float[4];
PVector ray_wor = new PVector();

//setup ray
Ray ray = new Ray();

return ray;

This is how I try to get if there is a collision with a plane and if the collision contact is in the plan :

public boolean intersectInPlane(Ray ray, PVector center , PVector nPlane, PVector halfSize){
PVector nPlaneSub = new PVector();
float t = -((float);
return false;
PVector centerMax = new PVector(center.x+halfSize.x,center.y+halfSize.y,center.z+halfSize.z);
PVector centerMin = new PVector(center.x-halfSize.x,center.y-halfSize.y,center.z-halfSize.z);
if(ray.p.x>=centerMin.x && ray.p.x<=centerMax.x
&& ray.p.y>=centerMin.y && ray.p.y<=centerMax.y
&& ray.p.z>=centerMin.z && ray.p.z<=centerMax.z)
return true;
return false;

The 't' has some really weird values and the contact is never in the plane. The models are in Z=-500 but the t is something in the line of 516753 which is a bit much.


Share this post

Link to post
Share on other sites

try a search of for the term "raypick".


the raypick demo in the older directx sdk's is some of the most reliable code to base your raypick code off of.


but as always, the intersection test at the far end is up to you.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!