Home | History | Annotate | Download | only in shapes
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 package com.jme3.bullet.collision.shapes;
     33 
     34 import com.bulletphysics.collision.shapes.CapsuleShape;
     35 import com.bulletphysics.collision.shapes.CapsuleShapeX;
     36 import com.bulletphysics.collision.shapes.CapsuleShapeZ;
     37 import com.jme3.bullet.util.Converter;
     38 import com.jme3.export.InputCapsule;
     39 import com.jme3.export.JmeExporter;
     40 import com.jme3.export.JmeImporter;
     41 import com.jme3.export.OutputCapsule;
     42 import java.io.IOException;
     43 
     44 /**
     45  * Basic capsule collision shape
     46  * @author normenhansen
     47  */
     48 public class CapsuleCollisionShape extends CollisionShape{
     49     protected float radius,height;
     50     protected int axis;
     51 
     52     public CapsuleCollisionShape() {
     53     }
     54 
     55     /**
     56      * creates a new CapsuleCollisionShape with the given radius and height
     57      * @param radius the radius of the capsule
     58      * @param height the height of the capsule
     59      */
     60     public CapsuleCollisionShape(float radius, float height) {
     61         this.radius=radius;
     62         this.height=height;
     63         this.axis=1;
     64         CapsuleShape capShape=new CapsuleShape(radius,height);
     65         cShape=capShape;
     66     }
     67 
     68     /**
     69      * creates a capsule shape around the given axis (0=X,1=Y,2=Z)
     70      * @param radius
     71      * @param height
     72      * @param axis
     73      */
     74     public CapsuleCollisionShape(float radius, float height, int axis) {
     75         this.radius=radius;
     76         this.height=height;
     77         this.axis=axis;
     78         createShape();
     79     }
     80 
     81     public float getRadius() {
     82         return radius;
     83     }
     84 
     85     public float getHeight() {
     86         return height;
     87     }
     88 
     89     public int getAxis() {
     90         return axis;
     91     }
     92 
     93     public void write(JmeExporter ex) throws IOException {
     94         super.write(ex);
     95         OutputCapsule capsule = ex.getCapsule(this);
     96         capsule.write(radius, "radius", 0.5f);
     97         capsule.write(height, "height", 1);
     98         capsule.write(axis, "axis", 1);
     99     }
    100 
    101     public void read(JmeImporter im) throws IOException {
    102         super.read(im);
    103         InputCapsule capsule = im.getCapsule(this);
    104         radius = capsule.readFloat("radius", 0.5f);
    105         height = capsule.readFloat("height", 0.5f);
    106         axis = capsule.readInt("axis", 1);
    107         createShape();
    108     }
    109 
    110     protected void createShape(){
    111         switch(axis){
    112             case 0:
    113                 cShape=new CapsuleShapeX(radius,height);
    114             break;
    115             case 1:
    116                 cShape=new CapsuleShape(radius,height);
    117             break;
    118             case 2:
    119                 cShape=new CapsuleShapeZ(radius,height);
    120             break;
    121         }
    122         cShape.setLocalScaling(Converter.convert(getScale()));
    123         cShape.setMargin(margin);
    124     }
    125 
    126 }
    127