need help with Camera and Mouseinput

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
ehenkes
Posts: 47
Joined: Sun Aug 03, 2008 2:52 pm
Location: Germany
Contact:

need help with Camera and Mouseinput

Post by ehenkes »

I need a little bit of help. If I use a movable Maya Camera and get X and Y from mouse moves and clicks, then I have the vector3df of the camera, the "look at target vector" of the camera, the mouse X and Y, the near and far value and the aspect ratio and some other stuff, which I currently do not understand. How can can I calculate whether a scenenode is intersected by a 3D-line going through camera and mouseclick? Is this already built-in in Irrlicht? I need a function like
bool SceneNode->isIntersected(mouseinput, nodeCamera).

I have found the following steps, but do not know how to do step 3:
Part One : Normalizing the Mouse Coordinates
There are a few things that we need to know before we can proceed. We need to know
1. The width of our screen
2. The height of our screen
3. The aspect ratio
4. The field of view (viewing angle of the scene, usually pi/2)
5. The near clipping plane distance
6. The far clipping plane distance

NX = ((X / (Screen_Width / 2)) - 1)
NY = 1 - (Y / (Screen_height / 2))

(Anmerkung: er multipliziert hier noch mit der aspect ratio, das ist m.E. falsch, weil er dann Werte > 1 erhält, oder?

Part Two : Finding Points on the Near + Far Clipping Planes
We need to find two 3D points: One point residing on the near clipping plane and another point sitting on the far clipping plane.



So what we need to do is Multiply the normalized coordinates by tan(fov/2)

Once we have done this we can multiply our ratios by any value of Z (adjacent side) to get points on the near and far clipping planes.
This is because of the formula Tan(theta) * Adjacent side = opposite side. Which should be familar to anyone who has done High School Trigonometry. We just replace Tan(theta) with our own ratios we worked out earlier and Z with our distances between the eye and the near or far planes and viola we've just found two points in 3D space. So what we need to do is Multiply the normalized coordinates by tan(fov/2).

RatioX = tan(fov / 2) * NX
RatioY = tan(fov / 2) * NY

P1.X = ratioX * Near
P1.Y = ratioY * Near
P1.Z = Near

P2.X = ratioX * Far
P2.Y = ratioY * Far
P2.Z = Far




Part Three : Transforming our points inside the viewing frustum into world points

Transforming into world points
A' * P = TransformedP
Where
A' = The inverse view matrix
P = A point within our viewing frustum

'Inverse the view matrix
Dim matInverse As D3DMATRIX
D3DDevice.GetTransform D3DTS_VIEW, matView
D3DXMatrixInverse matInverse, 0, matView
VectorMatrixMultiply P1, P1, matInverse
VectorMatrixMultiply P2, P2, matInverse

public Sub VectorMatrixMultiply
(ByRef vDest As D3DVECTOR, ByRef vSrc As D3DVECTOR, ByRef mat As D3DMATRIX)

dim W as single

W = 1 / ( vSrc.X * mat.m14 + vSrc.Y * mat.m24 + vSrc.Z * mat.m34 + mat.m44 )

vDest.X = (vSrc.X * mat.m11 + vSrc.Y * mat.m21 + vSrc.Z * mat.m31 + mat.m41) * W
vDest.Y = (vSrc.X * mat.m12 + vSrc.Y * mat.m22 + vSrc.Z * mat.m32 + mat.m42) * W
vDest.Z = (vSrc.X * mat.m13 + vSrc.Y * mat.m23 + vSrc.Z * mat.m33 + mat.m43) * W

end sub

Part Four : Creating a Ray
Now we have got our two points we need to turn them into a ray. The point on the near plane will become the starting point for our ray and the point on the far plane will become the end point.
source: http://gpwiki.org/index.php/VB:3Dmouse

Please help! :?
ehenkes
Posts: 47
Joined: Sun Aug 03, 2008 2:52 pm
Location: Germany
Contact:

Post by ehenkes »

I have found:

getSceneCollisionManager()
getRayFromScreenCoordinates(...)
getIntersectionWithSphere(...)

That helps for the beginning. :)
The most important one is the second function (I am glad, I use an engine).
torleif
Posts: 188
Joined: Mon Jun 30, 2008 4:53 am

Post by torleif »

ehenkes:
I had the same problem and used the engine to calculate the mouse cords in 3D space. Use the API like a christian a bible

It's really great when you don't have to do the hard work =D
Post Reply