Home | History | Annotate | Download | only in shapes
      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 package org.jbox2d.collision.shapes;
     25 
     26 import org.jbox2d.collision.AABB;
     27 import org.jbox2d.collision.RayCastInput;
     28 import org.jbox2d.collision.RayCastOutput;
     29 import org.jbox2d.common.Transform;
     30 import org.jbox2d.common.Vec2;
     31 
     32 /**
     33  * A shape is used for collision detection. You can create a shape however you like. Shapes used for
     34  * simulation in World are created automatically when a Fixture is created. Shapes may encapsulate a
     35  * one or more child shapes.
     36  */
     37 public abstract class Shape {
     38 
     39   public final ShapeType m_type;
     40   public float m_radius;
     41 
     42   public Shape(ShapeType type) {
     43     this.m_type = type;
     44   }
     45 
     46   /**
     47    * Get the type of this shape. You can use this to down cast to the concrete shape.
     48    *
     49    * @return the shape type.
     50    */
     51   public ShapeType getType() {
     52     return m_type;
     53   }
     54 
     55   /**
     56    * The radius of the underlying shape. This can refer to different things depending on the shape
     57    * implementation
     58    *
     59    * @return
     60    */
     61   public float getRadius() {
     62     return m_radius;
     63   }
     64 
     65   /**
     66    * Sets the radius of the underlying shape. This can refer to different things depending on the
     67    * implementation
     68    *
     69    * @param radius
     70    */
     71   public void setRadius(float radius) {
     72     this.m_radius = radius;
     73   }
     74 
     75   /**
     76    * Get the number of child primitives
     77    *
     78    * @return
     79    */
     80   public abstract int getChildCount();
     81 
     82   /**
     83    * Test a point for containment in this shape. This only works for convex shapes.
     84    *
     85    * @param xf the shape world transform.
     86    * @param p a point in world coordinates.
     87    */
     88   public abstract boolean testPoint(final Transform xf, final Vec2 p);
     89 
     90   /**
     91    * Cast a ray against a child shape.
     92    *
     93    * @param argOutput the ray-cast results.
     94    * @param argInput the ray-cast input parameters.
     95    * @param argTransform the transform to be applied to the shape.
     96    * @param argChildIndex the child shape index
     97    * @return if hit
     98    */
     99   public abstract boolean raycast(RayCastOutput output, RayCastInput input, Transform transform,
    100       int childIndex);
    101 
    102 
    103   /**
    104    * Given a transform, compute the associated axis aligned bounding box for a child shape.
    105    *
    106    * @param argAabb returns the axis aligned box.
    107    * @param argXf the world transform of the shape.
    108    */
    109   public abstract void computeAABB(final AABB aabb, final Transform xf, int childIndex);
    110 
    111   /**
    112    * Compute the mass properties of this shape using its dimensions and density. The inertia tensor
    113    * is computed about the local origin.
    114    *
    115    * @param massData returns the mass data for this shape.
    116    * @param density the density in kilograms per meter squared.
    117    */
    118   public abstract void computeMass(final MassData massData, final float density);
    119 
    120   /**
    121    * Compute the distance from the current shape to the specified point. This only works for convex
    122    * shapes.
    123    *
    124    * @param xf the shape world transform.
    125    * @param p a point in world coordinates.
    126    * @param normalOut returns the direction in which the distance increases.
    127    * @return distance returns the distance from the current shape.
    128    */
    129   public abstract float computeDistanceToOut(Transform xf, Vec2 p, int childIndex, Vec2 normalOut);
    130 
    131   public abstract Shape clone();
    132 }
    133