5 Replies - 1269 Views - Last Post: 12 March 2013 - 10:54 AM Rate Topic: -----

#1 Phyyl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-March 13

[problem] Ray-Line selection with triangles

Posted 11 March 2013 - 05:11 PM

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 :

public final class Plane {
	// o = origin (camera/eye position)
	// n = normalized vector for the direction the player is looking to
	// p = plane point (in this case the A point of a triangle)
	// pn = plane normalized vector
	
	public static Vector3f intersect(Vector3f o, Vector3f n, Vector3f p, Vector3f pn) {
		//tests if the line is perpendicular to the plane
	    if ((n.x * pn.x + 
	    	 n.y * pn.y +
	    	 n.z * pn.z) != 0) {
	    	// 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 - 
	    			   pn.y*o.y - 
	    			   pn.z*o.z) / (n.x*pn.x + 
	    					   		n.y*pn.y + 
	    					   		n.z*pn.z);
	    	//tests if the line intersects IN FRONT of the player
	    	if (t > 0)
	    		//returns the position of the collision
	    		return new Vector3f(o.x + t*n.x,
	    				o.y + t*n.y,
	    				o.x + t*n.z);
	    	else
	    		return null;
	    }
	    else {
	    	return null;
	    }
	    
	}
}



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


public class Triangle {
	public Vector3f a, b, c, n;
	
	public Triangle(Vector3f a, Vector3f b, Vector3f c, Vector3f n) {
		this.a = a;
		this.b = b;
		this.c = c;
		this.n = n;
	}
	
	public boolean intersect(Vector3f p) {
        if (SameSide(p, a, b, c) && SameSide(p, a, b, c) && SameSide(p, a, b, c)) {
            Vector3f vc1 = new Vector3f();
            		Vector3f.cross(sub(a, B)/>, sub(a, c), vc1);
            if (Math.abs(Vector3f.dot(sub(a, p), vc1)) <= .01f)
                return true;
        }
        return false;
    }

    private boolean SameSide(Vector3f p1, Vector3f p2, Vector3f A, Vector3f B)/> {
        Vector3f cp1 = new Vector3f();
        		Vector3f.cross(sub(B, A), sub(p1, A), cp1);
        Vector3f cp2 = new Vector3f();
        		Vector3f.cross(sub(B, A), sub(p2, A), cp2);
        return Vector3f.dot(cp1, cp2) >= 0;

    }
    
    private Vector3f sub(Vector3f a, Vector3f B)/> {
    	Vector3f result = new Vector3f();
    	Vector3f.sub(a, b, result);
    	return result;
    }
    
    public void draw() {
    	
    	glColor3f(1, 1, 1);
    	glBegin(GL_TRIANGLES);
    	
    	glNormal3f(n.x, n.y, n.z);
    	glVertex3f(a.x, a.y, a.z);
    	glVertex3f(b.x, b.y, b.z);
    	glVertex3f(c.x, c.y, c.z);
    	
    	glEnd();
    }
}



If anyone see what I'm doing wrong, please help!

Thank you so much!

Is This A Good Question/Topic? 0
  • +

Replies To: [problem] Ray-Line selection with triangles

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: [problem] Ray-Line selection with triangles

Posted 11 March 2013 - 10:48 PM

Are you attempting a variant of 'picking'?
Was This Post Helpful? 0
  • +
  • -

#3 Phyyl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-March 13

Re: [problem] Ray-Line selection with triangles

Posted 11 March 2013 - 10:55 PM

yes exatly! I want to find out if there's a collision between the ray and the triangle and also find the coordinates of that intersection point...
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: [problem] Ray-Line selection with triangles

Posted 12 March 2013 - 05:28 AM

I can see why your approach is error prone, you have added an extra layer of complexity by deciding what should be picked based on player orientation. That isn't really the mechanic for picking.

Just used normal picking and have the player [model] orientate themselves towards the currently selected item.
Was This Post Helpful? 0
  • +
  • -

#5 Phyyl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-March 13

Re: [problem] Ray-Line selection with triangles

Posted 12 March 2013 - 08:19 AM

View PostButchDean, on 12 March 2013 - 05:28 AM, said:

I can see why your approach is error prone, you have added an extra layer of complexity by deciding what should be picked based on player orientation. That isn't really the mechanic for picking.

Just used normal picking and have the player [model] orientate themselves towards the currently selected item.

normal picking ? I seriously have no idea how to do this ! do you have any short example so I can start somewhere ?

thank you
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: [problem] Ray-Line selection with triangles

Posted 12 March 2013 - 10:54 AM

Research skills help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1