aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhwc4872012-04-24 17:16:29 -0700
committerhwc4872012-04-24 17:16:29 -0700
commitcec076988d3ff6547b7c9d74ebc80530ffcea67b (patch)
tree389a60f3d5605c082c49c4c9829cb8a560cbc26a
parent9d274a93cecc7cba543d27237e65425b2d6a393c (diff)
downloadninja-cec076988d3ff6547b7c9d74ebc80530ffcea67b.tar.gz
Snapping tocontained objects
-rwxr-xr-xjs/lib/geom/circle.js53
-rwxr-xr-xjs/lib/geom/geom-obj.js59
-rwxr-xr-xjs/lib/geom/rectangle.js1
3 files changed, 109 insertions, 4 deletions
diff --git a/js/lib/geom/circle.js b/js/lib/geom/circle.js
index 896803bf..b7027eec 100755
--- a/js/lib/geom/circle.js
+++ b/js/lib/geom/circle.js
@@ -4,9 +4,12 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. 4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */ 5</copyright> */
6 6
7var GeomObj = require("js/lib/geom/geom-obj").GeomObj; 7var GeomObj = require("js/lib/geom/geom-obj").GeomObj;
8var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive; 8var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive;
9var MaterialsModel = require("js/models/materials-model").MaterialsModel; 9var MaterialsModel = require("js/models/materials-model").MaterialsModel;
10var drawUtils = require("js/helper-classes/3D/draw-utils").DrawUtils;
11var vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils;
12
10/////////////////////////////////////////////////////////////////////// 13///////////////////////////////////////////////////////////////////////
11// Class GLCircle 14// Class GLCircle
12// GL representation of a circle. 15// GL representation of a circle.
@@ -710,6 +713,49 @@ var Circle = function GLCircle() {
710 return (MathUtils.fpCmp(distToPt,distToBoundary) <= 0); 713 return (MathUtils.fpCmp(distToPt,distToBoundary) <= 0);
711 }; 714 };
712 715
716 this.getNearPoint = function( pt, dir )
717 {
718 var world = this.getWorld();
719 if (!world) throw( "null world in getNearPoint" );
720
721 // the input point and direction are in GL space
722 // project to the z == 0 plane
723 var mat = this.getMatrix();
724 var plane = [0,0,1,0];
725 plane = MathUtils.transformPlane( plane, mat );
726 var projPt = MathUtils.vecIntersectPlane ( pt, dir, plane );
727
728 // get the center of the circle in GL space
729 var ctr = this.getGLCenter();
730
731 // transform the projected point to the plane of the circle
732 var planePt = MathUtils.transformPoint( projPt, mat );
733
734 // get a matrix mapping the circle to a 2D coordinate system
735 var normal = [ mat[8], mat[9], mat[10] ];
736 var planeMat = drawUtils.getPlaneToWorldMatrix(normal, ctr);
737 var planeMatInv = glmat4.inverse( planeMat, [] );
738 var planePt2D = MathUtils.transformPoint( planePt, planeMatInv );
739
740 // get 2 points on the axes of the oval
741 var wPt = this.preViewToGL( [this._xOffset + 0.5*this.getWidth(), this._yOffset, 0] ),
742 hPt = this.preViewToGL( [this._xOffset, this._yOffset + 0.5*this.getHeight(), 0] );
743 var w = vecUtils.vecDist( 2, wPt, ctr ),
744 h = vecUtils.vecDist( 2, hPt, ctr );
745 var aspect = w/h;
746
747 // get the angle of the projected point relative to the circle
748 var angle = Math.atan2( planePt2D[1], planePt2D[0]/aspect );
749 var degrees = angle*180.0/Math.PI;
750
751 // get the corresponding point on the object
752 var pt = [ Math.cos(angle)*w, Math.sin(angle)*h, 0 ];
753 var glPt = MathUtils.transformPoint( pt, planeMat );
754
755 return glPt;
756 }
757
758 /*
713 this.getNearPoint = function( pt, dir ) { 759 this.getNearPoint = function( pt, dir ) {
714 var world = this.getWorld(); 760 var world = this.getWorld();
715 if (!world) throw( "null world in getNearPoint" ); 761 if (!world) throw( "null world in getNearPoint" );
@@ -759,6 +805,7 @@ var Circle = function GLCircle() {
759 805
760 return objPt; 806 return objPt;
761 }; 807 };
808 */
762 809
763 this.recalcTexMapCoords = function( vrts, uvs ) { 810 this.recalcTexMapCoords = function( vrts, uvs ) {
764 var n = vrts.length/3; 811 var n = vrts.length/3;
diff --git a/js/lib/geom/geom-obj.js b/js/lib/geom/geom-obj.js
index f2991bdb..7cb9b80f 100755
--- a/js/lib/geom/geom-obj.js
+++ b/js/lib/geom/geom-obj.js
@@ -375,6 +375,65 @@ var GeomObj = function GLGeomObj() {
375 } 375 }
376 }; 376 };
377 377
378
379 this.getGLCenter = function()
380 {
381 // get the normalized device coordinates (NDC) for
382 // all position and dimensions.
383 var world = this.getWorld();
384 var vpw = world.getViewportWidth(), vph = world.getViewportHeight();
385 var xNDC = 2*this._xOffset/vpw, yNDC = -2*this._yOffset/vph;
386
387 var aspect = world.getAspect();
388 var zn = world.getZNear(), zf = world.getZFar();
389 var t = zn * Math.tan(world.getFOV() * Math.PI / 360.0),
390 b = -t,
391 r = aspect*t,
392 l = -r;
393
394 // calculate the object coordinates from their NDC coordinates
395 var z = -world.getViewDistance();
396
397 // unproject to get the position of the origin in GL
398 var x = -z*(r-l)/(2.0*zn)*xNDC,
399 y = -z*(t-b)/(2.0*zn)*yNDC;
400 z = 0.0;
401
402 // transform by the object's transformation matrix
403 var ctr = MathUtils.transformPoint( [x, y, z], this.getMatrix() );
404
405 return ctr;
406 };
407
408 this.preViewToGL = function( preViewPt )
409 {
410 // get the normalized device coordinates (NDC) for
411 // all position and dimensions.
412 var world = this.getWorld();
413 var vpw = world.getViewportWidth(), vph = world.getViewportHeight();
414 var xNDC = 2*preViewPt[0]/vpw, yNDC = -2*preViewPt[1]/vph;
415
416 var aspect = world.getAspect();
417 var zn = world.getZNear(), zf = world.getZFar();
418 var t = zn * Math.tan(world.getFOV() * Math.PI / 360.0),
419 b = -t,
420 r = aspect*t,
421 l = -r;
422
423 // calculate the object coordinates from their NDC coordinates
424 var z = -world.getViewDistance();
425
426 // unproject to get the position of the origin in GL
427 var x = -z*(r-l)/(2.0*zn)*xNDC,
428 y = -z*(t-b)/(2.0*zn)*yNDC;
429 z = 0.0;
430
431 // transform by the object's transformation matrix
432 var glPt = MathUtils.transformPoint( [x, y, z], this.getMatrix() );
433
434 return glPt;
435 };
436
378 this.buildBuffers = function () { 437 this.buildBuffers = function () {
379 // this function must be overridden by the base class 438 // this function must be overridden by the base class
380 alert("GLGeomObj.buildBuffers must be overridden by base class"); 439 alert("GLGeomObj.buildBuffers must be overridden by base class");
diff --git a/js/lib/geom/rectangle.js b/js/lib/geom/rectangle.js
index d9f1f0b1..41c51e2f 100755
--- a/js/lib/geom/rectangle.js
+++ b/js/lib/geom/rectangle.js
@@ -693,7 +693,6 @@ var Rectangle = function GLRectangle() {
693 var projPt = MathUtils.vecIntersectPlane ( pt, dir, plane ); 693 var projPt = MathUtils.vecIntersectPlane ( pt, dir, plane );
694 694
695 // transform the projected point back to the XY plane 695 // transform the projected point back to the XY plane
696 //var invMat = mat.inverse();
697 var invMat = glmat4.inverse(mat, []); 696 var invMat = glmat4.inverse(mat, []);
698 var planePt = MathUtils.transformPoint( projPt, invMat ); 697 var planePt = MathUtils.transformPoint( projPt, invMat );
699 698