1 /**
  2  * The Render Engine
  3  * RayInfo
  4  *
  5  * @fileoverview Data object which holds ray cast relevant information.
  6  *
  7  * @author: Brett Fattori (brettf@renderengine.com)
  8  *
  9  * @author: $Author: bfattori $
 10  * @version: $Revision: 1555 $
 11  *
 12  * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com)
 13  *
 14  * Permission is hereby granted, free of charge, to any person obtaining a copy
 15  * of this software and associated documentation files (the "Software"), to deal
 16  * in the Software without restriction, including without limitation the rights
 17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 18  * copies of the Software, and to permit persons to whom the Software is
 19  * furnished to do so, subject to the following conditions:
 20  *
 21  * The above copyright notice and this permission notice shall be included in
 22  * all copies or substantial portions of the Software.
 23  *
 24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 30  * THE SOFTWARE.
 31  *
 32  */
 33 
 34 // The class this file defines and its required classes
 35 R.Engine.define({
 36     "class":"R.struct.RayInfo",
 37     "requires":[
 38         "R.engine.PooledObject"
 39     ]
 40 });
 41 
 42 /**
 43  * @class An object which contains information about a ray.  The values of the
 44  *    ray structure are read directly.
 45  *
 46  * @param start {R.math.Point2D} The start point of the ray
 47  * @param dir {R.math.Vector2D} The direction vector
 48  *
 49  * @extends R.engine.PooledObject
 50  * @constructor
 51  * @description Creates a ray info data structure for ray casting.
 52  */
 53 R.struct.RayInfo = function () {
 54     return R.engine.PooledObject.extend(/** @scope R.struct.RayInfo.prototype */{
 55 
 56         /**
 57          * The overlap in pixels
 58          * @type {Number}
 59          */
 60         overlap:0,
 61 
 62         /**
 63          * The collision normal
 64          * @type {R.math.Vector2D}
 65          */
 66         normal:null,
 67 
 68         /**
 69          * The object that was collided with
 70          * @type {R.engine.GameObject}
 71          */
 72         shape:null,
 73 
 74         /**
 75          * The point along the ray at which the collision occurred
 76          * @type {R.math.Point2D}
 77          */
 78         impactPoint:null,
 79 
 80         /**
 81          * The starting point of the ray
 82          * @type {R.math.Point2D}
 83          */
 84         startPoint:null,
 85 
 86         /**
 87          * The direction and magnitude of the ray
 88          * @type {R.math.Vector2D}
 89          */
 90         direction:null,
 91 
 92         /**
 93          * The world time at the time of the collision
 94          * @type {Number}
 95          */
 96         worldTime:0,
 97 
 98         /**
 99          * The time delta between the world time and the last time the engine was updated
100          * @type {Number}
101          */
102         delta:0,
103 
104         /**
105          * A data object which can contain additional information about the ray
106          * @type {Object}
107          */
108         data:null,
109 
110         /** @private */
111         constructor:function (start, dir) {
112             this.startPoint = R.clone(start);
113             this.direction = R.clone(dir);
114             this.normal = R.clone(dir).normalize().neg();
115             this.shape = null;
116             this.impactPoint = R.math.Point2D.create(0, 0);
117             this.worldTime = 0;
118             this.delta = 0;
119             this.data = {};
120             this.base("RayInfo");
121         },
122 
123         /**
124          * Destroy the collision data object.
125          */
126         destroy:function () {
127             if (this.impactPoint) {
128                 this.impactPoint.destroy();
129             }
130             if (this.normal) {
131                 this.normal.destroy();
132             }
133             if (this.data && this.data.destroy) {
134                 this.data.destroy();
135             }
136             this.startPoint.destroy();
137             this.direction.destroy();
138             this.base();
139         },
140 
141         /**
142          * Release the collision data object back into the pool for reuse.
143          */
144         release:function () {
145             this.base();
146             this.overlap = 0;
147             this.normal = null;
148             this.shape = null;
149             this.impactPoint = null;
150             this.startPoint = null;
151             this.direction = null;
152             this.worldTime = 0;
153             this.delta = 0;
154         },
155 
156         /**
157          * Set the point of impact along the ray.
158          * @param impact {R.math.Point2D} The impact point
159          * @param shape {R.engine.PooledObject} The object that was impacted
160          * @param [data] {Object} Optional data object
161          */
162         set:function (impact, shape, data) {
163             this.worldTime = R.Engine.worldTime;
164             this.delta = R.Engine.lastTime;
165             this.impactPoint.set(impact);
166             this.shape = shape;
167             var end = R.math.Vector2D.create(this.startPoint).add(this.direction);
168             this.overlap = end.sub(impact).len();
169             end.destroy();
170             this.data = data;
171         }
172 
173     }, /** @scope R.struct.RayInfo.prototype */{
174         getClassName:function () {
175             return "R.struct.RayInfo";
176         }
177     });
178 };