Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Ray Picking Tutorial  (Read 1013 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (20 views)
2014-11-25 12:00:59

SHC (18 views)
2014-11-25 11:53:45

Norakomi (18 views)
2014-11-25 11:26:43

Gibbo3771 (21 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (67 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!