Author Topic: Matching mouse clicks to 3D co-ords  (Read 3734 times)

Offline Hrolf1

  • byte
  • *
  • Posts: 2
    • View Profile
Matching mouse clicks to 3D co-ords
« on: February 05, 2005, 10:57:16 pm »
Hi!

I'm writing an applet where an insect follows mouse clicks.
The camera follows the insect from behind (height -240, distance 300).
To save valuable processing time the flat (ie Y=0) landscape is rendered as one big square with a texture on it.
I can use Interact2D to find the landscape, but how do I get more precise x,y,z 3D co-ords from the 2D (x,y)  where the user clicked? I know the y will be 0 but I'm not good enough at maths to work out how to get x & z!
The only way I can think of is breaking the landscape into lots of polys and using Interact2D, but this would play havoc with my processing time!
Can you help?

Offline rolz

  • float
  • ****
  • Posts: 280
  • Technocrat
    • View Profile
Matching mouse clicks to 3D co-ords
« Reply #1 on: February 07, 2005, 07:48:36 am »
should be somewhere in examples

Code: [Select]

    /**
     * get 3d coordinate of srcPoint on object's surface from 2d coordinates.
     *
     * @param camera scene camera
     * @param buffer
     * @param x      screen (mouse) X coordinate
     * @param y      screen (mouse) Y coordinate
     * @param object to check against
     * @return
     */
    public static SimpleVector getAbsoluteCoordinate(Camera camera, FrameBuffer buffer, int x, int y, Object3D object) {
        SimpleVector rezult = null;
        if (camera != null && buffer != null) {
            SimpleVector rayTemp = Interact2D.reproject2D3D(camera, buffer, x, y);

            rayTemp.normalize();
            Matrix orient = camera.getFront();
            float[] dump = orient.getDump();
            SimpleVector ray = new SimpleVector();
            ray.x = dump[0] * rayTemp.x + dump[1] * rayTemp.y + dump[2] * rayTemp.z + dump[3] * 1;
            ray.y = dump[4] * rayTemp.x + dump[5] * rayTemp.y + dump[6] * rayTemp.z + dump[7] * 1;
            ray.z = dump[8] * rayTemp.x + dump[9] * rayTemp.y + dump[10] * rayTemp.z + dump[11] * 1;

            float distance = object.rayIntersectsAABB(camera.getPosition(), ray);

            if (distance != Object3D.RAY_MISSES_BOX) {
                rezult = new SimpleVector(camera.getPosition());
                ray.scalarMul(distance);
                rezult.add(ray);
            }
        }
        return rezult;
    }
Regards,
Andrei

Offline Hrolf1

  • byte
  • *
  • Posts: 2
    • View Profile
Matching mouse clicks to 3D co-ords
« Reply #2 on: February 07, 2005, 05:33:16 pm »
Thanks Andrei!

This works great (but I needed to add 'isNormalized=true' to the end of rayIntersectsAABB to get y=0).

If you're ever in the UK I'll buy you a beer!

Cheers!