[SOLVED] [Maths] A small question... :)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Lideln
Posts: 79
Joined: Sat Jun 24, 2006 11:35 am
Location: Paris, France

[SOLVED] [Maths] A small question... :)

Post 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 :cry: ^^

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: )

Thank you all, have a nice day ! :)
Last edited by Lideln on Mon Nov 06, 2006 2:03 pm, edited 1 time in total.
--
Lideln, France
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post 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?)
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Lideln
Posts: 79
Joined: Sat Jun 24, 2006 11:35 am
Location: Paris, France

Post 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 :oops:
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 ? :roll:

Thank you, (again :) )
--
Lideln, France
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post 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.
Image Image Image
Lideln
Posts: 79
Joined: Sat Jun 24, 2006 11:35 am
Location: Paris, France

Post by Lideln »

Ok I finally got the answer on that french Math forum :lol:

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)
--
Lideln, France
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post 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 :P
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Lideln
Posts: 79
Joined: Sat Jun 24, 2006 11:35 am
Location: Paris, France

Post by Lideln »

Heheeeee thanks for the help anyway :)

In fact, I changed the

Code: Select all

k = 1 / (1 - y'/y)
in

Code: Select all

k = y / (y - y')
Its a small optim : a "divide" (division ?) less, but I like when things are well written and clear, so.... :wink:

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 :) Thanks again !
--
Lideln, France
Post Reply