There doesn’t seem to be much out there explaining how to drag something in 3d space. Recently I’ve had to create some planes that are draggable along one plane (i.e. the z depth doesn’t change but the x and y coordinates will). Doing this involves converting 2D coordinates into 3D coordinates. There is a built in function in Papervision that does this:
InteractiveUtils.getMapCoordAtPointDO3D(…)
So by passing the plane in to this, along with the mouseX and mouseY relative to the plane, we can get all the values needed for dragging.
Please note: I’m using pv3d\as3\tags\1_7\src as my base class, so this isn’t v2.0.
private function _startDrag(e:MouseEvent):void
{
// get the plane from my dictionary where all my planes are
// this is org.papervision3d.objects.Plane;
_dragPlane = dict[e.target].pl;
// start the enterframe function
addEventListener(Event.ENTER_FRAME, _updateDrag);
// get the intial mouseX and Y offset from
// where the user has clicked on the plane
var obj:Object =
InteractiveUtils.getMapCoordAtPointDO3D(_thePlane,
_thePlane.container.mouseX,
_thePlane.container.mouseY);
// save these coordinate offset values to use while dragging
_drag_X_offset = obj.x;
_drag_Y_offset = obj.y;
}
private function _updateDrag(e:Event):void
{
// get the updated position of the plane
var obj:Object =
InteractiveUtils.getMapCoordAtPointDO3D(_thePlane,
_thePlane.container.mouseX,
_thePlane.container.mouseY)
// move the dragged plane to the new postion with the offset
_thePlane.x = _thePlane.x + obj.x - _drag_X_offset;
_thePlane.y = _thePlane.y - obj.y + _drag_Y_offset;
}
Hi , I think this post is very useful but Iam breaking my head how to create draggable plain so that when i drag it will have acceleration of the mouse movement velocity .I tried to use your article but the dragging that came out is not good .The plain behaves quite unpredictable when I drag it . Also I payed attention that the numbers that I get from obj.x and obj.y are very small floating 0. with 10 digits after . Do you have any working example of the implementation you show above?
Thanks
Hi Michael,
I’ll try to post an example in the next few days.
Cheers,
Daniel
Hi Michael,
I’ve just posted a working example with the source available for download. See here: http://blog.sitedaniel.com/2009/09/dragging-in-papervision-3d-2/
You were right about the numbers being too small, it seems the function has changed since this post, returning values 36 times smaller. This was easily fixed though.
Cheers
Daniel