Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (569) Games in Android Showcase (154) games submitted by our members Games in WIP (618) 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 1321 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.

 Riven (22 views) 2015-04-16 10:48:47 Duke0200 (35 views) 2015-04-16 01:59:01 Fairy Tailz (27 views) 2015-04-14 20:13:12 Riven (28 views) 2015-04-12 21:36:37 bus hotdog (45 views) 2015-04-10 02:39:32 CopyableCougar4 (46 views) 2015-04-10 00:51:04 BurntPizza (46 views) 2015-04-06 22:06:58 ags1 (50 views) 2015-04-02 10:58:48 Riven (49 views) 2015-04-01 18:27:05 ags1 (66 views) 2015-03-31 10:55:12
 BurntPizza 24x theagentd 21x wessles 15x 65K 12x Rayvolution 12x kingroka123 11x alwex 10x KevinWorkman 9x ra4king 8x phu004 8x kevglass 8x SHC 8x Olo 7x Ecumene 7x chrislo27 7x Roquen 7x
 How 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:00List of Learning Resources2014-08-05 19:33:27
 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