Dragging an object to rotate
The cool thing about this example is you can really just drop any DisplayObject3D in to rotate it around. It’s not super exciting, but I bet many of you will find it useful.
package { import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; import org.papervision3d.core.math.Matrix3D; import org.papervision3d.core.math.Number3D; 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 Trackball extends BasicView { private static const FORWARD:Number3D = new Number3D(0, 0, 1); private var sphere:Sphere; private var previousMousePoint:Point = new Point(); private var isMouseDown:Boolean = false; public function Trackball() { var light:PointLight3D = new PointLight3D(); var flatShadeMaterial:FlatShadeMaterial = new FlatShadeMaterial(light, 0xcc0000, 0x222222); sphere = new Sphere(flatShadeMaterial, 500, 32, 24); scene.addChild(sphere); startRendering(); stage.addEventListener(MouseEvent.MOUSE_DOWN, stage_mouseDownHandler); stage.addEventListener(MouseEvent.MOUSE_UP, stage_mouseUpHandler); } private function stage_mouseDownHandler(event:MouseEvent):void { isMouseDown = true; } private function stage_mouseUpHandler(event:MouseEvent):void { isMouseDown = false; } override protected function onRenderTick(event:Event=null):void { var currentMousePoint:Point = new Point(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY); if(isMouseDown) { var difference:Point = currentMousePoint.subtract(previousMousePoint); var vector:Number3D = new Number3D(difference.x, difference.y, 0); var rotationAxis:Number3D = Number3D.cross(vector, FORWARD); rotationAxis.normalize(); var distance:Number = Point.distance(currentMousePoint, previousMousePoint); var rotationMatrix:Matrix3D = Matrix3D.rotationMatrix(rotationAxis.x, -rotationAxis.y, rotationAxis.z, distance/250); sphere.transform.calculateMultiply3x3(rotationMatrix, sphere.transform); } previousMousePoint = currentMousePoint; super.onRenderTick(event); } } }
-
derkoidus
-
Dave
-
Kosta
-
Scott
-
yukimi
-
richard
-
yukimi
-
mca
-
harry
-
lwz7512
-
dada
Search
Recommended Books
Speaking at FITC Toronto
Recent Posts
- 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
- End dump
- Test if a plane is within the view of the camera (aka testing if culled)
- Materials Reference
Recent Comments
- KD on ActionScript 3 – Model View Controller (MVC)
- 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
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


