Drag3D doesn’t appear to work when using an Orthographic lens

Software: Away3D 4.x

OEGInc, Newbie
Posted: 28 March 2014 04:16 AM   Total Posts: 27

Ok, so I’m trying to create a library for doing Isometric games in Away3D (because I like torturing myself apparently).

I’ve drawn a grid of boxes on the X/Z axis and now I’m trying to determine what the position of a user click is in X/Z space assuming the plane is @ 0 Y.

I’m sure there are several ways of doing this, but I decided to try using the Drag3D class as it seemed the simplest.

Everything works just fine using the default “Perspective” lens, but if I simply change the lens to Orthographic the intersect numbers are all over the place.

Am I asking too much?  Is there a better way of calculating this?  The camera will not be at a fixed position, so I need to be able to calculate the click based on the current view.

Thanks in advance!
—Rob

   

John Brookes, Moderator
Posted: 29 March 2014 11:54 AM   Total Posts: 732   [ # 1 ]

There is a bug with Orthographic and fix here
https://github.com/away3d/away3d-core-fp11/issues/683

   

OEGInc, Newbie
Posted: 31 March 2014 02:11 AM   Total Posts: 27   [ # 2 ]

Thanks for the tip.  I applied the fix, and it did seem to help somewhat, but the locations are still very off.

Here is a link to a sample application:
Drag3D Sample Application


And here is the source code:

package
{
 import flash
.display.Sprite;
 
import flash.display.StageAlign;
 
import flash.display.StageScaleMode;
 
import flash.events.Event;
 
import flash.events.KeyboardEvent;
 
import flash.geom.Vector3D;
 
import flash.text.TextField;
 
import flash.ui.Keyboard;
 
 
import away3d.cameras.lenses.OrthographicLens;
 
import away3d.cameras.lenses.PerspectiveLens;
 
import away3d.containers.View3D;
 
import away3d.entities.Mesh;
 
import away3d.materials.ColorMaterial;
 
import away3d.primitives.PlaneGeometry;
 
import away3d.primitives.WireframePlane;
 
import away3d.tools.utils.Drag3D;
 
 
[SWF(backgroundColor="#777777"frameRate="60"width="1024"height="768")]
 
public class Main extends Sprite
 {
  
private const MESH_SIZE:uint 16;
  private const 
GRID_SIZE:uint 32;
  
  private var 
_view:View3D;
  private var 
_dragMesh:Mesh;
  private var 
_drag3D:Drag3D;
  private var 
_lensPersp:PerspectiveLens = new PerspectiveLens();
  private var 
_lensOrtho:OrthographicLens = new OrthographicLens();
  private var 
_orthoView:Boolean false;
  private var 
_txtLensType:TextField = new TextField();
  private var 
_txtLocation:TextField = new TextField();
  
  public function 
Main()
  
{
   
if (stageinit();
   else 
addEventListener(Event.ADDED_TO_STAGEinit);
  
}
  
  
private function init(e:Event null):void 
  {
   removeEventListener
(Event.ADDED_TO_STAGEinit);

   
stage.align     StageAlign.TOP_LEFT;
   
stage.scaleMode StageScaleMode.NO_SCALE;
  
   
// Create our 3D view
   
stage.addChild(_view = new View3D());
   
   
// Create our Orthographic camera and position it nicely
   
_view.camera.lens _lensPersp;
   
_view.camera.= (MESH_SIZE GRID_SIZE) + ((MESH_SIZE GRID_SIZE) / 4);
   
_view.camera.= (MESH_SIZE GRID_SIZE);
   
_view.camera.= (MESH_SIZE GRID_SIZE) + ((MESH_SIZE GRID_SIZE) / 4);
   
_view.camera.lookAt(new Vector3D( ((MESH_SIZE GRID_SIZE) >> 1), 0, ((MESH_SIZE GRID_SIZE) >> 1) ));

   
// Create a white wireframe grid on the XZ plane
   
var grid:WireframePlane;
   
grid = new WireframePlane(MESH_SIZE GRID_SIZEMESH_SIZE GRID_SIZEMESH_SIZEMESH_SIZE0xFFFFFF1"xz");
   
grid.= (MESH_SIZE GRID_SIZE) >> 1;
   
grid.= (MESH_SIZE GRID_SIZE) >> 1;
   
_view.scene.addChild(grid);   

   
// Setup our mesh that we will drag around
   
var dragMaterial:ColorMaterial = new ColorMaterial(0x0000FF0.6);
   var 
dragPlane:PlaneGeometry = new PlaneGeometry(GRID_SIZEGRID_SIZE11truefalse);
   
_dragMesh = new Mesh(dragPlanedragMaterial);
   
_dragMesh.1;
   
_dragMesh.visible false;
   
_view.scene.addChild(_dragMesh);
   
   
// Initialize Drag3D to get our mouse position
   
_drag3D = new Drag3D(_view);
   
_drag3D.debug true// This shows what plane it's using

   // Setup our display
   
_txtLensType.text "Current Lens: Perspective";
   
_txtLensType.textColor 0xFFFFFF;
   
_txtLensType.width 400;
   
_txtLensType._view.stage.stageHeight 40;
   
_view.addChild(_txtLensType);
  
   
_txtLocation.text "X: 0   Z: 0";
   
_txtLocation.textColor 0xFFFFFF;
   
_txtLocation.width 400;
   
_txtLocation._view.stage.stageHeight 20;
   
_view.addChild(_txtLocation);

   
// Setup our listeners
   
_view.stage.addEventListener(Event.RESIZE,         onResize    );
   
_view.stage.addEventListener(Event.ENTER_FRAME,    onEnterFrame);
   
_view.stage.addEventListener(KeyboardEvent.KEY_UPonKeyUp   );

   
// Size our screen properly
   
onResize();
  
}

  
protected function onKeyUp(event:KeyboardEvent):void
  {
   
if (event.keyCode == Keyboard.SPACE)
   
{
    _orthoView 
= !_orthoView;

    if (
_orthoView)
    
{
     _view
.camera.lens _lensOrtho;
     
_txtLensType.text "Camera Lens: Orthographic";
    
}
    
else
    
{
     _view
.camera.lens _lensPersp;
     
_txtLensType.text "Camera Lens: Perspective";
    
}
   }
  }
  
  
protected function onResize(event:Event null):void
  {
   _view
.width  _view.stage.stageWidth;
   
_view.height _view.stage.stageHeight;  
  
}
  
  
protected function onEnterFrame(event:Event):void
  {
   
var intersect:Vector3D _drag3D.getIntersect();
   var 
hoverX:uint Math.floor(intersect.GRID_SIZE);
   var 
hoverZ:uint Math.floor(intersect.GRID_SIZE);
   
   if (
hoverX || hoverX >= MESH_SIZE || hoverZ || hoverZ >= MESH_SIZE )
   
{
    _dragMesh
.visible false;
    
    
_txtLocation.text 
     
"GRID - X: --   Z: --   " +
     
"DRAG3D - X: " intersect.x.toFixed() + "   Z: " intersect.z.toFixed(); 
   
}
   
else
   
{
    _dragMesh
.visible true;
    
    
_dragMesh.= (hoverX GRID_SIZE) + (GRID_SIZE >> 1);
    
_dragMesh.= (hoverZ GRID_SIZE) + (GRID_SIZE >> 1);

    
_txtLocation.text 
     
"GRID - X: " hoverX "   Z: " hoverZ "   " +
     
"DRAG3D - X: " intersect.x.toFixed() + "   Z: " intersect.z.toFixed(); 
   
}

   _view
.render();
  
}
 }
   

John Brookes, Moderator
Posted: 08 April 2014 10:11 AM   Total Posts: 732   [ # 3 ]

Not fully tested with all of the Drag3d options but should work in your Ortho/Perspective example.

https://gist.github.com/JohnBrookes/4898c006c23a6aa71d0b

As before make sure you have also applied the fix for the ortho bug #683

   

OEGInc, Newbie
Posted: 08 April 2014 02:30 PM   Total Posts: 27   [ # 4 ]

Awesome!  Thanks John!  Seems to work fine so far.  I appreciate your work.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X