Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  Vector Math  (Read 877 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.
Offline quew8

JGO Coder


Medals: 31



« 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.
Offline 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline quew8

JGO Coder


Medals: 31



« 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/.
Offline 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.
Offline Roquen
« 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.
Offline quew8

JGO Coder


Medals: 31



« 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.
Offline Roquen
« Reply #7 - Posted 2014-01-25 18:16:41 »

What's the question anyway?
Offline 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.
Offline quew8

JGO Coder


Medals: 31



« 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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« 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.
Offline 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.
Offline 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.
Offline Roquen
« Reply #13 - Posted 2014-01-26 10:49:38 »

Did I mention that's an annoying thing to do?
Offline 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.
Offline Roquen
« 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.
Offline Roquen
« 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.
Offline 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  
 
 
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.

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

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

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

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

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

kovacsa (60 views)
2014-11-07 19:57:14

TehJavaDev (64 views)
2014-11-03 22:04:50

BurntPizza (62 views)
2014-11-03 18:54:52

moogie (77 views)
2014-11-03 06:22:04

CopyableCougar4 (77 views)
2014-11-01 23:36:41
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!