I’m experimenting with a simulation of a “MouseJoint”, like the one I know from Box2D (see e.g. http://shiftarray.com/away3d-with-box2d-physics/).
For a project I need to work the MouseJoint in real 3D, not only in a look-alike-3D as in the former example. My idea was to create a “MouseObject” at the position where the user clicks at the draggable object. Then create a AWPPoint2PointConstraint with the MouseObject and the draggable box as the two RigidBodies. Then create a Drag3D-Object for the MouseObject and update that in the render-loop. Unfortunately something must be really wrong in my code, because the result looks quite weird.
So please, is there anybody out there to show me how it works with AwayPhysics?
Here’s what I got so far:
//create a draggable box
var size:int = 400;
var mass:int = 10;
var mat:ColorMaterial = new ColorMaterial(0xfc6a11);
mat.lightPicker = _lightPicker;
_boxMesh = new Mesh(new CubeGeometry(size,size,size),mat);
var shape:AWPBoxShape = new AWPBoxShape(size,size,size);
_boxBody = new AWPRigidBody(shape, _boxMesh, mass);
_boxBody.position = new Vector3D(0,1000,0);
_view.scene.addChild(_boxMesh);
_world.addRigidBody(_boxBody);
_boxMesh.mouseEnabled = true;
_boxMesh.addEventListener(MouseEvent3D.MOUSE_DOWN, startDragging);
//here are the two mouse-handlers:
private function startDragging(e:MouseEvent3D):void
{
_boxMesh.removeEventListener(MouseEvent3D.MOUSE_DOWN, startDragging);
_view.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
var mouseDownPos:Vector3D = new Vector3D(e.object.x, e.object.y, e.object.z);
// add mouse object
_mouseBody.position = mouseDownPos;
_view.scene.addChild(_mouseMesh);
_world.addRigidBody(_mouseBody);
// create the mouse joint
_mouseJoint = new AWPPoint2PointConstraint(_mouseBody, mouseDownPos, _boxBody, new Vector3D());
_world.addConstraint(_mouseJoint, true);
// enable dragging
_drag3D = new Drag3D(_view, ObjectContainer3D(_mouseMesh), "xy");
_drag3D.offsetCenter = false;
_dragging = true;
}
private function stopDragging(e:MouseEvent):void
{
_dragging = false;
_boxMesh.addEventListener(MouseEvent3D.MOUSE_DOWN, startDragging);
_view.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);
// remove the mouse joint
_world.removeConstraint(_mouseJoint);
_mouseJoint = null;
// remove the mouse object
_view.scene.removeChild(_mouseMesh);
_world.removeRigidBody(_mouseBody);
}
// and my render loop:
private function loop(e:Event) : void
{
_world.step(timeStep);
_debugDraw.debugDrawWorld();
if(_dragging) {
_drag3D.updateDrag();
_mouseBody.position = _mouseMesh.position;
}
_view.render();
}
Whole AS-File is attached below.
Any help is highly appreciated.
Thanks,
Markus