## News:

LWJGL 3.3.2 released

## [Problem] Ray-Line Selection With Triangles

Started by Phyyl, March 12, 2013, 00:23:55

#### Phyyl

Hi eveyone, I'm in great need of help here... I've been trying to make a selection system working with the camera and a normalized vector pointing in front of the camera... In other words, check if there's a collision with a triangle and a line. Unfortunatly, it doesn't work as good as I though and I'm having a hard time finding the errors/things I'm doing wrong!

What I'm seeing :

- It detects that I'm looking in the right direction, but only at a distance of approx. 1 from the plane.
- It only detects that I'm lloking in the right direction when I'm looking in between -45 and 45 degrees on the x axis (looking up/down)
- I never get the "collision" result...

Here's the bit of code where I check for a plane intersection with the line :

... And here's the part where I check if the point is in the triangle :

Thank you so much!

#### quew8

Quote
// calculate t for the plane+line intersection
float t = (p.x*pn.x +
p.y*pn.y +
p.z*pn.z -
pn.x*pn.x - //MISTAKE HERE
pn.y*o.y -
pn.z*o.z) / (n.x*pn.x +
n.y*pn.y +
n.z*pn.z);
On the 5th line (Where my helpful comment is), there is a mistake. You wrote "pn.x * pn.x", it should be "pn.x * o.x"
Also perhaps it's just you trying to find bugs, but you seem to be working out n dot pn twice. And your writing out the individual operations when you could just be using the dot method in Vector you use later on. Made it a bit of a pain to debug (probably for you too) and this is where you made the mistake. Methods exist for a reason. Encapsulation ftw.

Thanks for listening.