• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

HARD Question : Z position impact on stage position

New Here ,
Jan 13, 2011 Jan 13, 2011

Copy link to clipboard

Copied

I have a sprite that I zoom out using the sprite.z property. Once the z property has been modified the sprite x and Y values do not match the stage x and y real value. So far so good and what I was expecting!

What I want to accomplish is that when the user clicks the stage the sprite tweens to where the mouse was clicked but I don't know what x and y position to send to the sprite. ( since stage 200,200 is something like 400,400 for the sprite depending on z value)

I tried Utils3D.projectVector using Matrix3D of the perspectiveProjection of the stage without success. (i.e. I get Vector3D(Infinity, Infinity, NaN))

Any pointers?

I could fallback to scalex/scaley but sprites.z gives me the correct scale, perspective projection, paralax scrolling since I have sprites on different z level all for free. I would have to recode all that.

TOPICS
ActionScript

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 13, 2011 Jan 13, 2011

This is an interesting one

The formula to obtain the x coordinate on the screen from a point in 3D space is:

screen x = point x * (distance to screen / distance to point z)  

So, to obtain the 3D point x from the screen x, you'd do:

point x = (screen x * distance to point z) / distance to screen

Translating this to AS3, you'd get:

...

       private function moveObjectTo3DFrom2D(object:DisplayObject, vX:int, vY:int):void {
            var cX:Number = root.transform.perspectiveProjection.projectionCente

...

Votes

Translate

Translate
Participant ,
Jan 13, 2011 Jan 13, 2011

Copy link to clipboard

Copied

Hi,

I haven't played around with the z axis a lot, but have you tried the localToGlobal3D and globalToLocal3D methods?

Cheers,

Rui.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 13, 2011 Jan 13, 2011

Copy link to clipboard

Copied

Boy I feel I'm close with globalToLocal3D!

Unfortunatly the more the sprite if far on the z axis the more the result is offset by many pixels either left or right.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 13, 2011 Jan 13, 2011

Copy link to clipboard

Copied

Well, I tested my script and it works just fine no matter what number you set for z

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 13, 2011 Jan 13, 2011

Copy link to clipboard

Copied

LATEST

I will have to give you reason since if I use :

a- Your code

b- GlobalToLocal3D

c- Sprite.MouseX / MouseY

I get the same X,Y values but with an offset so the problem must be with me and my Stage setup.

Great Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 13, 2011 Jan 13, 2011

Copy link to clipboard

Copied

This is an interesting one

The formula to obtain the x coordinate on the screen from a point in 3D space is:

screen x = point x * (distance to screen / distance to point z)  

So, to obtain the 3D point x from the screen x, you'd do:

point x = (screen x * distance to point z) / distance to screen

Translating this to AS3, you'd get:

...

       private function moveObjectTo3DFrom2D(object:DisplayObject, vX:int, vY:int):void {
            var cX:Number = root.transform.perspectiveProjection.projectionCenter.x;
            var cY:Number = root.transform.perspectiveProjection.projectionCenter.y;
            var f:Number = root.transform.perspectiveProjection.focalLength;
            with(object){
                x = cX + ((f + object.z)*(vX - cX))/f;
                y = cY + ((f + object.z)*(vY - cY))/f;
            }
        }

...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines