Page 1 of 1
[SOLVED] [Maths] A small question... :)
Posted: Mon Nov 06, 2006 7:49 am
by Lideln
Okay okay, I know this is
NOT a maths forum, but I asked the question on a french maths forum and still got no answer
![Crying or Very sad :cry:](./images/smilies/icon_cry.gif)
^^
I know the position of two points (say A and B), and an imaginary line go through these two points.
I would like to know the X and Z coordinates of the point on this line with an Y of 0 (i.e. the intersection between this line and the plane of equation Y = 0)
The goal of this calculation is to make a map editor for my "game", more specificaly to put objects on the plane map, getting these points A and B thanks to the function :
Code: Select all
line3d<f32> line = hotp->scene->getSceneCollisionManager()->getRayFromScreenCoordinates(pos, camera);
A and B are the start and end of this line... And I would like to know the point of Y = 0 to place the objects on the map, under the mouse
If someone good in maths may help me with this calculation... I would really appreciate any help (as usual
![Wink :wink:](./images/smilies/icon_wink.gif)
)
Thank you all, have a nice day !
![Smile :)](./images/smilies/icon_smile.gif)
Posted: Mon Nov 06, 2006 11:23 am
by bitplane
you could use trig to work out with right angled triangles, or ratios to work out the slopes.. but the idea of having classes for vectors and stuff is to abstract the maths so you don't need to worry about it.
try "startVector + line.getVector().normalize() * yDistance" or use one of the plane3d helper functions (getIntersectionWithLine?)
Posted: Mon Nov 06, 2006 1:35 pm
by Lideln
Hi bitplane,
Although I followed advanced math studies, I forgot everything and I'm now back to a low maths level.
I'm sorry but I don't see what startVector is, yDistance neither
And about plane3D... Don't see what you mean
lol I know I am a desperate case.
When asking the question, I thought it would exist something like in 2 dimensions :
you have two points, then you can calculate the line "function" (y = ax + b), and then you replace y by 0 and you get the x value.
It does not exist in 3D ? You have 2 points, you can calculate the "function" of the line that goes through these 2 points, and then replace Y by 0, and you can find X and Z ?
Thank you, (again
![Smile :)](./images/smilies/icon_smile.gif)
)
Posted: Mon Nov 06, 2006 1:58 pm
by JP
Yeah you should just be able to generalise the 2D line forumla (y = mx + c) to 3D, but i'm not sure how that would go, i'm sure you could google something like "3D line forumla" and find out what it would be.
Posted: Mon Nov 06, 2006 2:02 pm
by Lideln
Ok I finally got the answer on that french Math forum
Just for information (in case someone would like to put objects on the map with a mouse click) :
Say A = (x, y, z) and B = (x', y', z') the 2 points that are known.
Say M = (X, 0, Z) the intersection between (AB) and the plane of equation y = 0.
Since M is on (AB), we have :
M = (x + k(x' - x), y + k(y' - y), z + k(z' - z))
All there is left, is to compute k thanks to : Y = y + k(y' - y) = 0
We get :
k = 1 / (1 - y'/y)
And then we can replace it in X and Z equations.
Hope this will help someone.
Thanks again Bitplane for your help !
And thanks JP as well ! (I just saw your post)
Posted: Mon Nov 06, 2006 5:22 pm
by bitplane
sure you can make a slope in 3d, you just do it twice as many times as in 2d (ie. for xy and zy).
but, like i said.. using vectors means you do all 3 dimensions at once and you don't have to worry about the maths at the x,y,z level, no ratios, no trig.. its like working in 1d! okay it's a bit slower, but it allows you to think freely.
this code may be wrong, i just guessed it-
Code: Select all
// the starting point
vector3df startVector = line.start;
// the direction (length is 1.0)
vector3df direction = line.getVector().normalize();
// the distance we want to travel
if (direction.Y<0) // downwards, and not / 0
{
f32 yDistance = line.start.Y / direction.Y;
// the final position
vector3df endVector = startVector + (direction * yDistance);
}
edit: bleh, was busy in work and didn't spot your post
![Razz :P](./images/smilies/icon_razz.gif)
Posted: Mon Nov 06, 2006 9:57 pm
by Lideln
Heheeeee thanks for the help anyway
In fact, I changed the
in
Its a small optim : a "divide" (division ?) less, but I like when things are well written and clear, so....
My editor now ressemble to something ! Its great, and I wont have to edit the maps directly in the XML lol
Thanks again !
Lideln
ps : the Quadtree is REALLY good !!! I have a good FPS (100fps) with 100 trees in the current field of view. The cons are : I use a map mesh with only 9 faces (!!!), and my trees are far less than 100 faces. But it works fine for me, so Im happy
![Smile :)](./images/smilies/icon_smile.gif)
Thanks again !