1 /** 2 * The Render Engine 3 * PulleyJointComponent 4 * 5 * @fileoverview A pulley 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.PulleyJoint", 37 "requires":[ 38 "R.components.physics.BaseJoint", 39 "R.physics.Simulation", 40 "R.math.Math2D" 41 ] 42 }); 43 44 /** 45 * @class A pulley joint which links two bodies together via a pulley in a {@link R.physics.Simulation}. 46 * 47 * @param name {String} Name of the component 48 * @param body1 {R.components.physics.BaseBody} The first body for the joint 49 * @param body2 {R.components.physics.BaseBody} The second body for the joint 50 * @param anchor1 {R.math.Point2D} A point, in world coordinates relative to the two 51 * bodies, to use as one of the joint's anchor points 52 * @param anchor2 {R.math.Point2D} A point, in world coordinates relative to the two 53 * bodies, to use as one of the joint's anchor points 54 * @param [ratio=1] {Number} The ratio between the two anchors. 55 * 56 * @extends R.components.physics.BaseJoint 57 * @constructor 58 * @description Creates a pulley joint between two physical bodies. 59 */ 60 R.components.physics.PulleyJoint = function () { 61 return R.components.physics.BaseJoint.extend(/** @scope R.components.physics.PulleyJoint.prototype */{ 62 63 anchor1:null, 64 anchor2:null, 65 ratio:0, 66 67 /** 68 * @private 69 */ 70 constructor:function (name, body1, body2, anchor1, anchor2, ratio) { 71 var jointDef = new Box2D.Dynamics.Joints.b2PulleyJointDef(); 72 73 this.anchor1 = R.math.Point2D.create(anchor1).div(R.physics.Simulation.WORLD_SIZE); 74 this.anchor2 = R.math.Point2D.create(anchor2).div(R.physics.Simulation.WORLD_SIZE); 75 this.ratio = ratio || 1; 76 77 this.base(name || "PulleyJoint", 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 var ofs = R.clone(pt).div(R.physics.Simulation.WORLD_SIZE); 86 this.anchor1.add(ofs); 87 this.anchor2.add(ofs); 88 ofs.destroy(); 89 }, 90 91 /** 92 * When simulation starts offset the anchor point by the position of rigid body 1 (the "from" body). 93 * @private 94 */ 95 startSimulation:function () { 96 if (!this.getSimulation()) { 97 var sim = this.getGameObject().getSimulation(); 98 99 var anchor1 = new Box2D.Common.Math.b2Vec2(), anchor2 = new Box2D.Common.Math.b2Vec2(); 100 anchor1.Set(this.anchor1.x, this.anchor1.y); 101 anchor2.Set(this.anchor2.x, this.anchor2.y); 102 103 this.getJointDef().Initialize(this.getBody1().getBody(), this.getBody2().getBody(), 104 sim.getGroundBody(), sim.getGroundBody(), 105 anchor1, anchor2, this.ratio); 106 } 107 108 this.base(); 109 } 110 111 }, { /** @scope R.components.physics.PulleyJoint.prototype */ 112 113 /** 114 * Get the class name of this object 115 * 116 * @return {String} "R.components.physics.PulleyJoint" 117 */ 118 getClassName:function () { 119 return "R.components.physics.PulleyJoint"; 120 } 121 }); 122 };