1 /** 2 * The Render Engine 3 * WeldJointComponent 4 * 5 * @fileoverview A weld joint which can be used in a {@link Simulation}. 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.components.physics.WeldJoint", 37 "requires":[ 38 "R.components.physics.BaseJoint", 39 "R.physics.Simulation", 40 "R.math.Point2D", 41 "R.math.Vector2D", 42 "R.math.Rectangle2D", 43 "R.math.Math2D" 44 ] 45 }); 46 47 /** 48 * @class A weld joint effectively welds two bodies together at a given point 49 * in a {@link R.physics.Simulation}. 50 * 51 * @param name {String} Name of the component 52 * @param body1 {R.components.physics.BaseBody} The first body for the joint 53 * @param body2 {R.components.physics.BaseBody} The second body for the joint 54 * @param [anchor] {R.math.Point2D} The anchor point on body1, or <code>null</code> 55 * to use the body's position 56 * 57 * @extends R.components.physics.BaseJoint 58 * @constructor 59 * @description Creates a weld joint between two physical bodies. The location of the weld 60 * is described by the anchor position. When the first or second body is acted 61 * upon, the other body is also affected. 62 */ 63 R.components.physics.WeldJoint = function () { 64 return R.components.physics.BaseJoint.extend(/** @scope R.components.physics.WeldJoint.prototype */{ 65 66 anchor:null, 67 68 /** 69 * @private 70 */ 71 constructor:function (name, body1, body2, anchor) { 72 var jointDef = new Box2D.Dynamics.Joints.b2WeldJointDef(); 73 if (anchor) { 74 this.anchor = R.math.Point2D.create(anchor).div(R.physics.Simulation.WORLD_SIZE); 75 } 76 77 this.base(name || "WeldJoint", body1, body2, jointDef); 78 }, 79 80 /** 81 * Offset the joint's anchors by the given point 82 * @param pt {R.math.Point2D} The offset amount 83 */ 84 offset:function (pt) { 85 if (this.anchor) { 86 var ofs = R.clone(pt).div(R.physics.Simulation.WORLD_SIZE); 87 this.anchor.add(ofs); 88 ofs.destroy(); 89 } 90 }, 91 92 /** 93 * When simulation starts set the anchor point and bodies. 94 * @private 95 */ 96 startSimulation:function () { 97 if (!this.getSimulation()) { 98 var anchor = new Box2D.Common.Math.b2Vec2(); 99 if (this.anchor) { 100 anchor.Set(this.anchor.x, this.anchor.y); 101 } else { 102 var pos = R.clone(this.getBody1().getPosition()).div(R.physics.Simulation.WORLD_SIZE); 103 anchor.Set(pos.x, pos.y); 104 pos.destroy(); 105 } 106 this.getJointDef().Initialize(this.getBody1().getBody(), this.getBody2().getBody(), anchor); 107 } 108 109 this.base(); 110 } 111 112 }, { /** @scope R.components.physics.WeldJoint.prototype */ 113 114 /** 115 * Get the class name of this object 116 * 117 * @return {String} "R.components.physics.WeldJoint" 118 */ 119 getClassName:function () { 120 return "R.components.physics.WeldJoint"; 121 } 122 }); 123 };