Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (633) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Ray Picking Tutorial  (Read 1382 times) 0 Members and 1 Guest are viewing this topic.
zFollette

Junior Devvie

Exp: 2 years

I like jokes

 « Posted 2014-01-26 13:37:50 »

Hi all, this is a continuation of my first tutorial: Ray Casting
In this tutorial, I will be going over the Math involved in Line-Plane intersection, and also how to implement it.

The basic idea of what is going on is very simple, first we get 3 coordinates from a plane, the convert it to a plane equation, then use the Ray from the Ray Cast to find the intersection point on the plane.

The only values you need (other than mouse coords) are 3 random points in the plane you want to intersect.

For this whole tutorial, I will use the values below:

Plane Coordinates:
A = (0, 0, -1)
B = (1, 2, -1)
C = (1, 3, -1)

Mouse Coordinates:
start = (1, 1, 1)
end = (1, 1, -2)

Ok, lets get into it. First off, I like to make a hell of a lot of floats, just to make it easier on me.
 1  2  3 `float x = 0, x1 = A.x, x2 = B.x, x3 = C.x;float y = 0, y1 = A.y, y2 = B.y, y3 = C.y;float z = 0, z1 = A.z, z2 = B.z, z3 = C.z;`

Now, we will do some math.
Here is the matrix that we are going to mimic.

We will make 3 float arrays, x Column, y Column, and z Column and put in the values corresponding to the matrix.
 1  2  3 `float[] xC = new float[]{x - x1, x2 - x1, x3 - x1};float[] yC = new float[]{y - y1, y2 - y1, y3 - y1};float[] zC = new float[]{z - z1, z2 - z1, z3 - z1};`

We will get the multiplication values for each row. This will be done by covering up the row we are solving for and cross multiplying the values in the other rows.
 1  2  3 `float addI = (yC[1] * zC[2]) - (yC[2] * zC[1]);float addJ = ((xC[1] * zC[2]) - (xC[2] * zC[1]));float addK = (xC[1] * yC[2]) - (xC[2] * yC[1]);`

Sorry if the names confuse you with what they are used for.

What we will do next is find out how many of the variable T we have in the equation:
 1 `float numOfTs = (addI * (end.x - start.x)) + (addJ * (end.y - start.y)) + (addK * (end.z - start.z));`

Following that is figuring out the sum of any numbers in the equation:
 1 `float num = (addI * (x1)) + (addJ * (y1)) + (addK * (z1)) - (addI * start.x) - (addJ * start.y) - (addK * start.z);`

Now that we have that, we can solve for T:
 1 `float t = num / numOfTs;`

Now we can essentially plug T back into the equation to find X, Y and Z
 1  2  3 `x = start.x + ((end.x - start.x) * t);            y = start.y + ((end.y - start.y) * t);            z = start.z + ((end.z - start.z) * t);`

I am sorry that this is not in depth, Vector Math is very new to me and I just wanted to get a tutorial out there because I struggled to find anything myself.

Here is my final source code:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `            float x = 0, x1 = A.x, x2 = B.x, x3 = C.x;            float y = 0, y1 = A.y, y2 = B.y, y3 = C.y;            float z = 0, z1 = A.z, z2 = B.z, z3 = C.z;            float[] xC = new float[]{x - x1, x2 - x1, x3 - x1};            float[] yC = new float[]{y - y1, y2 - y1, y3 - y1};            float[] zC = new float[]{z - z1, z2 - z1, z3 - z1};            float addI = (yC[1] * zC[2]) - (yC[2] * zC[1]);            float addJ = ((xC[1] * zC[2]) - (xC[2] * zC[1]));            float addK = (xC[1] * yC[2]) - (xC[2] * yC[1]);            float numOfTs = (addI * (end.x - start.x)) + (addJ * (end.y - start.y)) + (addK * (end.z - start.z));            float num = (addI * (x1)) + (addJ * (y1)) + (addK * (z1)) - (addI * start.x) - (addJ * start.y) - (addK * start.z);            float t = num / numOfTs;            x = start.x + ((end.x - start.x) * t);            y = start.y + ((end.y - start.y) * t);            z = start.z + ((end.z - start.z) * t);`

Humor will keep you alive.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Ralphanese (18 views) 2015-05-29 13:07:55 Gibbo3771 (16 views) 2015-05-29 06:54:17 Ralphanese (23 views) 2015-05-28 16:52:52 theagentd (31 views) 2015-05-27 22:21:17 theagentd (36 views) 2015-05-27 22:21:08 CopyableCougar4 (27 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (33 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21
 Riven 25x Rayvolution 24x theagentd 22x BurntPizza 19x Spasi 17x orangepascal 16x KevinWorkman 15x Drenius 15x ra4king 13x DavidBVal 12x princec 12x opiop65 12x EgonOlsen 11x Husk 11x kevglass 9x Slyth2727 8x
 Intersection Methodsby Roquen2015-05-29 08:19:33List 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:02
 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