Select Page

Dragging in Papervision 3D – example

UPDATE 29/09/09: This technique is possibly no longer valid with the latest version of Papervision3D.


Dragging in Papervision example

Here is a small example I put together quickly today to demonstrate how to drag in 3D using Papervision and the InteractiveUtils.getMapCoordAtPointDO3D function.
Since my last post on this in December last year, the Papervision getMapCoordAtPointDO3D method returned object’s x and y values seemed to have changed by a factor of 36.

UPDATE 29/09/09: This technique is possibly no longer valid with the latest version of Papervision3D.

private function _updateDrag(e:Event):void 
{
    // get the update position on the plane
    var obj:Object = InteractiveUtils.getMapCoordAtPointDO3D(_selectedPlane, 
                                        _selectedPlane.container.mouseX, 
                                        _selectedPlane.container.mouseY)
    // move the dragged plane to the new postion with the offset                
    _selectedPlane.x = _selectedPlane.x + obj.x * 36 - _drag_X_offset;
    _selectedPlane.y = _selectedPlane.y - obj.y * 36  + _drag_Y_offset;
}

Click here to see it in action.

The source files can be downloaded here.

Dragging in PaperVision 3D

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