Finding 2D Coordinates of a DisplayObject3D
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.
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)); } } }
-
AlexG
-
Greg Lindquist
-
Rockwolf
-
siaukia
-
jared
-
Joshua Mostafa
-
Holland Risley
-
Seb
Search
Recommended Books
Speaking at FITC Toronto
Recent Posts
- Moving to johnlindquist.com
- AsyncCommand with Robotlegs, Signals, Flight, MinimalComps
- Search Widget – Robotlegs, Signals, Flight, Minimal Comps, Yahoo Astra
- FDT Super Awesome March Deal
- haXe Tutorial
- AS3 Signals Tutorial
- Preferred Video Tutorial Resolution?
- TweenMax – Tweening a timeline (Advanced Tweening)
- Robotlegs + Flight + Union Platform
- Back in the saddle
- Eclipse Theme Designer Preview
- RobotLegs Hello World Video Tutorial
- 10 Things Every Senior Flash Developer Should Know
- Efflex – 3D Effects for Flex
- MorphController – Mighty Morphing Papervision3D
Recent Comments
- Xdragosy on Ribbons
- As on What is BasicView?
- Godie_dito on Dynamic Text on a Plane
- Angel Kaido on Following line
- bambii7 on faq
- Felipe on TweenMax – Tweening a timeline (Advanced Tweening)
- Ocatarinabelachichix on about
- Rajiv on faq
- Rajiv on 3ds max texture baking for Papervision3D
- Anupam Biswas on Maya Texture Baking
Categories
Archives
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

