Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Ray - 3d triangle intersection test  (Read 1080 times) 0 Members and 1 Guest are viewing this topic.
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.
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.

 MrMapcom (16 views) 2015-05-23 20:26:16 MrMapcom (22 views) 2015-05-23 20:23:34 Waterwolf (29 views) 2015-05-20 15:01:45 chrislo27 (36 views) 2015-05-20 03:42:21 BurntPizza (71 views) 2015-05-10 15:53:18 FrozenShade (56 views) 2015-05-07 09:11:21 TheLopais (219 views) 2015-05-06 13:36:48 TheLopais (202 views) 2015-05-06 13:35:14 TheLopais (207 views) 2015-05-06 13:33:39 TheLopais (228 views) 2015-05-06 13:32:48
 Spasi 32x Riven 16x BurntPizza 16x Drenius 12x ra4king 12x theagentd 11x DavidBVal 11x Husk 11x EgonOlsen 11x princec 10x KevinWorkman 9x scanevaro 8x opiop65 7x Slyth2727 6x revers 6x KaiHH 6x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 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