Drag when zoom to a limit.

Software: Away3D 4.x

ytr2345, Newbie
Posted: 11 September 2013 03:55 AM   Total Posts: 22

Hi, how to make like this example (http://custom.sendra.com), it’s become drag when zoom to a limit and become rotate normal when zoom out of limit, anyone know that code?
Thanks.

   

Avatar
SharpEdge, Member
Posted: 11 September 2013 08:38 AM   Total Posts: 94   [ # 1 ]

I think they used a simple HoverController for distant camera.
Instead for near camera an edited version of HoverController that keeps pan as is, and moves the target of LookAtController vertically of the offset generated by the mouse.

If you need a clearer explanation ask.

   

ytr2345, Newbie
Posted: 11 September 2013 09:40 AM   Total Posts: 22   [ # 2 ]

Thanks for reply but i’m really new in away so can you write that code for me? smile

   

Avatar
SharpEdge, Member
Posted: 11 September 2013 10:52 AM   Total Posts: 94   [ # 3 ]

NOT TESTED, i’ve not time right now, maybe later…
This is a really fast example, many controls and improvements are missing, this is just to give you an idea.

Usage: use it as any controller (but you don’t have to set manually the values because it gets them automatically).

create instance: camCtrl = new HoverDragController(view,camera);
update on enterframe: camCtrl.update();

import flash.events.Event;
 
import flash.events.MouseEvent;
 
import flash.geom.Vector3D;
 
 
import away3d.containers.View3D;
 
import away3d.controllers.HoverController;
 
import away3d.entities.Entity;
 
 public class 
HoverDragController extends HoverController
 {
  
private var _view View3D;
  
  private var 
_zoomThr Number;
  
  private var 
_move:Boolean false;
  private var 
_lastPanAngle:Number;
  private var 
_lastTiltAngle:Number;
  private var 
_lastMouseX:Number;
  private var 
_lastMouseY:Number;
  private var 
_newDistance Number;
  
  private var 
_lastVOffset Number;
  
  public function 
HoverDragController(view:View3DtargetObject:Entity=nullzoomThr:Number 500panAngle:Number=0tiltAngle:Number=0distance:Number=1000minTiltAngle:Number=15maxTiltAngle:Number=75minPanAngle:Number=NaNmaxPanAngle:Number=NaNsteps:uint=8yFactor:Number=2wrapPanAngle:Boolean=false)
  
{
   _view 
view;   
   
_zoomThr zoomThr
   
   
addListeners();
   
   
super(targetObjectnullpanAngletiltAngledistanceminTiltAnglemaxTiltAngleminPanAnglemaxPanAnglestepsyFactorwrapPanAngle);
   
   
_newDistance distance;
   
_lastPanAngle panAngle;
   
_lastTiltAngle tiltAngle;
  
}
  
  
public function dispose():void
  {
   removeListeners
();
   
_view null;
  
}
  
  
private function addListeners():void
  { 
   
// Setup event listeners
   
_view.addEventListener(MouseEvent.MOUSE_DOWNmouseDownHandler);
   
_view.addEventListener(MouseEvent.MOUSE_UPmouseUpHandler);
   
_view.addEventListener(MouseEvent.MOUSE_WHEELwheelZoomHandlerfalse0true);   
  
}
  
  
  
private function removeListeners():void
  {    
   _view
.removeEventListener(MouseEvent.MOUSE_DOWNmouseDownHandler);
   
_view.removeEventListener(MouseEvent.MOUSE_UPmouseUpHandler);
   
_view.removeEventListener(MouseEvent.MOUSE_WHEELwheelZoomHandlerfalse);
  

  
  
protected function wheelZoomHandler(event:MouseEvent):void
  {
   _newDistance 
*= - (event.delta/10);   
   
_newDistance Math.max(50Math.min(3000_newDistance));
  

  
  
  
// --------------------------------------------------------------------------------------------------------------
  /*** Mouse down handler */
  
private function mouseDownHandler(e:MouseEvent):void
  {
   _lastPanAngle 
panAngle;
   
_lastTiltAngle tiltAngle;
   
_lastVOffset lookAtPosition.y;
   
_lastMouseX _view.mouseX;
   
_lastMouseY _view.mouseY;
   
_move true;
   
_view.addEventListener(Event.MOUSE_LEAVEonStageMouseLeave);
  
}
  
// --------------------------------------------------------------------------------------------------------------
  // --------------------------------------------------------------------------------------------------------------
  /*** Mouse up handler */
  
private function mouseUpHandler(e:MouseEvent):void
  {
   _move 
false;
   
_view.removeEventListener(Event.MOUSE_LEAVEonStageMouseLeave);
  
}
  
// --------------------------------------------------------------------------------------------------------------
  // --------------------------------------------------------------------------------------------------------------
  /*** Mouse stage leave handler */
  
private function onStageMouseLeave(e:Event):void
  {
   _move 
false;
   
_view.removeEventListener(Event.MOUSE_LEAVEonStageMouseLeave);
  
}
  
// --------------------------------------------------------------------------------------------------------------
  
  
  
override public function update(interpolate:Boolean=true):void
  { 
   
if(this.distance != _newDistance)
   
{
    
if(Math.abs(this.distance _newDistance) > 0.001 && interpolate)
     
this.distance += (_newDistance this.distance) * (_view.deltaTime/1000) * 2;
    else
     
this.distance _newDistance;
    
    if(
this.distance _zoomThr)
    
{
     
//Where you want it to look when distant
     
this.lookAtPosition = new Vector3D();
    
}
    
else
    
{
     this
.tiltAngle 0;
    
}
   }
   
   
if(_move)
   
{
    this
.panAngle 0.3 * (_view.mouseX _lastMouseX) + _lastPanAngle;
    
    if(
distance _zoomThr)
    
{
     this
.tiltAngle 0.3 * (_view.mouseY _lastMouseY) + _lastTiltAngle;     
    
}
    
else
    
{     
     this
.lookAtPosition.0.5 * (_view.mouseY _lastMouseY) + _lastVOffset
    
}
   }
   
   super
.update(interpolate);
  
}
 } 
   

ytr2345, Newbie
Posted: 11 September 2013 12:43 PM   Total Posts: 22   [ # 4 ]

It’s got error:
C:\Users\qwe\Documents\New Project2\src\HoverDragController.as(30): col: 4 Error: A super statement can be used only inside class instance constructors.
super(targetObject, null, panAngle, tiltAngle, distance, minTiltAngle, maxTiltAngle, minPanAngle, maxPanAngle, steps, yFactor, wrapPanAngle);
^
Build halted with errors (fcsh).
.
How to fix it?

   

Avatar
SharpEdge, Member
Posted: 11 September 2013 01:21 PM   Total Posts: 94   [ # 5 ]

Ok i’ve updated the code, now i’ve tested it…obviously it’s not smoothed and good as the one you saw…

About the error, are you shure that you have copied the code inside a new HoverDragController.as file with the correct package?

   

ytr2345, Newbie
Posted: 11 September 2013 01:47 PM   Total Posts: 22   [ # 6 ]

I’ve update the code but still got that error. smile

   

Avatar
SharpEdge, Member
Posted: 11 September 2013 02:03 PM   Total Posts: 94   [ # 7 ]

I’ve tested it with away 4.1.4, is the same version that are you using?

Have you added correct “package” statement?

   

ytr2345, Newbie
Posted: 12 September 2013 03:15 AM   Total Posts: 22   [ # 8 ]

OMG Thanks you so much SharpEdge, i did it, it work perfectly but only one thing, can you make it limit the drag? smile (sr because i’m asking you too much)

   

Avatar
SharpEdge, Member
Posted: 12 September 2013 09:40 AM   Total Posts: 94   [ # 9 ]

Oh it’s really simple change the line at the end to:
This.lookatposition.y=math.max (MINOFFSET , Math.mim (MAXOFFSET,  0.5 * (_view.mouseY - _lastMouseY) + _lastVOffset)); 

   

ytr2345, Newbie
Posted: 12 September 2013 10:48 AM   Total Posts: 22   [ # 10 ]

Only one small problems yet, when zoom in, it change from rotate normal to drag very smooth but when zoom out, it’s hitch, not smooth, can you fix it?

   

Avatar
SharpEdge, Member
Posted: 12 September 2013 03:19 PM   Total Posts: 94   [ # 11 ]

As i said is nowhere near a perfect solution…it should be done with another approach to get a performant control. I’ve not time now, sorry, but i think that you have a good starting point.
Maybe when i’m not busy i’ll write a better solution.

What i can say to help you solve your problem is to smooth the variation of “lookAtPosition”.

   

ytr2345, Newbie
Posted: 17 September 2013 01:02 AM   Total Posts: 22   [ # 12 ]

Thanks, please help me when you have free time.wink

   

ytr2345, Newbie
Posted: 30 September 2013 03:37 AM   Total Posts: 22   [ # 13 ]

Can anyone make it smooth when zoom out plz…

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X