- Home /
Linear equations in Unity?
Hey,
does anybody know a library/functions how to solve linear equations in Unity? My previous search just found old results, that basically suggest there weren't many working options :/
Because for my current project I would like to calculate the hitpoint on a plane in 3D space and get that hitpoint in relative coordinates to the plane, to use it for ViewportPointToRay Raycast afterwards.
I was hoping I can solve it myself by getting the hitpoint and the planes 3-corner points then getting 3 equations with 2 variables, which should be solvable by itself. Unfortunately for whatever reason, the hitpoint I receive is not on the plane of the 3 corner points of the plane, but behind it. (I am getting the cornerpoints by the meshes Vertice List and am using a Mesh Collider on the plane... so idk why that's the case).
Anyways it should still b e solvable, as I know the 2 Points of the line and can see it is intersecting with the Debug.DrawLine, so I would just get that line and check for the intersection point with the plane, but the math is a little more complicated then. Or I can't think of an easy way, because I have never been that good with math D:
So I look forward for any suggestions! :)
Answer by Bunny83 · Feb 22, 2020 at 03:05 PM
Well, it seems you've gone a bit too far here ^^. Solving linear systems of equation by a human is a pretty trivial process. Even it's fundamentally the same process, a computer generally works with predefined formulas / algorithms. However solving a system of linear equation requires rearrangement of the equations.
The most straight forward way is to represent your equations with an augmented / extended matrix and use gaussian elimination.
However a simple vector - plane intersection can be calculated with simple vector math. Unity actually provides you two structs which encapsulate those simple tasks. Specifically the Plane and Ray struct. The plane struct has a Raycast method which gives you the distance of the intersection along the ray. So you can simply use Ray.GetPoint to get the intersection point.
If you're interested in the math behind those methods, just have a look at the reference source code for the Raycast method and the GetPoint method.
Note that the Raycast method does only detect a hit (returns true) when the ray hits the "front side" of the plane. Though the intersection distance should also work backwards as long as your ray is not parallel to the plane surface.
If you (or someone else) are looking for a way to solve a linear system of equations, I just uploaded my "Gauss$$anonymous$$atrixF" class on pastebin. It's a generic matrix class for solving linear systems of equations of any size. It can also be used to calculate the inverse of a matrix of any size (which is also just a linear system of equations). The gaussian eli$$anonymous$$ation happens only in the first square section of the matrix. So to solve a system of 3 equations you would create a matrix like this var g = new Gauss$$anonymous$$atrixF(4, 3);
. Of course each row represents one equation and the last column are the constant terms.
To invert a 5x5 matrix create one like this var g = new Gauss$$anonymous$$atrixF(10, 5);
. Insert your original matrix in the first half and initialize the second half with the 5x5 identity matrix.
Note that this can only solve equations which have one / a solution. The result has to be unique. So the result can not be parametric. So for example you can not calculate the intersection of two planes (which would be a new line equation). However the intersection of 3 planes is possible. Of course you have to formulate your equations yourself. So for example if you have a plane in vector form and a line in vector form like this:
P(u,v) = Pstart + u*Pbase1 + v * Pbase2;
L(t) = Lstart + t*Ldir
You have to manually set them equal and rearrange them
Pstart + u*Pbase1 + v * Pbase2 = Lstart + t*Ldir;
Group all parameters on the left and combine the constants on the right
u*Pbase1 + v * Pbase2 - t*Ldir = Lstart - Pstart;
Now we have 3 equations and 3 unknows (u, v and t). So the matrix would look like this
// u | v | t |
// ---------------------------------------------------
// Pbase1.x | Pbase2.x | -Ldir.x | Lstart.x - Pstart.x
// Pbase1.y | Pbase2.y | -Ldir.y | Lstart.y - Pstart.y
// Pbase1.z | Pbase2.z | -Ldir.z | Lstart.z - Pstart.z
After calling Solve on that matrix the 3rd column ( counting from 0 ) will contain the solutions for u, v and t.
Note that this is just an example. In most cases you have a plane in normal form. In this case you would just plug in the line equation and you only have one unknown. That's essentially what the Plane and Ray struct solve for you in a straight forward way.
Answer by unity_Wd9us_rR3t_Emg · Feb 22, 2020 at 03:18 PM
Thanks :) I didnt know about the Plane struct, I will see if it returns the accurate result! :)
Otherwise I also manged to import math.NET, which should help doing the math, if needed!
Your answer
Follow this Question
Related Questions
Raycast visual and moving text possition. 1 Answer
Steering while following terrain surface normal 0 Answers
Unity Reload Bullets left 1 Answer
How to create a biological topography in Unity3D? 0 Answers
Lay a Sphere on a Point 3 Answers