Finding 2D Coordinates of a DisplayObject3D

Monday, November 17th, 2008 | examples

I see this question pop up rather frequently, so I thought I’d make a quick demo.

First the 3d plane will tween to a random x, y, and z, then the 2d movieclip will tween to the plane’s local screen x and y. The 2d movieclip is also showing the current x and y in text of the plane for reference.

View example
source

package {
	import gs.TweenMax;
	import gs.easing.*;
 
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class Finding2DCoordinates extends BasicView
	{
		private var plane:Plane;
		private var box2D:blue2DBox;
 
		public function Finding2DCoordinates()
		{
			var planeMaterial:ColorMaterial = new ColorMaterial(0xcc0000);
 
			plane = new Plane(planeMaterial);
			box2D = new blue2DBox();	
 
			scene.addChild(plane);
			addChild(box2D);
 
			plane.autoCalcScreenCoords = true; //Turn screen coords on
 
			startRendering();
 
			tweenPlane();
		}
 
		private function tweenPlane():void
		{
			var xTarget:Number = Math.random() * 1500 - 750;	
			var yTarget:Number = Math.random() * 1500 - 750;
			var zTarget:Number = Math.random() * 1500;
 
			var tweenObject:Object = {};
			tweenObject.x = xTarget;
			tweenObject.y = yTarget;
			tweenObject.z = zTarget;
			tweenObject.onComplete = tweenBox;
			tweenObject.onUpdate = updateText;	
 
			TweenMax.to(plane, 1, tweenObject);
		}
 
		private function tweenBox():void
		{
			//plane's 2d x and y
			var xTarget:Number = plane.screen.x + this.width/2;
			var yTarget:Number = plane.screen.y + this.height/2;
 
			var tweenObject:Object = {};
			tweenObject.x = xTarget;
			tweenObject.y = yTarget;
			tweenObject.onComplete = tweenPlane;	
 
			tweenObject.ease = Quad.easeInOut;
 
			TweenMax.to(box2D, 1, tweenObject);
		}
 
		private function updateText():void
		{
			box2D.xText.text = "x: " + String(Math.floor(plane.screen.x + this.width/2));
			box2D.yText.text = "y: " + String(Math.floor(plane.screen.y + this.height/2));
		}
	}
}

Tags: ,

  • I didnt try this code but it should not work without calculateScreenCoords() method.
    I use the same .screen property of the DisplayObject3D class with PV3D 2 , and .screen gives real result only if you call first the calculateScreenCoords() method
  • Greg Lindquist
    Rockwolf.

    Yeah, I'm seeing similar results. It doesn't have to be the tween engine though, but with almost every event. I cannot get coordinates to work properly with the FileLoadEvent.LOAD_COMPLETE event listener. But I can if I use the InteractiveScene3DEvent.OBJECT_OVER.

    Kind of interesting, I think.
  • Rockwolf
    Thats awesome. However, is it just me, or does the calculateScreenCoords() method only work properly on a DisplayObject3D that has been tweened?
  • thanks man, this really brighten up my day a lot in my recent tight project. you’re the saver! thanks again for sharing
  • jared
    thanks man, saved me.
  • Keep up the good work!
  • Thanks for writing up these demos. Papervision can be really tricky due to the lack of documentation and examples out there. Great job dude, I am sure this will be very useful to lots of people. PV3d is awesome :)
  • Seb
    Excellent! I'm sure this will come in very handy for me in the future. Cheers! :D
blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

November 2008
M T W T F S S
    Dec »
 12
3456789
10111213141516
17181920212223
24252627282930

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...