Home | History | Annotate | Download | only in collision
      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;
     33 
     34 import com.bulletphysics.collision.narrowphase.ManifoldPoint;
     35 import com.jme3.bullet.util.Converter;
     36 import com.jme3.math.Vector3f;
     37 import com.jme3.scene.Spatial;
     38 import java.util.EventObject;
     39 
     40 /**
     41  * A CollisionEvent stores all information about a collision in the PhysicsWorld.
     42  * Do not store this Object, as it will be reused after the collision() method has been called.
     43  * Get/reference all data you need in the collide method.
     44  * @author normenhansen
     45  */
     46 public class PhysicsCollisionEvent extends EventObject {
     47 
     48     public static final int TYPE_ADDED = 0;
     49     public static final int TYPE_PROCESSED = 1;
     50     public static final int TYPE_DESTROYED = 2;
     51     private int type;
     52     private PhysicsCollisionObject nodeA;
     53     private PhysicsCollisionObject nodeB;
     54     private ManifoldPoint cp;
     55 
     56     public PhysicsCollisionEvent(int type, PhysicsCollisionObject source, PhysicsCollisionObject nodeB, ManifoldPoint cp) {
     57         super(source);
     58         this.type = type;
     59         this.nodeA = source;
     60         this.nodeB = nodeB;
     61         this.cp = cp;
     62     }
     63 
     64     /**
     65      * used by event factory, called when event is destroyed
     66      */
     67     public void clean() {
     68         source = null;
     69         type = 0;
     70         nodeA = null;
     71         nodeB = null;
     72         cp = null;
     73     }
     74 
     75     /**
     76      * used by event factory, called when event reused
     77      */
     78     public void refactor(int type, PhysicsCollisionObject source, PhysicsCollisionObject nodeB, ManifoldPoint cp) {
     79         this.source = source;
     80         this.type = type;
     81         this.nodeA = source;
     82         this.nodeB = nodeB;
     83         this.cp = cp;
     84     }
     85 
     86     public int getType() {
     87         return type;
     88     }
     89 
     90     /**
     91      * @return A Spatial if the UserObject of the PhysicsCollisionObject is a Spatial
     92      */
     93     public Spatial getNodeA() {
     94         if (nodeA.getUserObject() instanceof Spatial) {
     95             return (Spatial) nodeA.getUserObject();
     96         }
     97         return null;
     98     }
     99 
    100     /**
    101      * @return A Spatial if the UserObject of the PhysicsCollisionObject is a Spatial
    102      */
    103     public Spatial getNodeB() {
    104         if (nodeB.getUserObject() instanceof Spatial) {
    105             return (Spatial) nodeB.getUserObject();
    106         }
    107         return null;
    108     }
    109 
    110     public PhysicsCollisionObject getObjectA() {
    111         return nodeA;
    112     }
    113 
    114     public PhysicsCollisionObject getObjectB() {
    115         return nodeB;
    116     }
    117 
    118     public float getAppliedImpulse() {
    119         return cp.appliedImpulse;
    120     }
    121 
    122     public float getAppliedImpulseLateral1() {
    123         return cp.appliedImpulseLateral1;
    124     }
    125 
    126     public float getAppliedImpulseLateral2() {
    127         return cp.appliedImpulseLateral2;
    128     }
    129 
    130     public float getCombinedFriction() {
    131         return cp.combinedFriction;
    132     }
    133 
    134     public float getCombinedRestitution() {
    135         return cp.combinedRestitution;
    136     }
    137 
    138     public float getDistance1() {
    139         return cp.distance1;
    140     }
    141 
    142     public int getIndex0() {
    143         return cp.index0;
    144     }
    145 
    146     public int getIndex1() {
    147         return cp.index1;
    148     }
    149 
    150     public Vector3f getLateralFrictionDir1() {
    151         return Converter.convert(cp.lateralFrictionDir1);
    152     }
    153 
    154     public Vector3f getLateralFrictionDir2() {
    155         return Converter.convert(cp.lateralFrictionDir2);
    156     }
    157 
    158     public boolean isLateralFrictionInitialized() {
    159         return cp.lateralFrictionInitialized;
    160     }
    161 
    162     public int getLifeTime() {
    163         return cp.lifeTime;
    164     }
    165 
    166     public Vector3f getLocalPointA() {
    167         return Converter.convert(cp.localPointA);
    168     }
    169 
    170     public Vector3f getLocalPointB() {
    171         return Converter.convert(cp.localPointB);
    172     }
    173 
    174     public Vector3f getNormalWorldOnB() {
    175         return Converter.convert(cp.normalWorldOnB);
    176     }
    177 
    178     public int getPartId0() {
    179         return cp.partId0;
    180     }
    181 
    182     public int getPartId1() {
    183         return cp.partId1;
    184     }
    185 
    186     public Vector3f getPositionWorldOnA() {
    187         return Converter.convert(cp.positionWorldOnA);
    188     }
    189 
    190     public Vector3f getPositionWorldOnB() {
    191         return Converter.convert(cp.positionWorldOnB);
    192     }
    193 
    194     public Object getUserPersistentData() {
    195         return cp.userPersistentData;
    196     }
    197 }
    198