topical media & game development
graphic-o3d-samples-o3djs-arcball.js / js
/*
* Copyright 2009, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// A shout out to Terrance J. Grant at tatewake.com for his tutorial on arcball
// implementations.
@fileoverview This file contains functions for implementing an arcball
calculation. It puts them in the "arcball" module on the o3djs object.
Note: This library is only a sample. It is not meant to be some official
library. It is provided only as example code.
o3djs.provide('o3djs.arcball');
o3djs.require('o3djs.math');
o3djs.require('o3djs.quaternions');
A Module for arcball manipulation.
This is useful for rotating a model with the mouse.
@namespace
o3djs.arcball = o3djs.arcball || {};
Creates a new arcball.
parameter: {number} areaWidth width of area arcball should cover.
parameter: {number} areaHeight height of area arcball should cover.
returns: {!o3djs.arcball.ArcBall} The created arcball.
see: o3djs.arcball
o3djs.arcball.create = function(areaWidth, areaHeight) {
return new o3djs.arcball.ArcBall(areaWidth, areaHeight);
};
A class that implements an Arcball.
@constructor
parameter: {number} areaWidth width of area arcball should cover.
parameter: {number} areaHeight height of area arcball should cover.
see: o3djs.arcball
o3djs.arcball.ArcBall = function(areaWidth, areaHeight) {
this.startVector = [0, 0, 0];
this.endVector = [0, 0, 0];
this.areaWidth = areaWidth;
this.areaHeight = areaHeight;
};
Sets the size of the arcball.
parameter: {number} areaWidth width of area arcball should cover.
parameter: {number} areaHeight height of area arcball should cover.
o3djs.arcball.ArcBall.prototype.setAreaSize = function(areaWidth, areaHeight) {
this.areaWidth = areaWidth;
this.areaHeight = areaHeight;
};
Converts a 2d point to a point on a sphere.
parameter: {!o3djs.math.Vector2} newPoint point in 2d.
returns: {!o3djs.math.Vector3} point on sphere.
o3djs.arcball.ArcBall.prototype.mapToSphere = function(newPoint) {
// Copy parameter into temp
var tempPoint = o3djs.math.copy(newPoint);
// Scale to -1.0 <-> 1.0
tempPoint[0] = tempPoint[0] / this.areaWidth * 2.0 - 1.0;
tempPoint[1] = 1.0 - tempPoint[1] / this.areaHeight * 2.0;
// Compute square of length from center
var lengthSquared = o3djs.math.lengthSquared(tempPoint);
// If the point is mapped outside of the sphere... (length > radius squared)
if (lengthSquared > 1.0)
return o3djs.math.normalize(tempPoint).concat(0);
else // Otherwise it's on the inside.
return tempPoint.concat(Math.sqrt(1.0 - length));
};
Records the starting point on the sphere.
parameter: {!o3djs.math.Vector2} newPoint point in 2d.
o3djs.arcball.ArcBall.prototype.click = function(newPoint) {
this.startVector = this.mapToSphere(newPoint);
};
Computes the rotation of the sphere based on the initial point clicked as
set through Arcball.click and the current point passed in as newPoint
parameter: {!o3djs.math.Vector2} newPoint point in 2d.
returns: {!o3djs.quaternions.Quaternion} A quaternion representing the new
orientation.
o3djs.arcball.ArcBall.prototype.drag = function(newPoint) {
this.endVector = this.mapToSphere(newPoint);
return [o3djs.math.dot(this.startVector, this.endVector)].concat(
o3djs.math.cross(this.startVector, this.endVector));
};
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.