Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (498) Games in Android Showcase (117) games submitted by our members Games in WIP (564) 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 789 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.

Check out my Devblog!
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.
 Grunnt (21 views) 2014-09-23 14:38:19 radar3301 (14 views) 2014-09-21 23:33:17 BurntPizza (31 views) 2014-09-21 02:42:18 BurntPizza (22 views) 2014-09-21 01:30:30 moogie (20 views) 2014-09-21 00:26:15 UprightPath (29 views) 2014-09-20 20:14:06 BurntPizza (33 views) 2014-09-19 03:14:18 Dwinin (48 views) 2014-09-12 09:08:26 Norakomi (75 views) 2014-09-10 13:57:51 TehJavaDev (108 views) 2014-09-10 06:39:09
 BurntPizza 38x Rayvolution 23x Riven 19x KdotJPG 19x princec 18x KevinWorkman 17x basil_ 16x Mike 13x LiquidNitrogen 12x kevglass 12x deathpat 11x nsigma 11x ags1 10x theagentd 9x Roquen 8x HeroesGraveDev 8x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 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