I am trying to get the derived position and scaling of a 3D Sprite and set them to a 2D Sprite.

I have managed to do the first part like this:

        var p:Point = sprite3d.local3DToGlobal(new Vector3D(0,0,0));
        billboard.x = p.x;
        billboard.y = p.y;

But I can't get the scaling part correctly. I am trying this:

        var mat:Matrix3D = sprite3d.transform.getRelativeMatrix3D(stage); // get derived matrix(?)
        var scaleV:Vector3D = mat.decompose()[2]; // get scaling vector from derived matrix
        var scale:Number = scaleV.length;
        billboard.scaleX = scale;
        billboard.scaleY = scale;

...but the result is apparently wrong.

PS. One might ask what I am trying to achieve. I am trying to create "billboard" 3D sprites, i.e. sprites which are affected by all 3D transformations except rotations, thus they always face the "camera".

  • \$\begingroup\$ I think the scale in the Matrix will have been changed by the rotation of the 3D sprite, in which case you may need to multiply it by the inverse of the rotation matrix? \$\endgroup\$ – Jonathan Connell Oct 7 '12 at 11:27
  • \$\begingroup\$ see a relevant Q&A on SO: stackoverflow.com/questions/12446835/… \$\endgroup\$ – gilad hoch Oct 7 '12 at 11:27
  • \$\begingroup\$ @Jonathan Connell Come again? \$\endgroup\$ – Bill Kotsias Oct 7 '12 at 17:46

You could project each vertex of your 3d sprite to screenspace and then calculate the distance between these points to get the size of the sprite on screen.

To do billboarding, I always find a helper LookAt function is useful. It will orientate your game entity towards a point in space. To billboard you just make it LookAt the camera position. (The alternative is getting the inverse of your view matrix and using that to apply the appropriate rotation).

Here's a link to at LookAt implementation: http://msdn.microsoft.com/en-us/library/windows/desktop/bb281710%28v=vs.85%29.aspx

  • \$\begingroup\$ +1 For simplicity. The result is that the sprite's new Forward vector for its transformation matrix is the opposite of LookAt. \$\endgroup\$ – ChrisC Feb 15 '13 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.