Switching parents at runtime
This example comes from a conversation I had with Andy. You can actually do a lot of neat tricks by swapping parents, especially when you start rotating the parents.
package { import gs.TweenMax; import org.papervision3d.core.math.Matrix3D; import org.papervision3d.events.InteractiveScene3DEvent; import org.papervision3d.lights.PointLight3D; import org.papervision3d.materials.shadematerials.FlatShadeMaterial; import org.papervision3d.objects.primitives.Sphere; import org.papervision3d.view.BasicView; [SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")] public class SwitchingParentsAtRuntime extends BasicView { private var parent1:Sphere; private var parent2:Sphere; private var childSphere:Sphere; private var light:PointLight3D; public function SwitchingParentsAtRuntime() { var headerText:headerContainer = new headerContainer(); headerText.header.text = "Click a red sphere to set as parent"; addChild(headerText); viewport.interactive = true; light = new PointLight3D(); var parentMaterial:FlatShadeMaterial = new FlatShadeMaterial(light, 0xcc0000); parentMaterial.interactive = true; var childMaterial:FlatShadeMaterial = new FlatShadeMaterial(light, 0x00cc00); parent1 = new Sphere(parentMaterial, 300, 10, 10); parent1.x = -500; parent1.y = 500; parent1.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, objectPressHandler); parent1.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, objectOverHandler); parent1.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, objectOutHandler); parent2 = new Sphere(parentMaterial, 300, 10, 10); parent2.x = 500; parent2.y = -500; parent2.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, objectPressHandler); parent2.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, objectOverHandler); parent2.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, objectOutHandler); childSphere = new Sphere(childMaterial, 100, 10, 10); childSphere.name = "child"; scene.addChild(parent1); scene.addChild(parent2); parent1.addChild(childSphere); childSphere.x = 500; TweenMax.to(parent1, 2, {y:-500, yoyo:true}); TweenMax.to(parent2, 2, {y:500, yoyo:true}); startRendering(); } private function objectPressHandler(event:InteractiveScene3DEvent):void { var parentSphere:Sphere = Sphere(event.target); if(childSphere.parent == parentSphere) { //do nothing }else { var childSphereWorldMatrix:Matrix3D = childSphere.world; var inverse:Matrix3D = new Matrix3D(); var tempParentMatrix:Matrix3D = new Matrix3D(); inverse.calculateInverse(parentSphere.world); tempParentMatrix.calculateMultiply(inverse, childSphereWorldMatrix); childSphere.copyTransform(tempParentMatrix); //get the parent of the child to //remove the child from the parent :) childSphere.parent.removeChild(childSphere); parentSphere.addChild(childSphere); } } private function objectOverHandler(event:InteractiveScene3DEvent):void { viewport.buttonMode = true; } private function objectOutHandler(event:InteractiveScene3DEvent):void { viewport.buttonMode = false; } } }
-
Quantium
-
mark mun
-
Jeff Yamada
Search
Recommended Books
Speaking at FITC Toronto
Recent Posts
- 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
- End dump
- Test if a plane is within the view of the camera (aka testing if culled)
- Materials Reference
- Perlin Blob
Recent Comments
- BAM5 on haXe Tutorial
- AlexG on Finding 2D Coordinates of a DisplayObject3D
- Josh on ActionScript 3 – Model View Controller (MVC)
- martin everett on requests
- martin everett on requests
- lillacska on Dragging Spheres
- Guy Ritchie on MXML without the Flex framework
- Pedro on ActionScript 3 – Namespaces
- daveevolve on AS3DMod Perlin Noise
- sebomoto on haXe Tutorial
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


