away3dlite mouse coords to object3d coords

63 views
Skip to first unread message

kon...@standardabweichung.de

unread,
Oct 20, 2009, 2:43:32 PM10/20/09
to away3...@googlegroups.com

Hello,

 

does anybody know how to use the built in Flash Player 10 functions to

project mouse coords to object3d-space?

 

I´ve figured out how to do it from object3d-space to screen in this way:

 

var _objectPoint:Vector3D = new Vector3D(100, 100, 0);

var _screenCoord:Vector3D = Utils3D.projectVector(_object3D.viewMatrix3D, _objectPoint);

 

But i dont know how to do itt the other way without MouseEvent3D.

 

Thanks,

Daniel

 

 

Li

unread,
Oct 20, 2009, 6:02:58 PM10/20/09
to away3...@googlegroups.com
From 3d space to screen coordinates you can use camera.screen(), which returns a screen vertex for the position of a 3d object.

From screen to 3d space its a bit more complicated since you can have infinite solutions. Any ray that reaches the camera at your 2d coordinates is a candidate line for you to place the 3d projection, practically anywhere. However a good solution is to define a virtual (mathematical) plane in space, always looking at the camera, and cast a ray from the cameras perspective onto the plane and use this ray's intersection with the plane as your produced 3d point. You can see this implemented in Rob's Sierpisnki demo: http://www.infiniteturtles.co.uk/blog/away3d-merry-christmas-2008

Near the start of the onEnterframe() he uses:
persp = camera.zoom/(1 + 1600/camera.focus);
mouseVector.x = view.mouseX/persp;
mouseVector.y = view.mouseY/persp;
mouseVector.z = 1600;

where 1600 is how far the virtual plane is positioned from the camera.

Of course this latter topic is more subjective than the first, and you could device many other ways to obtain a 3d coord from a screen coord.

katopz

unread,
Oct 21, 2009, 12:08:57 AM10/21/09
to away3...@googlegroups.com

daniel

unread,
Oct 21, 2009, 5:33:09 PM10/21/09
to away3d.dev
Thank you both for your suggestions.
Yes i want to do something similar to katopz example.

The difference is, that i got an fixed Camera3D at -z without any
rotation applied and
a Scene3D rotated around multiple axes.
What i try to get are the local XY coordinates from an infinite sized
mathematical Plane at 0,0,0 that matches the orientation of the Scene.
I´ve tried to find the Solution with the help of Katopz classes but
got no success.

package
{
import away3dlite.cameras.Camera3D;
import away3dlite.containers.ObjectContainer3D;
import away3dlite.containers.Scene3D;
import away3dlite.materials.*;
import away3dlite.primitives.*;
import away3dlite.templates.*;
import flash.events.Event;
import flash.geom.Matrix3D;
import flash.geom.Point;

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Vector3D;
import flash.geom.Orientation3D;
import flash.geom.Utils3D;

import jiglib.math.JMath3D;

public class ExMousecontrol extends FastTemplate
{
private var container:ObjectContainer3D = new ObjectContainer3D();

private var mouseObject:Plane = new Plane(new ColorMaterial
(0xFF9900), 100, 100, 1, 1);
private var referencePlane:Plane = new Plane(new WireframeMaterial
(0xFFFFFF, 0.5), 1000, 1000, 10, 10);
private var jMath3DPlane:Vector3D;

protected override function onInit():void
{
view.camera = new Camera3D();
view.camera.zoom = 4;
view.camera.z = -1000;

referencePlane.bothsides = true;

mouseObject.bothsides = true;
mouseObject.width = 100;
mouseObject.height = 100;

scene.rotationX = 45;
scene.rotationY = 45;

container.addChild(mouseObject);

container.addChild(referencePlane);
scene.addChild(container);

var _sceneNormal:Vector3D = getNormal(scene);
jMath3DPlane = JMath3D.fromNormalAndPoint(_sceneNormal, new Vector3D
(0,0,0));

stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
}

private function getNormal(object3D:Scene3D):Vector3D{
/*var p1:Point = object3D.local3DToGlobal(new Vector3D(0,0,0));
var p2:Point = object3D.local3DToGlobal(new Vector3D(100,0,0));
var p3:Point = object3D.local3DToGlobal(new Vector3D(0,100,0));

var edge1:Vector3D = new Vector3D(p1.x - p2.x, p1.y - p2.y, 0);
var edge3:Vector3D = new Vector3D(p1.x - p3.x, p1.y - p3.y, 0);*/

var _matrix:Matrix3D = new Matrix3D();
_matrix.rawData = view.scene.transform.matrix3D.rawData;
//_matrix.append(view.camera.viewMatrix3D);

var p1:Vector3D = _matrix.transformVector(new Vector3D(0,0,0));
var p2:Vector3D = _matrix.transformVector(new Vector3D(100,0,0));
var p3:Vector3D = _matrix.transformVector(new Vector3D(0,100,0));

var edge1:Vector3D = new Vector3D(p1.x - p2.x, p1.y - p2.y, p1.z -
p2.z);
var edge3:Vector3D = new Vector3D(p1.x - p3.x, p1.y - p3.y, p1.z -
p3.z);

var _normal:Vector3D = edge3.crossProduct(edge1);
_normal.normalize();

trace(_normal);
return _normal;
};

private function screenToWorld(_x:Number, _y:Number):Vector3D{
var ray:Vector3D = JMath3D.unproject
(view.camera.transform.matrix3D, view.camera.focus, view.camera.zoom,
_x, _y);
ray.add(new Vector3D(view.camera.x, view.camera.y, view.camera.z));

var intersectPoint:Vector3D = JMath3D.getIntersectionLine
(jMath3DPlane, view.camera.position, ray);

return intersectPoint;
};

private function handleMouseMove(event:MouseEvent):void
{
var _pos:Vector3D = screenToWorld(view.mouseX, view.mouseY);
mouseObject.x = _pos.x;
//mouseObject.y = _pos.z;
mouseObject.z = _pos.y;
//trace(_pos.x);
}
}
}


On Oct 21, 6:08 am, katopz <kat...@gmail.com> wrote:
> not sure that you try to drag something on plane, or something like that?
>
> 1. here's some exampleshttp://jiglibflash.googlecode.com/svn/trunk/fp10/src/jiglib/math/JMat...
>
> 2. and here how to use ithttp://jiglibflash.googlecode.com/svn/trunk/fp10/examples/away3dlite/...
>
> 3. and here's resulthttp://jiglibflash.googlecode.com/svn/trunk/fp10/examples/away3dlite/...
>
> hth
>
> 2009/10/21 <kont...@standardabweichung.de>
Reply all
Reply to author
Forward
0 new messages