I’ve made a couple of small improvements to the PaperSprite class:
- The visible face of the plane is updated automatically only when required. This saves on processing load and also means you can just set up the PaperSprite and not have to worry about anything else, just tween the crap out of all its properties and it won’t mind one bit. (Thanks to Jesse for the pointers re. using stage.invalidate for this process)
- The back face is now automatically flipped horizontally, so text and graphics are no longer mirrored and will display correctly
- I’ve changed the logic which sets the dynamic registration point for the pivot. This should now correctly line up the front and back faces of the 3D plane, regardless of where their individual registration points are.
Also, some people have asked how to use it, so here is a really quick example to get you up and running with PaperSprite if you are not familiar with using classes and are using the Flash IDE to compile your movie:
- First, download the PaperSprite class and put the soulwire folder in the same directory as your FLA or source code.
- Then paste this code into the main timeline of your FLA:
// Import the PaperSprite class
import soulwire.display.PaperSprite;
/*
Create a new PaperSprite:
If your front and back faces already exist, you can pass them straight
into the constructor, like so:
myPaperSprite = new PaperSprite( myFrontMc, myBackMc );
*/
var myPaperSprite:PaperSprite = new PaperSprite();
/*
Optionally specify a pivot point, in this example the centre is used
(this is also the default so there is no need to set this normally).
To pivot around the top left use:
myPaperSprite.pivot = new Point(0,0);
or for bottom right:
myPaperSprite.pivot = new Point(1,1);
and so on...
*/
myPaperSprite.pivot = new Point(0.5,0.5);
// Centre it on the stage
myPaperSprite.x = stage.stageWidth / 2;
myPaperSprite.y = stage.stageHeight / 2;
/*
Set the front and back faces:
These can be any type of DisplayObject, for example, to use a MovieClip
from your library use:
myPaperSprite.front = new MyLibraryClip();
*/
myPaperSprite.front = createSprite(0x66FF00);
myPaperSprite.back = createSprite(0xFF3399);
// Add the PaperSprite to the display list
addChild ( myPaperSprite );
// This method just creates coloured boxes for the demo
function createSprite (colour:uint):Sprite
{
var mySprite:Sprite = new Sprite();
mySprite.graphics.beginFill ( colour );
mySprite.graphics.drawRect (0,0,100,100);
mySprite.graphics.endFill ();
return mySprite;
}
// Listen for the enterFrame event
addEventListener ( Event.ENTER_FRAME, spin );
/*
Move and rotate your PaperSprite in any way you want - it will
update it's visible face automatically!
*/
function spin ( event:Event ):void
{
var mX:Number = ((mouseX / stage.stageWidth) - 0.5) * 360;
var mY:Number = ((mouseY / stage.stageHeight) - 0.5) * 360;
myPaperSprite.rotationY += (mX - myPaperSprite.rotationY) / 40;
myPaperSprite.rotationX += (mY - myPaperSprite.rotationX) / 40;
}
Publish the movie and you’re all done!
Background
Flash Player 10’s new 3D capabilities are a much welcomed and exiting addition, and although their implementation at first seemed a little obtuse after using libraries such as Papervision3D, Adobe’s implementation is beginning to make a lot more sense to me. Rather than being handed a complete 3D library, developer’s have been given the necessary building blocks to use 3D, or 2.5D with less ‘hacks’ and arguably better performance where it matters.
Time will tell, but I personally think this will be a positive development for existing frameworks like Papervision and Sandy, because whilst Flash can now natively handle certain tasks like triangle drawing, 3D matrices etc – implementing cameras and shaders, loading and building meshes and so on is something which developers will still require. If this can plug neatly into Flash Player’s capabilities and possibly yield increased performance then all the better.
In more basic situations though, the ability to rotate a DisplayObject around its X, Y and Z axis, as well as finally having a Z property, is useful for adding basic 3D effects, even if your project doesn’t require anything more sophisticated than that.
But this is where some gaps begin to arise. Z sorting, for example, will be required, and Ralph Hauwert (a member of the Papervision3D team) has already written a useful script for this.
Two Sided DisplayObjects
Another common use for Flash Player 10’s 3D capabilities is to create double sided sprites or planes, than can be rotated to reveal alternate content on either side. Again, I would have expected this to be a new property of DisplayObjectContainer, so that front and back could be passed to it as DisplayObjects. This isn’t the case though, so it requires some minimal work around.
Lee Brimelow demonstrated one approach to this, which was to monitor the rotation of a DisplayObject, and swap the visible faces accordingly (Jesse Freeman also used this technique). This works great in many cases, however it doesn’t give the desired results when the parent DisplayObject is off centre and therefore subject to the perspective of the 3D environment. It also means that if you are rotating the DisplayObject around multiple axis then the calculations required get slightly more complex.
The Solution
As ever, the wise and prolific Senocular came to the rescue. He described a technique whereby 3 points are placed on the surface of the DisplayObject, and by determining the winding direction of these points when translated from 3D to 2D space; one is able to determine which way the DisplayObject is facing. Because this is relative to the perspective used to produce this transformation, the result is true to the point of view of the user.
Senocular published his helper class which will return a Boolean value indicating whether a given DisplayObject is front facing or not, and so I wish to stress that all credit is his for his solution and wonderfully clear explanation of the process.
I did feel that it might be beneficial though to wrap this up, along with other functionality, into a class which solved the double sided DisplayObject issue, and so I introduce the PaperSprite class (I chose to call it PaperSprite so that it isn’t confused with Sprite3D if you use another 3D library).
How to use PaperSprite
Creating a two sided Plane using PaperSprite is simple:
import soulwire.display.PaperSprite;
var mySprite:PaperSprite = new PaperSprite();
// Any type of DisplayObject can be used for each face
mySprite.front = new Sprite();
mySprite.back = new Bitmap( myImage );
So you can use any DisplayObject for the front and reverse sides; MovieClips, Sprites, Bitmaps, Shapes, Loaders etc. These can also be set via the constructor…
Another property of PaperSprite is whether is updates it’s display automatically. If true, once the PaperSprite is added to the stage it will update its visible face autonomously, however if you have a more general render loop where you’re controlling your 3D scene from, you can set this to false and then call the update method when calculating the visible face is required:
Thanks to Jesse Freeman for suggesting a method to limit unnecessary calls to the update method. By overriding the setters for the position and rotation properties and using stage.invalidate(), then listening for the render event (Event.RENDER), the calculations needed to determine the visible face of the PaperSprite need only be made if one or more properties have been changed and if the PaperSprite needs to be rendered. Therefore there is no longer a need for autoUpdate, everything is handled internally by the PaperSprite class. Cheers Jesse! :)
…The other useful feature is the pivot property. By default, a PaperSprite will pivot (rotate around) its centre point, which will be automatically calculated for each face depending on its respective dimensions. Alternatively, if you want the PaperSprite to rotate around the top left corner, bottom right, or indeed any point on its surface, you can set the pivot property, which is a Point object and requires 2 normalised values, representing the fraction of the width and height of the PaperSprite at which you want to place the pivot.
So to set the PaperSprite to rotate around its centre (this is set by default) you can use:
mySprite.pivot = new Point( 0.5, 0.5 );
And its Top Left corner:
mySprite.pivot = new Point( 0.0, 0.0 );
And its top centre:
mySprite.pivot = new Point( 0.5, 0.0 );
And so on!
You can also access its isFrontFacing property at any time:
if ( mySprite.isFrontFacing )
{
doSomethingGood();
}
An finally, removing the front or back face from its display list (or indeed not defining one at all) will cause the PaperSprite to display the available face at all times, essentially giving it the default behaviour of a one sided Sprite.
Download the PaperSprite class
Again, this is really just a glorified augmentation of Senocular’s solution to finding the front facing side of a DisplayObject in 3D. All credit should go to him for creating such an elegant solution. I built this class in order to simplify its use and provide the extra functionality needed to quickly start working with a two-sided DisplayObject.
Download: PaperSprite Class & Demo
good job dude, amazing class it helps a lot!
yay – you are good!
yay – you are good!
Nice.
Just tried to use this code in Flex Builder 3 (targeting Flash Player 10). Needs one quick modification…
Should be:
This may be obvious to some, but I’m pretty new at this so it took me a good while to figure this out.
Hi,
I think you done really nice job. How ever I have a lot of problems with collision detection.
I tried:
http://code.google.com/p/collisiondetectionkit/downloads/list
and
http://labs.boulevart.be/index.php/2007/06/08/skinner-collision-detection-in-as3/
Both work ok if I have normal sprites, but it I want to test collision between two PaperSprite objects I got error.
If you know any other open source collisiondetection class that would work with PaperSprite please let me know.
Regards,
Gregor Cepek
Just wanted to throw this out there in regards to pixel precision. I had to modify your class based upon the techniques here:
http://www.flashandmath.com/flashcs4/blursol/index.html
WIthout adding in that code, once a plane is rotated and rotated back to 0, it was not pixel precise. Here is my modification that made it work:
public function set front( value:DisplayObject ):void
{
if ( _front && contains( _front ) )
{
removeChild( _front );
}
var photoXFactor:Number=value.width/(value.width+1);
var photoYFactor:Number=value.height/(value.height+1);
_front = addChild( value );
_front.scaleX = photoXFactor
_front.scaleY = photoYFactor
_front.z = 0
alignFaces();
}
Cheers John.
Yes, you’re right it’s desirable to nullify the 3D transformation matrix once you’re done rotating so that Flash player won’t draw the plane as a bitmap.
I need to update this class as there are a few little bugs and improvements, so I’ll be sure to include this feature.
Thanks!