Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) 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 3078 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

 EgonOlsen (45 views) 2018-06-10 19:43:48 EgonOlsen (25 views) 2018-06-10 19:43:44 EgonOlsen (47 views) 2018-06-10 19:43:20 DesertCoockie (202 views) 2018-05-13 18:23:11 nelsongames (127 views) 2018-04-24 18:15:36 nelsongames (126 views) 2018-04-24 18:14:32 ivj94 (867 views) 2018-03-24 14:47:39 ivj94 (128 views) 2018-03-24 14:46:31 ivj94 (771 views) 2018-03-24 14:43:53 Solater (143 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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