[away3d] Added Drag3D class

127 views
Skip to first unread message

Fabrice3D

unread,
May 21, 2010, 4:49:23 AM5/21/10
to away3...@googlegroups.com
Hi all,
I get many requests about this and I keep sending code...

So I've added a new class dedicated to this particular functionality.
Drag3D. in tools.utils package


you can use it to drag an object or just to get the intersection back.

here an example of implementation

import away3d.tools.utils.Drag3D
[... usual away code...]
private var drag3d:Drag3D;

private function setUp():void
{
//the object you want to drag
var _debugPrimitive:Sphere = new Sphere({radius:50, material:null});
this.scene.addChild(_debugPrimitive);

//declare instance
drag3d = new Drag3D(this.view);
//which plane do you want to drag on
// note here that plane != Plane primitive!!
drag3d.plane = "xz";
// in case you want to check what you are doing
//drag3d.debug = true;
// to assign an object to be dragged
drag3d.object3d = _debugPrimitive;
}


private function myEnterFrame(event:Event = null):void
{
//will update the object automatically on mouse moves
drag3d.updateDrag(); // its is of course handy to couple this with mouseEvents, like "if mouseisdown --> drag"

// in case you want only the position intersect back
var intersect:Number3D = drag3d.getIntersect();

this.view.render();
}

next to 0,0,0 default, there are also options for custom positions of plane, in case of AABB tests and even support for rotated planes
if you want object aligned calcs...

here a little tech demo
http://www.closier.nl/playground/drag/drag3d.swf

cheers,


Fabrice

Michael Iv

unread,
May 21, 2010, 5:03:50 AM5/21/10
to away3...@googlegroups.com
Yahoooooo! Fabrice , you rock man!

Sent from my iPhone

qatab chakir

unread,
May 22, 2010, 6:30:21 AM5/22/10
to away3...@googlegroups.com
Very useful classe , makes useing drag very simple , Thanks fab for this classe & demo

chakas

2010/5/21 Michael Iv <explo...@gmail.com>

elguapoloco

unread,
May 22, 2010, 8:46:45 AM5/22/10
to Away3D.dev
Brilliant!

Thank you.

abowman

unread,
May 31, 2010, 12:09:58 PM5/31/10
to Away3D.dev
Thanks for the class Fabrice.

For some reason, when I move the mouse the sphere I'm dragging doesn't
stay under the mouse.

http://abowman.nozonenet.com/away3d/dragTest.swf
http://abowman.nozonenet.com/away3d/DragTest.as

What do I need to do to fix this?

Thanks
>                         varintersect:Number3D = drag3d.getIntersect();

Guitouxx

unread,
May 31, 2010, 12:16:17 PM5/31/10
to Away3D.dev
Awesome, I will play with it :))

Great contribution

Guitouxx

Fabrice3D

unread,
May 31, 2010, 4:54:51 PM5/31/10
to away3...@googlegroups.com
looked very quickly at your code,
since class caches few values to spare calcs, try call resize(); before you do setup the class.

let me know how it goes, I will in case it doesn't fix your issue, try your class tomorrow.

also one detail, camera is standard of type Camera3D
so view.camera.x, y, z is enough, no need to declare one.

Fabrice

abowman

unread,
May 31, 2010, 7:08:13 PM5/31/10
to Away3D.dev
I tried calling the onResize function at the beginning of my init
function and that didn't solve the problem.

Fabrice3D

unread,
Jun 1, 2010, 3:18:12 AM6/1/10
to away3...@googlegroups.com
I'll look at it later on today...

Sent from an iPhone without Flash

Fabrice3D

unread,
Jun 1, 2010, 5:25:59 AM6/1/10
to away3...@googlegroups.com
I haven't debugged your code, (that would be your job :)) )
but made quick test class using latest 3.5 to test class itself, all seams fine to me.

I would check if the mouse coordinates are correct in your case.

Fabrice


package{
import flash.display.*;
import flash.events.*;
import away3d.containers.Scene3D;
import away3d.containers.View3D;
import away3d.core.base.Object3D;
import away3d.core.render.*;
import away3d.cameras.HoverCamera3D;
import away3d.events.MouseEvent3D;
import away3d.primitives.Sphere;

import away3d.tools.Drag3D;

public class Drag3DTest extends Sprite
{
public var scene:Scene3D;
private var view:View3D;
private var drag3d:Drag3D;

private var cameraDistance:Number = 3000;
private var Hcamera:HoverCamera3D;

private var move:Boolean = false;
private var lastPanAngle:Number;
private var lastTiltAngle:Number;
private var lastMouseX:Number;
private var lastMouseY:Number;

public function Drag3DTest()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event):void
{
initSWFEnvironement();
populate();
}

private function initSWFEnvironement():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.stageFocusRect = false;

Hcamera = new HoverCamera3D();
view = new View3D({camera:Hcamera});
scene = view.scene;
view.x = stage.stageWidth * .5;
view.y = stage.stageHeight * .5;
Hcamera.distance = cameraDistance;
Hcamera.tiltAngle = Hcamera.panAngle = 45;

addChild(view);
//scene.addChild(new Trident(1000, true));

start();
}

public function start():void
{
if(!hasEventListener(Event.ENTER_FRAME)){
addEventListener(Event.ENTER_FRAME, refreshScreen);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseIsDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseIsUp);
stage.addEventListener(Event.RESIZE, onResize);
}
}


private function populate():void
{


var _debugPrimitive:Sphere = new Sphere({radius:50, material:null});

scene.addChild(_debugPrimitive);

drag3d = new Drag3D(view);
drag3d.plane = "xz";
drag3d.debug = true;
drag3d.object3d = _debugPrimitive;

Hcamera.hover(true);

}

private function refreshScreen(event:Event = null):void
{

if (move) {
Hcamera.panAngle = 0.7*(stage.mouseX - lastMouseX) + lastPanAngle;
Hcamera.tiltAngle = 0.7*(stage.mouseY - lastMouseY) + lastTiltAngle;
}

Hcamera.hover();

drag3d.updateDrag();

view.render();

}

private function onResize(event:Event):void
{
view.x = stage.stageWidth *.5;
view.y = stage.stageHeight *.5;
}

private function onMouseIsDown(event:MouseEvent):void
{
lastPanAngle = Hcamera.panAngle;
lastTiltAngle = Hcamera.tiltAngle;
lastMouseX = stage.mouseX;
lastMouseY = stage.mouseY;
move = true;
stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}

private function onMouseIsUp(event:MouseEvent):void
{
move = false;
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}

private function onStageMouseLeave(event:Event):void
{
move = false;
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);

deep

unread,
Jun 1, 2010, 4:46:03 PM6/1/10
to Away3D.dev
What about only "x" or "y" or "z" drag? Like is more 3d editors or in
preFab.

Fabrice3D

unread,
Jun 1, 2010, 5:26:42 PM6/1/10
to away3...@googlegroups.com
thats exactly what it does... except the 2 chars defines the plane its on.
so if you want drag only on x, your are on xz plane for instance. but you could as well drag on xy plane.
You are also free to lock results using the getIntersection and use only the value you want.

Prefab uses a more complex drag system, but yes it always comes down to ray/plane intersection anyway.
Just think this class might help few to properly implement drag in 3d (and the easy way) into their apps/games.

Fabrice

abowman

unread,
Jun 1, 2010, 6:37:05 PM6/1/10
to Away3D.dev
Thanks for the test class.

I was able to reproduce my problem in your code by placing the camera
closer to the sphere and making the sphere smaller. Is there a bug in
the Drag3D class or am I just not using the camera correctly?

http://abowman.nozonenet.com/away3d/Drag3DTest.as
http://abowman.nozonenet.com/away3d/Drag3DTest.swf

Thanks for your help.

BTW, I purchased the new Away3D book and I'm finding it very helpful.
I was able to get my code working using that, but would really like to
be able to use this Drag3D class rather than using a plane primitive.

Fabrice3D

unread,
Jun 2, 2010, 4:24:28 AM6/2/10
to away3...@googlegroups.com
interresting...
so I guess, fix would be to project from camera pos instead of mouse using same ray dir...
thx for pointing that out, will try implement a fix to it asap.

in meanwhile keep away from cam :))

Fabrice

Michael Iv

unread,
Jul 23, 2010, 9:51:53 AM7/23/10
to away3...@googlegroups.com
There is a bug in Drag3D class, Actually it is just syntax miss, If you set the plane to "zy" it would throw you " Unvalid plane description error ...", because someone was writting it probably at 3 am ;)(Fabrice?) und did not put after the last caase break ; terminator .So please , if someone can update it on the trunk.
Nice weekend to everybody...

Fabrice3D

unread,
Jul 23, 2010, 10:48:00 AM7/23/10
to away3...@googlegroups.com
thx Michael,
fixed and in trunk

because someone was writting it probably at 3 am ;)(Fabrice?)
probably true :)

Fabrice

Michael Iv

unread,
Jul 24, 2010, 11:00:46 AM7/24/10
to away3...@googlegroups.com
found two more places where break; misses...
And it seems that only "xz" mode works well.

Fabrice3D

unread,
Jul 24, 2010, 11:08:05 AM7/24/10
to away3...@googlegroups.com
just added these,
tho it was not generating any weird behaviors on my tests

for test just made quick testclass, I was able drag on 3 planes with no errors
(sended you the test class)

Fabrice

Michael Iv

unread,
Jul 24, 2010, 11:20:42 AM7/24/10
to away3...@googlegroups.com
Yeah, now when I update drag only on Mouse3D press it works ok(may be a little fast transformation) but putting it onEnterFrame throws all sort of NaN from Lens as well as the plains start flying away with the speed of light
Reply all
Reply to author
Forward
0 new messages