Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Vector Math  (Read 3113 times) 0 Members and 1 Guest are viewing this topic.
zFollette

Junior Devvie

Exp: 2 years

I like jokes

 « Posted 2014-01-24 02:34:50 »

So I finally figured out my ray casting problem, now the new problem I am facing is the math for line/plane intersection, I have learned a lot so far on the topic, just not enough. I have now figured out the whole Vector Math problem, here is my code:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 `Vector3f A = new Vector3f(0, 0, 0);        Vector3f B = new Vector3f(1, 0, 0);        Vector3f C = new Vector3f(1, 1, 0);        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);        float vectX = (x + xC[0]);        float vectY = (y + yC[0]);        float vectZ = (z + zC[0]);        Vector3f intersection = new Vector3f(vectX, vectY, vectZ);`

On top of that, here is my Ray Casting bit if anyone needs it: (The compileBuffer methods just make Float and Int Buffers)
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 `float[] matModelView = new float[16], matProjView = new float[16];        int[] view = new int[16];        float mouseX = width / 2;        float mouseY = height / 2;        Vector3f start = new Vector3f();        Vector3f end = new Vector3f();        FloatBuffer modelBuffer = compileBuffer(matModelView);        FloatBuffer projBuffer = compileBuffer(matProjView);        FloatBuffer startBuffer = compileBuffer(new float[]{start.x, start.y, start.z, 1});        FloatBuffer endBuffer = compileBuffer(new float[]{end.x, end.y, end.z, 1});        IntBuffer viewBuffer = compileBuffer(view);        glGetFloat(GL_MODELVIEW_MATRIX, modelBuffer);        glGetFloat(GL_PROJECTION_MATRIX, projBuffer);        glGetInteger(GL_VIEWPORT, viewBuffer);        gluUnProject(mouseX, mouseY, 0.0f, modelBuffer, projBuffer, viewBuffer, startBuffer);        gluUnProject(mouseX, mouseY, 1.0f, modelBuffer, projBuffer, viewBuffer, endBuffer);        start = new Vector3f(startBuffer.get(0), startBuffer.get(1), startBuffer.get(2));        end = new Vector3f(endBuffer.get(0), endBuffer.get(1), endBuffer.get(2));`

The one problem I am facing when implementing my code into my game is finding the first plane that my line intersects, what it is doing now is going through the order that I test them in, and if the line happens to intersect that plane, that is the detected intersection, how can I make sure I am detecting the FIRST plane that my line intersects?

Humor will keep you alive.
quew8

JGO Knight

Medals: 53

 « Reply #1 - Posted 2014-01-24 08:21:25 »

So the ray is actually a line segment from start to end. What you have to do is test this line segment for intersection with your geometry. The form this takes depends on the geometry but it will probably be either line\plane or line\sphere intersection test. Then maybe sort the intersections to find the closest if you need to do that.
zFollette

Junior Devvie

Exp: 2 years

I like jokes

 « Reply #2 - Posted 2014-01-24 21:18:43 »

So the ray is actually a line segment from start to end. What you have to do is test this line segment for intersection with your geometry. The form this takes depends on the geometry but it will probably be either line\plane or line\sphere intersection test. Then maybe sort the intersections to find the closest if you need to do that.

I get that, but how? I don't have a physical line segment. All I have is the start and end positions, nothing in between to test for intersections.

Humor will keep you alive.
quew8

JGO Knight

Medals: 53

 « Reply #3 - Posted 2014-01-24 21:28:21 »

That is all you need. Because a start and end point completely describe a line segment. Here http://paulbourke.net/geometry/pointlineplane/. Scroll down to line plane intersection. This may not be the best tutorial but to give you an idea. Literally Googling line plane intersection will give you a plethora of good tutorials. If you're unsure about the whole vector maths then I advice this tutorial http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/.
zFollette

Junior Devvie

Exp: 2 years

I like jokes

 « Reply #4 - Posted 2014-01-25 08:30:00 »

I changed the original question.

Humor will keep you alive.
Roquen

JGO Kernel

Medals: 517

 « Reply #5 - Posted 2014-01-25 08:40:33 »

Don't do that...it's annoying and impossible to follow.  If you want to pose a new question do it in a new post or thread.
quew8

JGO Knight

Medals: 53

 « Reply #6 - Posted 2014-01-25 15:16:23 »

Essentially, I think so. Your description is quite hard to follow. It would be simpler if you worked in traditional algebra and I think would help you when it came to programming the test as well. Write it algebraically.
Roquen

JGO Kernel

Medals: 517

 « Reply #7 - Posted 2014-01-25 18:16:41 »

What's the question anyway?
zFollette

Junior Devvie

Exp: 2 years

I like jokes

 « Reply #8 - Posted 2014-01-25 19:48:29 »

What's the question anyway?

The plane equation differs depending on the points given, but to find the point in which the line intersects is:
<X0, Y0, Z0> + t<X1 - X0, Y1 - Y0, Z1 - Z0>
Where 0 is the first point, and 1 is the second point.
Then, solving that equation gives a Vector, these points are the one you have to plug back into the plane equation to get the x, y, z intersection point.

The numbers I have given in the OP are completely random and I have no idea if they actually intersect.

Humor will keep you alive.
quew8

JGO Knight

Medals: 53

 « Reply #9 - Posted 2014-01-26 00:19:49 »

The one time where it would be fine to answer a question with another question ... and you don't do it. Hilarious.

But yeah you seem to know what you're doing.
Roquen

JGO Kernel

Medals: 517

 « Reply #10 - Posted 2014-01-26 07:55:25 »

The plane equation has only three degrees of freedom.  Any positive scale factor applied to the terms represents the same plane.  So:

Quote
The plane equation differs depending on the points given...
No it's the same equation...just scaled differently.
zFollette

Junior Devvie

Exp: 2 years

I like jokes

 « Reply #11 - Posted 2014-01-26 08:20:46 »

The plane equation has only three degrees of freedom.  Any positive scale factor applied to the terms represents the same plane.  So:

Quote
The plane equation differs depending on the points given...
No it's the same equation...just scaled differently.

I guess you're correct, but what I am saying is that (x + y + z = 0) will not work for every value passed through, it differs.

Also, I plan to release a text tutorial on this (once I perfect my method) , since I had so much trouble finding out how to do it.

Humor will keep you alive.
zFollette

Junior Devvie

Exp: 2 years

I like jokes

 « Reply #12 - Posted 2014-01-26 10:38:58 »

Added another question in the OP.

Humor will keep you alive.
Roquen

JGO Kernel

Medals: 517

 « Reply #13 - Posted 2014-01-26 10:49:38 »

Did I mention that's an annoying thing to do?
zFollette

Junior Devvie

Exp: 2 years

I like jokes

 « Reply #14 - Posted 2014-01-26 10:57:34 »

Did I mention that's an annoying thing to do?

Sorry, I felt like making new threads for everything was a dumb idea and a useless waste of server space.

Humor will keep you alive.
Roquen

JGO Kernel

Medals: 517

 « Reply #15 - Posted 2014-01-26 11:23:44 »

No need.  Instead of posting "I changed the first post".  Pose your new question instead.  Everyone changing the contents of posts makes the thread worthless because it can't be followed.
Roquen

JGO Kernel

Medals: 517

 « Reply #16 - Posted 2014-01-26 11:53:34 »

Glancing at the first post to guess your new question: it looks like your asking questions related to "how" you think you need to solve a problem than the real problem itself.  Example if you want to see if/where a ray intersects an AABB or an AABB-like structure (say a tree)...then the approaches are totally different.
zFollette

Junior Devvie

Exp: 2 years

I like jokes

 « Reply #17 - Posted 2014-01-26 12:30:09 »

Glancing at the first post to guess your new question: it looks like your asking questions related to "how" you think you need to solve a problem than the real problem itself.  Example if you want to see if/where a ray intersects an AABB or an AABB-like structure (say a tree)...then the approaches are totally different.

Well I figured it out, so now my system works near flawlessly. I think I will be posting that tutorial now.

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

 ivj94 (580 views) 2018-03-24 14:47:39 ivj94 (44 views) 2018-03-24 14:46:31 ivj94 (371 views) 2018-03-24 14:43:53 Solater (60 views) 2018-03-17 05:04:08 nelsongames (105 views) 2018-03-05 17:56:34 Gornova (147 views) 2018-03-02 22:15:33 buddyBro (690 views) 2018-02-28 16:59:18 buddyBro (90 views) 2018-02-28 16:45:17 xxMrPHDxx (492 views) 2017-12-31 17:17:51 xxMrPHDxx (728 views) 2017-12-31 17:15:51
 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