Select Page

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;
}