Hello,
I am trying to create add some mouse picking to a project (to test it
out I am just removing objects when they are clicked on), the objects
to be picked are spheres (planets) which are in an array.I am using a
perspective projection and I can make it work fine when the camera is
at position (0,0.0) however as soon as I move the camera at all it
either stops working completely or will become inaccurate (pick the
wrong sphere or not at all).
I am confused because I assumed that calling mousePos.add(mousePos,
cam.position); would account for the camera position , but obviously
not.
Thankyou very much to anybody who can help :)
Please see code below:
public function onMouseClick(e:MouseEvent):void
{
var mousePos:Number3D = cam.unproject(view.mouseX, view.mouseY);
mousePos.add(mousePos, cam.position);
var ray:Ray = new Ray();
ray.orig = cam.position;
ray.dir = mousePos;
ray.dir.normalize();
for each(var planet:Sphere in planets)
{
if (ray.intersectBoundingRadius(planet.position ,
planet.radius))
{
view.scene.removeChild(planet);
}
}