Dragging objects in Papervision3D
A few months ago, Andy Zupko added some methods to Papervision3D which facilitated the ability to drag objects in 3d space. I collected those methods and placed them in a static class called RayTracer. The class currently has only one method which returns a Number3D object with the coordinates in 3D space based on the position of your mouse on a Plane3D object.
Andy says:
In lay terms – this basically shoots a ray from the camera, through where the mouse is, into 3D space. You can then do whatever you want knowing where that ray is.
This is the code for the test above:
{
import flash.events.Event;
import org.papervision3d.core.math.Number3D;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.objects.primitives.Sphere;
import org.papervision3d.view.BasicView;
public class Dragging3D extends BasicView
{
private var sphere:Sphere;
public function Dragging3D()
{
initialize();
createObjects();
startRendering();
}
private function initialize():void
{
camera.zoom = 11;
camera.focus = 100;
camera.z = -2000;
camera.y = 200;
}
private function createObjects():void
{
var plane:Plane = new Plane(new ColorMaterial(0x0FFFF00), 1000, 1000, 10, 10);
plane.rotationX = 90;
scene.addChild(plane);
var wireMaterial:WireframeMaterial = new WireframeMaterial();
sphere = new Sphere(wireMaterial, 50, 10, 10);
sphere.useOwnContainer = true;
sphere.y = 25;
scene.addChild(sphere);
}
override protected function onRenderTick(event:Event = null):void
{
super.onRenderTick(event);
var intersect:Number3D = RayTracer.getIntersection(viewport, camera, [0, 1, 0]);
sphere.x = intersect.x;
sphere.z = intersect.z;
}
}
}
The magic happens in the onRenderTick method. This is where we added the RayTracer static method. The first two arguments are self explanatory. The final argument is an array representing the “normal” on an imaginary plane:
[0, 1, 0] = XY plane = objects dragged on the x and z axises
[1, 0, 0] = YZ plane = objects dragged on the y and z axises
[0, 0, 1] = XZ plane = objects dragged on the x and y axises
This is the RayTracer class:
{
import org.papervision3d.core.math.Number3D;
import org.papervision3d.core.math.Plane3D;
import org.papervision3d.core.proto.CameraObject3D;
import org.papervision3d.view.Viewport3D;
public class RayTracer
{
public static function getIntersection(viewport:Viewport3D, camera:CameraObject3D, normal:Array):Number3D
{
var plane3D:Plane3D = new Plane3D(new Number3D(normal[0], normal[1], normal[2]), new Number3D(0, 0, 0));
var cameraPosition:Number3D = new Number3D(camera.x, camera.y, camera.z);
var ray:Number3D = camera.unproject(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
ray = Number3D.add(ray, cameraPosition);
var intersect:Number3D = plane3D.getIntersectionLineNumbers(cameraPosition, ray);
return intersect;
}
}
}
Should be pretty simple to implement. Hit me back if you have any questions!

dont you know how can i drag a collada file , its very imortant for me, thank you
You rock! This is exactly what I needed and was having a bit of difficulty with Andy’s description. Thanks!
Hey! That’s pretty amazing! Could u make a .fla source file? There’s a way to put more objects into the stage???
thanx for the raycasting class!
very helpful!
@strata, the same code should work. just nest your collada in a DO3D.
var dragHolder:DisplayObject3D = new DisplayObject3D();
dragHolder.addChild(yourDAE);
scene.addChild(dragHolder);
Hi,
I am very new to Flash Animation and having difficulty in setting up papervision3d and dowing 3D work. This is exactly what I want to do in my work, but coudn’t work it out. It would be great if you could send me the working sample with all the files.