TIGSource Forums

Developer => Technical => Topic started by: ChevyRay on May 12, 2009, 04:59:10 AM



Title: Mouse position on the xy plane
Post by: ChevyRay on May 12, 2009, 04:59:10 AM
I'm stuck on this. I can get close, but my method is crap and still doesn't get it right on. I'm working with Game Maker, but if anybody can explain best as possible how to do this, I might be able to code the solution myself.

Basically, I have the camera position [x,y,z] and the camera's direction, vector [xaim, yaim, zaim]. The camera is never rotated and I'm always looking down at the xy plane. I THINK the camera has a 45-degree span, but this is the part that I'm unsure of, because I don't exactly know how to find this out (set by default with GM :/)

Is there some sort of formula I can put this information into, or is it more complicated than that? (I'm going to assume the latter...)

:coffee:

PS: I'm a 3D amateur, so if this sounds ignorant, it's because it is :giggle:


Title: Re: Mouse position on the xy plane
Post by: Ivan on May 12, 2009, 08:34:11 AM
You would need to cast a ray, taking into account the camera's projection and see where the ray intersects your xy plane.

Look up ray picking/ray casting and ray-plane intersection and you should have everything you need.


Title: Re: Mouse position on the xy plane
Post by: Mikademus on May 12, 2009, 08:53:32 AM
What Ivan said, plus that this is in fact quite a complicated task, so don't feel ashamed about it.


Title: Re: Mouse position on the xy plane
Post by: Matt Thorson on May 12, 2009, 09:02:26 AM
Wish I could've been more help on MSN.  It sucks that GM's 3D support is so sparse.

I'm thinking the best way to do this would be to define obj_point, obj_ray, obj_vector, obj_plane objects.  Define scripts like newRay( point, vector ) and newPlane( point, perpendicular_vector ) to initialize them - then add a script for findRayIntersectsPlane( ray, plane ).

This way you can keep all the math clean and modularized and have it all automated when you need it later.

Still no idea what the guts of findRayIntersectsPlane() would look like though :P


Title: Re: Mouse position on the xy plane
Post by: Pishtaco on May 12, 2009, 09:35:13 AM
Off the top of my head:

Let n be the unit vector in the direction the camera is pointing, and u a unit up-vector for the camera (which you want to be orthogonal to n). Let p be the camera position.

Calculate a left (or right, whichever way it goes) vector for the camera as the cross product of u and n.

Now suppose the screen coordinates are such that (0,0) is the centre of the screen, (1,0) is to the right of the centre and (0,1) is above the centre.

If the mouse is in the middle of the screen, you want to intersect the plane with the line that starts at p and goes in the direction of n. If the mouse is at position (x,y), you want to intersect with the line that starts at p and goes in the direction n+a*x*l+a*y*u, where a is a constant factor that you can probably find by trial and error.

To find the intersection of the plane with a line starting from p and going in a direction v, let lambda be the "height" component of p divided by the "height" component of v; you want the point at p+lambda*v.