PV3D: get 2D mouse-click coordinates in a 3D world

This is something important for game development (or whatever else you may want to do with 3D worlds). The problem: You have a 3D world (e.g. a 3D-map) and click with your 2D-Mouse Cursor somewhere on your screen (e.g. to place a flag at that point) – to do that, you need to project the position of your 3D-mouse into the 3D world. This is how to do it in Papervision 3D:

var ray:Number3D = camera.unproject(viewport.containerSprite.mouseX,viewport.containerSprite.mouseY);
var cameraPosition:Number3D = new Number3D(camera.x,camera.y,camera.z);
ray = Number3D.add(ray,cameraPosition);
var p:Plane3D = new Plane3D();
p.setNormalAndPoint(new Number3D(0, 1, 0), new Number3D(0, 0, 0));
var intersect:Number3D = p.getIntersectionLineNumbers(cameraPosition,ray);

Basically you shoot a ray from your camera to the position where the mouse-cursor is and check where it intersects with an imaginary plane p. That’s where you’ve clicked.

Nice and all, but I personally can’t use it on tcm, because navigation by clicking is far slower and less intuitive than moving with arrow- or wasd-keys. D’oh. This is basic stuff though, so it might come in handy one day or the other.

Leave a Reply

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>