Problems using a MouseJoint

Software: Away3D 4.x

markuse, Newbie
Posted: 21 April 2013 09:13 PM   Total Posts: 4

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_boxMeshmass);
_boxBody.position = new Vector3D(0,1000,0);
_view.scene.addChild(_boxMesh);
_world.addRigidBody(_boxBody);
_boxMesh.mouseEnabled true;
_boxMesh.addEventListener(MouseEvent3D.MOUSE_DOWNstartDragging);

//here are the two mouse-handlers:

private function startDragging(e:MouseEvent3D):void
{
 _boxMesh
.removeEventListener(MouseEvent3D.MOUSE_DOWNstartDragging);
 
_view.stage.addEventListener(MouseEvent.MOUSE_UPstopDragging);
   
 var 
mouseDownPos:Vector3D = new Vector3D(e.object.xe.object.ye.object.z);
   
 
// add mouse object
 
_mouseBody.position mouseDownPos;
 
_view.scene.addChild(_mouseMesh);
 
_world.addRigidBody(_mouseBody);
   
 
// create the mouse joint
 
_mouseJoint = new AWPPoint2PointConstraint(_mouseBodymouseDownPos_boxBody, new Vector3D());
 
_world.addConstraint(_mouseJointtrue);
   
 
// enable dragging
 
_drag3D = new Drag3D(_viewObjectContainer3D(_mouseMesh), "xy");  
 
_drag3D.offsetCenter false;
 
_dragging true;   
}
  
private function stopDragging(e:MouseEvent):void 
{
 _dragging 
false;
   
 
_boxMesh.addEventListener(MouseEvent3D.MOUSE_DOWNstartDragging);
 
_view.stage.removeEventListener(MouseEvent.MOUSE_UPstopDragging);
   
 
// 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

 

File Attachments
MouseJoint.as  (File Size: 6KB - Downloads: 443)
   

John Brookes, Moderator
Posted: 22 April 2013 10:06 AM   Total Posts: 732   [ # 1 ]

Im just going to dump this on you.
https://gist.github.com/JohnBrookes/62e4c7d57dd2b02a153e

The basics is in onMouseLinkDown function.
Theres lots of other unnecessary stuff in the file, was just a test smile

Looks like this
http://www.shrewballooba.co.uk/chainpick/
double click to start then you can pick the chain.

   

markuse, Newbie
Posted: 22 April 2013 11:06 AM   Total Posts: 4   [ # 2 ]

Hi John,

many many thanks for this quick answer. Your chain-example is exactly what I wanted to achieve. I’ll give your code a deep look and then gonna fix mine.
grin

Thank you very much,
Markus

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X