Game Development Community

Mouse selecting

by Ben Woodhead · in Torque Game Engine · 02/23/2004 (12:16 pm) · 3 replies

Hello Everybody

I did a few searches and I was unable to find an answer to my question. I also read some of the docs and didn't really find an answer. I was wondering if anybody knew of a way to do this or a link if its already been discussed.

Basically, when a person clicks on the screen how can I calculate the location of the click when it hits the z plain. The camera is sitting just above the z plane looking down on a 45 degree angle but that can change.

Thanks, Ben

#1
02/23/2004 (5:21 pm)
Are you thinking of hit-scan type selection as is done in the mission editor? You might look through some of that script to see if you can find anything enlightening.
#2
02/24/2004 (3:34 pm)
Hey Alex,

Thanks for the responce. I was thinking to much about how to do it in torque and never enough thought of the fact that its a fairly simple problem to solve with math.

So for people wondering, here it is.

Intersecting a line with the Z plane.

There are a few way to represent a mathmatical line but the one that I find the easiest to use and least amount of calculations is:

X = X1 + ( X1 - X2 )T
Y = Y1 + ( Y1 - Y2 )T
Z = Z1 + ( Z1 - Z2 )T

X1, Y1, Z1 is point 1
X2, Y2, Z2 is point 2
X, Y, Z is the point you are trying to find.
T is time ( the distance down the line ).

When T = 0 you will be at the first point and when T = 1 you will be at the second point.

So to solve when the line hits the z plane. Well that tells us that Z = 0. So we start with that and figure out the time along the line.

Z = Z1 + ( Z1 - Z2 )T
0 = Z1 + ( Z1 - Z2 )T
-Z1 = ( Z1 - Z2)T
-Z1 / ( Z1 - Z2) = T

Well now we have 2 so to solve for x and y you sustitute in the time variable ( or distance ) and you can see were you are on the 3 different axes.

This may seem like a little odd when you have the all 3 axes seperate. Its hard to see how they relate to each other. Well here is an exercise for you.

Start in 2d: X, Y. We want to figure out were X is when you hit the Y plane.

So pick 2 points. I selected (6,4) and (4,2) so they would not go through the origin. Draw that on a peice of paper.

// We are trying to find T when Y hits 0 so set Y to zero. And you may as well substitute in the 2 points in.

Y = Y1 + ( Y1 - Y2 )T
0 = 4 + ( 4 - 2 )T
-4 = 2T
-4/2 = T
T = -2

// Now take your T value and put it in the X equation and solve for X.
X = X1 + ( X1 - X2 )T
X = 6 + ( 6 - 4 ) (-2)
X = 6 + 2 ( - 2)
X = 6 - 4
X = 2

So when Y = 0 then X = 2. Now all you have to do is do the same thing you did for X for the Z Axes and you will now be able to solve a 3d line hitting a Plane.

Ben
#3
02/27/2004 (1:20 am)
Thanks a lot man!