How to project a 3D point to screen coordinates?

58 views
Skip to first unread message

Markus Geiger

unread,
Feb 11, 2008, 6:47:11 AM2/11/08
to away3...@googlegroups.com
Hi there,

my first post here. I began with Away3D, switched to PV3D, and again I'm back to Away3D :) Away3D is more easier to use, runs stable, doesn't lag as much as PV3D when changing/adding objects and i think it runs smoother (on older hardware) and really gives me the features i want.


Well i ran into some problems getting started:

1) I have to show a label on an object, not only when the user is hovering with the mouse (so no MouseEvent3D!). But how do i get the coordinates of an object in 2D? Is there something like MouseEvent3D that gives me the screen coordinates onEnterFrame? Following doesn't work:

// Setup label used on hovering icon
label                       = new TextField();
label.text                  = "Hello World!";
addChild(label);
// var m:Vertex3D = new Vertex3D(sphere.x,sphere.y,sphere.z);
// var pos:ScreenVertex = m.project(view.camera.projection);

var pos:ScreenVertex = view.camera.screen(sphere); // doesn't work
label.x = pos.x+stage.stageWidth*0.5;
label.y = pos.y+stage.stageHeight*0.5;

.mg

Rob Bateman

unread,
Feb 11, 2008, 7:13:53 AM2/11/08
to away3...@googlegroups.com
Hey Markus. Glad to have you back with us!


you can use the screen() method on the Camera3D class to resolve a ScreenVertex from an Object (and optional Vertex within the object) like so:

var result:ScreenVertex = camera.screen(myobject, [myvertex])

[] indicate optional Vertex value.

the x and y properties of result will give you the x and y position of the object inside the view instance.

cheers

Rob
--
Rob Bateman
Flash Development & Consultancy

rob.b...@gmail.com
www.infiniteturtles.co.uk
www.away3d.com

Blu:RayNe

unread,
Feb 11, 2008, 7:33:01 AM2/11/08
to away3d.dev
That's exactly what i've done. But x and y always stay 0,0.

var pos:ScreenVertex = view.camera.screen(spreit);
label.x = pos.x+view.x;
label.y = pos.y+view.y;

.mg

On Feb 11, 1:13 pm, "Rob Bateman" <rob.bate...@gmail.com> wrote:
> Hey Markus. Glad to have you back with us!
>
> you can use the screen() method on the Camera3D class to resolve a
> ScreenVertex from an Object (and optional Vertex within the object) like so:
>
> var result:ScreenVertex = camera.screen(myobject, [myvertex])
>
> [] indicate optional Vertex value.
>
> the x and y properties of result will give you the x and y position of the
> object inside the view instance.
>
> cheers
>
> Rob
>
> On Feb 11, 2008 11:47 AM, Markus Geiger <m...@evolution515.net> wrote:
>
>
>
> > *Hi there,*
>
> > my first post here. I began with Away3D, switched to PV3D, and again I'm
> > back to Away3D :) Away3D is more easier to use, runs stable, doesn't lag as
> > much as PV3D when changing/adding objects and i think it runs smoother (on
> > older hardware) and really gives me the features i want.
>
> > *Well i ran into some problems getting started: *
>
> > 1) I have to show a label on an object, not only when the user is hovering
> > with the mouse (so no MouseEvent3D!). But how do i get the coordinates of an
> > object in 2D? Is there something like MouseEvent3D that gives me the screen
> > coordinates onEnterFrame? Following doesn't work:
>
> > // Setup label used on hovering icon
> > label = new TextField();label.text = "Hello World!";
> > addChild(label);
>
> > // var m:Vertex3D = new Vertex3D(sphere.x,sphere.y,sphere.z);
> > // var pos:ScreenVertex = m.project(view.camera.projection);
>
> > var pos:ScreenVertex = view.camera.screen(sphere); // doesn't worklabel.x = pos.x+stage.stageWidth*0.5;label.y = pos.y+stage.stageHeight*0.5;
>
> > .mg
>
> --
> Rob Bateman
> Flash Development & Consultancy
>
> rob.bate...@gmail.comwww.infiniteturtles.co.ukwww.away3d.com

Blu:RayNe

unread,
Feb 11, 2008, 8:19:42 AM2/11/08
to away3d.dev
Ah.. version is Away3D 1.9.3

Daevid

unread,
Feb 27, 2008, 5:01:30 AM2/27/08
to away3d.dev

I'm not able to get this working. I am using 1.9.4 with the following
script:

var 3dPos:Object3D=new Object3D();
3dPos.position=new Number3D(44,0,13)
var screenPos:ScreenVertex=panorama.view.camera.screen(3dPos);

the x property of screen screenPos is always 0, no matter what
position I use?

/David

Daevid

unread,
Feb 28, 2008, 10:24:21 AM2/28/08
to away3d.dev
Can anyone confirm if this function should work as expected. The
result I get from the screen function is always 0,0 and the visible
prop is false, no matter where in the scene the point is.

I've also tried to add the obj3d to the scene but it doesn't help.

Thankful for any hints on getting this working..!

/David

Rob Bateman

unread,
Feb 28, 2008, 11:28:33 AM2/28/08
to away3...@googlegroups.com
Hi David

i'm looking into this at the moment - will let you know my findings when i have them

Rob
--
Rob Bateman
Flash Development & Consultancy

rob.b...@gmail.com
www.infiniteturtles.co.uk
www.away3d.com

Peter Kapelyan

unread,
Feb 28, 2008, 11:41:06 AM2/28/08
to away3...@googlegroups.com
Hi
 
Here is a function to place the vertex to onscreen coordinate, have fun!

screenPlace(camera, myMovieClip, My3dObject, 1)//1=first vertice
function screenPlace(camera,who,whob,vert):void {
   var result:ScreenVertex;
   result=camera.screen(whob,whob.vertices[vert]);
   who.x=result.x + 300;//half view width
   who.y=result.y + 200;//half view height
  }

Does anyone know how to do this to an objects position instead of vertex?
Thanks

Daevid

unread,
Mar 1, 2008, 3:33:13 AM3/1/08
to away3d.dev
Thanks, I'll try it out on Monday.

/David

glenn_a_m

unread,
Apr 27, 2008, 5:02:35 AM4/27/08
to away3d.dev
Hi,

was there any progress with this issue ?
I'm trying to find the screen position of an arbitrary point in the
worlds co-ord space (no associated object, just a vertex),
but as noted, it always returns 0,0

if camera.screen wont work, are there any suggestions?


thanks,
glenn


On Feb 29, 2:28 am, "Rob Bateman" <rob.bate...@gmail.com> wrote:
> Hi David
>
> i'm looking into this at the moment - will let you know my findings when i
> have them
>
> Rob
>
> On Thu, Feb 28, 2008 at 3:24 PM, Daevid <david.sjolan...@gmail.com> wrote:
>
> > Can anyone confirm if this function should work as expected. The
> > result I get from the screen function is always 0,0 and the visible
> > prop is false, no matter where in the scene the point is.
>
> > I've also tried to add the obj3d to the scene but it doesn't help.
>
> > Thankful for any hints on getting this working..!
>
> > /David
>
> --
> Rob Bateman
> Flash Development & Consultancy
>
> rob.bate...@gmail.comwww.infiniteturtles.co.ukwww.away3d.com

Rob Bateman

unread,
May 1, 2008, 10:19:56 PM5/1/08
to away3...@googlegroups.com
Sorry for the delay with this issue guys

the bug is flagged for attention, but i'm afraid i can't do much now until after the weekend. Will definitely look at this, though

atb

Rob
rob.b...@gmail.com
www.infiniteturtles.co.uk
www.away3d.com

KeithKong

unread,
May 5, 2008, 6:15:52 PM5/5/08
to away3d.dev
I'm looking forward to using this as well, but I was wondering if
there is a reverse function. I know initially this doesn't work since
there would be infinite possible depths but what if you have one of
the three axis fixed? For example, say you have an object3D that is
always at y = 0, can you get x/z coordinates of an object on this
plane to make it position itself according to the mouse or
movieclip?

Or is there a way you can get x/y/z coordinates of an object at a set
depth that correspond to a movieclips location?

Thanks,

Keith
> rob.bate...@gmail.comwww.infiniteturtles.co.ukwww.away3d.com

Rob Bateman

unread,
May 5, 2008, 6:31:49 PM5/5/08
to away3...@googlegroups.com
Hey Keith

sounds like this is what you're looking for:

http://blog.zupko.info/?p=106

if you want to use Away3d for this technique, the code is remarkably similar:

findhit = new FindHit(view, view.primitives, x, y);

sphere.x = findhit.sceneX;
sphere.y = findhit.sceneY;
sphere.z = findhit.screenZ;


hth!


Rob
rob.b...@gmail.com
www.infiniteturtles.co.uk
www.away3d.com

KeithKong

unread,
May 5, 2008, 7:23:09 PM5/5/08
to away3d.dev
Yeah, this is what I'm looking for, but it's not working properly.
All three (findhit.sceneX, findhit.sceneY, findhit.screenZ) return
NaN. Here is what I've done...

findhit = new FindHit(view, view.primitives, mouseX, mouseY);
cursor.x = findhit.sceneX;
cursor.y = findhit.sceneY;
cursor.z = findhit.screenZ;

Any help would be appreciated, thanks!
> rob.bate...@gmail.comwww.infiniteturtles.co.ukwww.away3d.com

Rob Bateman

unread,
May 5, 2008, 7:29:07 PM5/5/08
to away3...@googlegroups.com
hey Keith

try doing the following:

findhit = new FindHit(view, view.primitives, view.mouseX, view.mouseY);

cursor.x = findhit.sceneX;
cursor.y = findhit.sceneY;
cursor.z = findhit.screenZ;

also, make sure this is executed AFTER the view.render() method


Rob
rob.b...@gmail.com
www.infiniteturtles.co.uk
www.away3d.com

KeithKong

unread,
May 5, 2008, 8:04:59 PM5/5/08
to away3d.dev
Now it returns NaN when the cursor is near the top of the screen and
no matter where I move the mouse cursor doesn't reach a position on
the screen. Does it matter that cursor is inside an ObjectContainer3D
which changes position?
> rob.bate...@gmail.comwww.infiniteturtles.co.ukwww.away3d.com

Rob Bateman

unread,
May 6, 2008, 5:27:50 AM5/6/08
to away3...@googlegroups.com
Hey Keith


The findhit function will only return coordinates if the cursor is over some object in the scene, otherwise sceneX, sceneY and sceneZ are never set


if you have some example code of what you are trying to do, it would be a great help in solving the problems you're having


atb


Rob
rob.b...@gmail.com
www.infiniteturtles.co.uk
www.away3d.com

nathan

unread,
May 6, 2008, 6:44:45 PM5/6/08
to away3d.dev
Did we have any luck from going from 3d to 2d points? This seems to
have gone a bit off track, would be handy to know if this is
possible. Since it is rendering the scene, surely the calculations
are being done already?

Any info would be great.

Cheers,
Nathan

KeithKong

unread,
May 6, 2008, 7:12:30 PM5/6/08
to away3d.dev
Hey Rob,

I've figured it out now, thanks. This is what I've done:

findhit = new FindHit(view, view.primitives, view.mouseX,
view.mouseY);

if(findhit.sceneX) {
cursor.x = findhit.sceneX;
cursor.y = findhit.sceneY + 2;
cursor.z = findhit.sceneZ;
}


Thanks again,

Keith
> rob.bate...@gmail.comwww.infiniteturtles.co.ukwww.away3d.com

nathan

unread,
May 6, 2008, 9:12:17 PM5/6/08
to away3d.dev
OK, so I think I may have figured this out, I found some code that
someone had modified from away3d for papervision and somone said it
was working over there, so thought I would have a crack at putting it
back into away3D, I think it works and it looks like it is working,
and it looks similar to what used to be in the away3d.projection
method......so if somone could take a look at it, that would be
awesome, will keep trying it out on my end

public function projectVertex(vertex:Number3D,
camera:Camera3D):Number3D
{
var vx:Number = vertex.x;
var vy:Number = vertex.y;
var vz:Number = vertex.z;

var view:Matrix3D = camera.view;
var sz:Number = vx * view.sxz + vy * view.syz + vz * view.szz
+ view.tz;

if (isNaN(sz)) return null;
if (sz*2 <= -camera.focus) return null;

var persp:Number = camera.zoom / (1 + sz / camera.focus);

var projected:Number3D = new Number3D();
projected.z = sz;
projected.x = (vx * view.sxx + vy * view.syx + vz * view.szx
+ view.tx) * persp;
projected.y = (vx * view.sxy + vy * view.syy + vz * view.szy
+ view.ty) * persp;

return projected;
}

Peter Kapelyan

unread,
May 6, 2008, 10:14:36 PM5/6/08
to away3...@googlegroups.com
Hi nathan
 
I don't know if this is what you are looking for, can you try it?


screenPlace(camera, myMovieClip, My3dObject, 1)//1=first vertice
function screenPlace(camera,who,whob,vert):void {
   var result:ScreenVertex;
   result=camera.screen(whob,whob.vertices[vert]);
   who.x=result.x + 300;//half view width
   who.y=result.y + 200;//half view height
  }

myMovieClip or "who" is a Sprite on the flash stage above the view, I tried this a couple of months ago and pasted it to this dev list, I hope it's of any use.
 
-Pete

--
___________________

Actionscript 3.0 Flash 3D Graphics Engine

HTTP://AWAY3D.COM

KeithKong

unread,
May 6, 2008, 11:21:24 PM5/6/08
to away3d.dev
I always get the error that the property vertices doesn't exist in
Object3D. I did a search through the documentation and vertices is
only found when the object is a Mesh. So the code should be:

screenPlace(camera, myMovieClip, My3dObject, 1); //1=first vertice

function screenPlace(camera,who,whob,vert):void {
var result:ScreenVertex;
result=camera.screen(whob,(whob as Mesh).vertices[vert]);
who.x=result.x + 300; //half view width
who.y=result.y + 200; //half view height
}

This seems to work for me. Hope this helped and sorry for changing
the subject a little :)

Keith

Peter Kapelyan

unread,
May 6, 2008, 11:22:15 PM5/6/08
to away3...@googlegroups.com
perfect change of subject :P

Rob Bateman

unread,
May 7, 2008, 5:53:51 AM5/7/08
to away3...@googlegroups.com
Hey Guys

apologies for not getting this sorted sooner. I've been slowly working through the backlog of bugs these last few days, and this one has finally been resolved.

You can get the working code by checking out the trunk version of the googlecode svn

now camera.screen() works as as it should. I've attached a modified demo file showing it in action - hopefully this can be integrated into your own projects now without too much difficulty


atb


Rob
--
Rob Bateman
Flash Development & Consultancy

rob.b...@gmail.com
www.infiniteturtles.co.uk
www.away3d.com
Lesson_Basic_04_Containers.fla
Lesson_Basic_04_Containers.swf

KeithKong

unread,
May 7, 2008, 10:12:40 PM5/7/08
to away3d.dev
It's working, thanks!

On May 7, 2:53 am, "Rob Bateman" <rob.bate...@gmail.com> wrote:
> Hey Guys
>
> apologies for not getting this sorted sooner. I've been slowly working
> through the backlog of bugs these last few days, and this one has finally
> been resolved.
>
> You can get the working code by checking out the trunk version of the
> googlecode svn
>
> now camera.screen() works as as it should. I've attached a modified demo
> file showing it in action - hopefully this can be integrated into your own
> projects now without too much difficulty
>
> atb
>
> Rob
>
>
>
> On Wed, May 7, 2008 at 4:22 AM, Peter Kapelyan <flashn...@gmail.com> wrote:
> > perfect change of subject :P
>
> > On Tue, May 6, 2008 at 11:21 PM, KeithKong <KeithKongGa...@gmail.com>
> > > > HTTP://AWAY3D.COM <http://away3d.com/>
>
> > --
> > ___________________
>
> > Actionscript 3.0 Flash 3D Graphics Engine
>
> > HTTP://AWAY3D.COM
>
> --
> Rob Bateman
> Flash Development & Consultancy
>
> rob.bate...@gmail.comwww.infiniteturtles.co.ukwww.away3d.com
>
>  Lesson_Basic_04_Containers.fla
> 115KDownload
>
>  Lesson_Basic_04_Containers.swf
> 129KDownload

nathan

unread,
May 8, 2008, 5:56:09 AM5/8/08
to away3d.dev
Thanks Rob, still working at putting it in at the momment, but I am
sure it will be fine, makes a big diff...thankyou
> > 129KDownload- Hide quoted text -
>
> - Show quoted text -

nathan

unread,
May 8, 2008, 10:18:07 AM5/8/08
to away3d.dev
Hey Rob,

Just wondering why we need to pass an Object3D to the screen function?

Basically I just to project a 3d point into the current view, so
really I want to be able to use Vertex.project(). I don't think that
seems to be working correctly at the moment, will check it out again
though.

Any ideas?

Nathan.



On May 8, 12:12 pm, KeithKong <KeithKongGa...@gmail.com> wrote:

Rob Bateman

unread,
May 8, 2008, 10:20:53 AM5/8/08
to away3...@googlegroups.com
HEy Nathan

if you want to project a point straight to the screen, use the scene instance in place of the object3d argument

the screen method needs to be told the scope of the vertex position, so that it can perform the necessary matrix multiplication.

cheers

Rob
rob.b...@gmail.com
www.infiniteturtles.co.uk
www.away3d.com

nathan

unread,
May 8, 2008, 6:21:41 PM5/8/08
to away3d.dev
oh ok, there you go, thanks heaps.
> rob.bate...@gmail.comwww.infiniteturtles.co.ukwww.away3d.com- Hide quoted text -
Reply all
Reply to author
Forward
Message has been deleted
0 new messages