Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Ray - 3d triangle intersection test  (Read 825 times)
0 Members and 1 Guest are viewing this topic.
Offline Mac70
« Posted 2013-08-12 14:49:12 »

I have written simple ray-triangle intersection code based on this two tutorials: http://graphics.stanford.edu/courses/cs348b-98/gg/intersect.html, http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-9-ray-triangle-intersection/ray-triangle-intersection-geometric-solution/. It works perfectly for triangles with all points y=0, but when any (or all) points have another y values then it does not work properly.

Here is the code for intersection (glColor calls added for debugging): http://www.java-gaming.org/?action=pastebin&id=676

Triangle:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
package intersection;

import org.lwjgl.util.vector.Vector3f;

public class Triangle {

    public Vector3f p0;
    public Vector3f p1;
    public Vector3f p2;
   
    public Triangle(Vector3f p0, Vector3f p1, Vector3f p2) {
        this.p0 = p0;
        this.p1 = p1;
        this.p2 = p2;
    }
   
}


Ray:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
package intersection;

import org.lwjgl.util.vector.Vector3f;

public class Ray {

    private Vector3f start;
    private Vector3f dir;
    private float angleX;
    private float angleY;
   
    public Ray(float x, float y, float z, float angleX, float angleY) {
        start = new Vector3f(x, y, z);
        dir = new Vector3f();
        this.angleX = angleX;
        this.angleY = angleY;
        updateDir();
    }
   
    Vector3f getStartVector() {
        return start;
    }
   
    public float getStartX() {
        return start.x;
    }
   
    public float getStartY() {
        return start.y;
    }
   
    public float getStartZ() {
        return start.z;
    }
   
    Vector3f getDirVector() {
        return dir;
    }
   
    public float getDirX() {
        return dir.x;
    }
   
    public float getDirY() {
        return dir.y;
    }
   
    public float getDirZ() {
        return dir.z;
    }
   
    public float getAngleX() {
        return angleX;
    }
   
    public float getAngleY() {
        return angleY;
    }
   
    public void setPosition(float x, float y, float z) {
        start.x = x;
        start.y = y;
        start.z = z;
        updateDir();
    }
   
    public void setAngle(float angleX, float angleY) {
        this.angleX = angleX;
        this.angleY = angleY;
        updateDir();
    }
   
    public void setPositionAndAngle(float x, float y, float z, float angleX, float angleY) {
        start.x = x;
        start.y = y;
        start.z = z;
        this.angleX = angleX;
        this.angleY = angleY;
        updateDir();
    }
   
    private void updateDir() {
        dir.x = (float)(Math.cos(angleY)*Math.sin(angleX));
        dir.y = (float)-Math.cos(angleX);
        dir.z = (float)(Math.sin(angleY)*Math.sin(angleX));
    }
   
}


Why this code works incorrectly? I am trying to find bug, but don't found everything since 2 days. Undecided

Check out my Devblog! Smiley
Offline Simba Productions

Senior Newbie


Medals: 1



« Reply #1 - Posted 2013-08-12 21:24:12 »

I'm still shaky on vectors so I didnt feel confident trying to find a mistake in your code.

I thought up another way to do it, by using 2D ray-plane intersection test 3x so that it would work for a 3D object

If you arent interested in another way to do it, i dont blame you, your way seems a lot more "correct" than mine and is probably more efficient, but anyway heres how i would do it

{
a triangle is made up of 3 points with x,y,z coordinates. a ray is just a line that is infinite in one direction.

first, i would take the x,y components of every one of the points in the triangle and break the triangle up into three lines. check if the ray intersects any of these 3 lines (once it intersects one move on to the next step ). In other words imagine flattening out the 3D object into 2D. If the line intersects it in 3D it must intersect it in 2D as well no matter which way you look at it.

second, take the y,z components of the lines of the triangles and ray, do it again.

third, x,z check intersections again.

if it intersects on every plane then the ray intersects the triangle. Again im not saying this way is "fast" or "correct". Its just the way i would do it. shouldn't kill performance too bad unless you are checking a ton of triangles.

You need to figure out an algorithm to see if two lines intersect. There are many, google is your friend.

there are also many algorithms that can quickly check if the ray even has a chance of intersecting the triangle.

Hope that wasn't too stupid an algorithm, I made that up off the top of my head when i read the title of this thread, though it will work i don't know if it'll be too fast.
}

Good Luck!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!