Home | History | Annotate | Download | only in joints
      1 /*******************************************************************************
      2  * Copyright (c) 2013, Daniel Murphy
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without modification,
      6  * are permitted provided that the following conditions are met:
      7  * 	* Redistributions of source code must retain the above copyright notice,
      8  * 	  this list of conditions and the following disclaimer.
      9  * 	* Redistributions in binary form must reproduce the above copyright notice,
     10  * 	  this list of conditions and the following disclaimer in the documentation
     11  * 	  and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     16  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     17  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     19  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     20  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     21  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     22  * POSSIBILITY OF SUCH DAMAGE.
     23  ******************************************************************************/
     24 /*
     25  * JBox2D - A Java Port of Erin Catto's Box2D
     26  *
     27  * JBox2D homepage: http://jbox2d.sourceforge.net/
     28  * Box2D homepage: http://www.box2d.org
     29  *
     30  * This software is provided 'as-is', without any express or implied
     31  * warranty.  In no event will the authors be held liable for any damages
     32  * arising from the use of this software.
     33  *
     34  * Permission is granted to anyone to use this software for any purpose,
     35  * including commercial applications, and to alter it and redistribute it
     36  * freely, subject to the following restrictions:
     37  *
     38  * 1. The origin of this software must not be misrepresented; you must not
     39  * claim that you wrote the original software. If you use this software
     40  * in a product, an acknowledgment in the product documentation would be
     41  * appreciated but is not required.
     42  * 2. Altered source versions must be plainly marked as such, and must not be
     43  * misrepresented as being the original software.
     44  * 3. This notice may not be removed or altered from any source distribution.
     45  */
     46 
     47 package org.jbox2d.dynamics.joints;
     48 
     49 import org.jbox2d.common.Vec2;
     50 import org.jbox2d.dynamics.Body;
     51 
     52 //Updated to rev 56->130->142 of b2DistanceJoint.cpp/.h
     53 
     54 /**
     55  * Distance joint definition. This requires defining an anchor point on both bodies and the non-zero
     56  * length of the distance joint. The definition uses local anchor points so that the initial
     57  * configuration can violate the constraint slightly. This helps when saving and loading a game.
     58  *
     59  * @warning Do not use a zero or short length.
     60  */
     61 public class DistanceJointDef extends JointDef {
     62   /** The local anchor point relative to body1's origin. */
     63   public final Vec2 localAnchorA;
     64 
     65   /** The local anchor point relative to body2's origin. */
     66   public final Vec2 localAnchorB;
     67 
     68   /** The equilibrium length between the anchor points. */
     69   public float length;
     70 
     71   /**
     72    * The mass-spring-damper frequency in Hertz.
     73    */
     74   public float frequencyHz;
     75 
     76   /**
     77    * The damping ratio. 0 = no damping, 1 = critical damping.
     78    */
     79   public float dampingRatio;
     80 
     81   public DistanceJointDef() {
     82     super(JointType.DISTANCE);
     83     localAnchorA = new Vec2(0.0f, 0.0f);
     84     localAnchorB = new Vec2(0.0f, 0.0f);
     85     length = 1.0f;
     86     frequencyHz = 0.0f;
     87     dampingRatio = 0.0f;
     88   }
     89 
     90   /**
     91    * Initialize the bodies, anchors, and length using the world anchors.
     92    *
     93    * @param b1 First body
     94    * @param b2 Second body
     95    * @param anchor1 World anchor on first body
     96    * @param anchor2 World anchor on second body
     97    */
     98   public void initialize(final Body b1, final Body b2, final Vec2 anchor1, final Vec2 anchor2) {
     99     bodyA = b1;
    100     bodyB = b2;
    101     localAnchorA.set(bodyA.getLocalPoint(anchor1));
    102     localAnchorB.set(bodyB.getLocalPoint(anchor2));
    103     Vec2 d = anchor2.sub(anchor1);
    104     length = d.length();
    105   }
    106 }
    107