Package net.phys2d.raw

Source Code of net.phys2d.raw.FixedJoint

/*
* Phys2D - a 2D physics engine based on the work of Erin Catto. The
* original source remains:
*
* Copyright (c) 2006 Erin Catto http://www.gphysics.com
*
* This source is provided under the terms of the BSD License.
*
* Copyright (c) 2006, Phys2D
* 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 the Phys2D/New Dawn Software 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.
*/
package net.phys2d.raw;

import net.phys2d.math.Vector2f;


/**
* A joint between two bodies. The joint affects the impulses applied to
* each body each step constraining the movement. This joint is anchored
* on the opposing bodies centre
*
* @author Kevin Glass
*/
public strictfp class FixedJoint implements Joint {
  /** The next ID to be used */
  public static int NEXT_ID = 0;
 
  /** The first body attached to the joint */
  private Body body1;
  /** The second body attached to the joint */
  private Body body2;

  /** The ID of this joint */
  private int id;
 
  /** The first joint fixing the position in one direction */
  private BasicJoint joint1;
  /** The second joint fixing the position in one direction */
  private BasicJoint joint2;
 
  /**
   * Create a joint holding two bodies together
   *
   * @param b1 The first body attached to the joint
   * @param b2 The second body attached to the joint
   */
  public FixedJoint(Body b1, Body b2) {
    id = NEXT_ID++;
   
    set(b1,b2);
  }

  /**
   * Set the relaxtion value on this joint. This value determines
   * how loose the joint will be
   *
   * @param relaxation The relaxation value
   */
  public void setRelaxation(float relaxation) {
    joint1.setRelaxation(relaxation);
    joint2.setRelaxation(relaxation);
  }
 
  /**
   * Get the first body attached to this joint
   *
   * @return The first body attached to this joint
   */
  public Body getBody1() {
    return body1;
  }

  /**
   * Get the second body attached to this joint
   *
   * @return The second body attached to this joint
   */
  public Body getBody2() {
    return body2;
  }
 
  /**
   * Reconfigure this joint
   *
   * @param b1 The first body attached to this joint
   * @param b2 The second body attached to this joint
   */
  public void set(Body b1, Body b2) {
    body1 = b1;
    body2 = b2;

    joint1 = new BasicJoint(b1,b2,new Vector2f(b1.getPosition()));
    joint2 = new BasicJoint(b2,b1,new Vector2f(b2.getPosition()));
  }

  /**
   * Precaculate everything and apply initial impulse before the
   * simulation step takes place
   *
   * @param invDT The amount of time the simulation is being stepped by
   */
  public void preStep(float invDT) {
    joint1.preStep(invDT);
    joint2.preStep(invDT);
  }
 
  /**
   * Apply the impulse caused by the joint to the bodies attached.
   */
  public void applyImpulse() {
    joint1.applyImpulse();
    joint2.applyImpulse();
  }
 
  /**
   * @see java.lang.Object#hashCode()
   */
  public int hashCode() {
    return id;
  }
 
  /**
   * @see java.lang.Object#equals(java.lang.Object)
   */
  public boolean equals(Object other) {
    if (other.getClass() == getClass()) {
      return ((FixedJoint) other).id == id;
    }
   
    return false;
  }
}
TOP

Related Classes of net.phys2d.raw.FixedJoint

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.