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.jme3.math.Vector3f;
35 import com.jme3.scene.Spatial;
36 import java.util.EventObject;
37 
38 /**
39  * A CollisionEvent stores all information about a collision in the PhysicsWorld.
40  * Do not store this Object, as it will be reused after the collision() method has been called.
41  * Get/reference all data you need in the collide method.
42  * @author normenhansen
43  */
44 public class PhysicsCollisionEvent extends EventObject {
45 
46     public static final int TYPE_ADDED = 0;
47     public static final int TYPE_PROCESSED = 1;
48     public static final int TYPE_DESTROYED = 2;
49     private int type;
50     private PhysicsCollisionObject nodeA;
51     private PhysicsCollisionObject nodeB;
52     private long manifoldPointObjectId = 0;
53 
PhysicsCollisionEvent(int type, PhysicsCollisionObject nodeA, PhysicsCollisionObject nodeB, long manifoldPointObjectId)54     public PhysicsCollisionEvent(int type, PhysicsCollisionObject nodeA, PhysicsCollisionObject nodeB, long manifoldPointObjectId) {
55         super(nodeA);
56         this.manifoldPointObjectId = manifoldPointObjectId;
57     }
58 
59     /**
60      * used by event factory, called when event is destroyed
61      */
clean()62     public void clean() {
63         source = null;
64         this.type = 0;
65         this.nodeA = null;
66         this.nodeB = null;
67         this.manifoldPointObjectId = 0;
68     }
69 
70     /**
71      * used by event factory, called when event reused
72      */
refactor(int type, PhysicsCollisionObject source, PhysicsCollisionObject nodeB, long manifoldPointObjectId)73     public void refactor(int type, PhysicsCollisionObject source, PhysicsCollisionObject nodeB, long manifoldPointObjectId) {
74         this.source = source;
75         this.type = type;
76         this.nodeA = source;
77         this.nodeB = nodeB;
78         this.manifoldPointObjectId = manifoldPointObjectId;
79     }
80 
getType()81     public int getType() {
82         return type;
83     }
84 
85     /**
86      * @return A Spatial if the UserObject of the PhysicsCollisionObject is a Spatial
87      */
getNodeA()88     public Spatial getNodeA() {
89         if (nodeA.getUserObject() instanceof Spatial) {
90             return (Spatial) nodeA.getUserObject();
91         }
92         return null;
93     }
94 
95     /**
96      * @return A Spatial if the UserObject of the PhysicsCollisionObject is a Spatial
97      */
getNodeB()98     public Spatial getNodeB() {
99         if (nodeB.getUserObject() instanceof Spatial) {
100             return (Spatial) nodeB.getUserObject();
101         }
102         return null;
103     }
104 
getObjectA()105     public PhysicsCollisionObject getObjectA() {
106         return nodeA;
107     }
108 
getObjectB()109     public PhysicsCollisionObject getObjectB() {
110         return nodeB;
111     }
112 
getAppliedImpulse()113     public float getAppliedImpulse() {
114         return getAppliedImpulse(manifoldPointObjectId);
115     }
getAppliedImpulse(long manifoldPointObjectId)116     private native float getAppliedImpulse(long manifoldPointObjectId);
117 
getAppliedImpulseLateral1()118     public float getAppliedImpulseLateral1() {
119         return getAppliedImpulseLateral1(manifoldPointObjectId);
120     }
getAppliedImpulseLateral1(long manifoldPointObjectId)121     private native float getAppliedImpulseLateral1(long manifoldPointObjectId);
122 
getAppliedImpulseLateral2()123     public float getAppliedImpulseLateral2() {
124         return getAppliedImpulseLateral2(manifoldPointObjectId);
125     }
getAppliedImpulseLateral2(long manifoldPointObjectId)126     private native float getAppliedImpulseLateral2(long manifoldPointObjectId);
127 
getCombinedFriction()128     public float getCombinedFriction() {
129         return getCombinedFriction(manifoldPointObjectId);
130     }
getCombinedFriction(long manifoldPointObjectId)131     private native float getCombinedFriction(long manifoldPointObjectId);
132 
getCombinedRestitution()133     public float getCombinedRestitution() {
134         return getCombinedRestitution(manifoldPointObjectId);
135     }
getCombinedRestitution(long manifoldPointObjectId)136     private native float getCombinedRestitution(long manifoldPointObjectId);
137 
getDistance1()138     public float getDistance1() {
139         return getDistance1(manifoldPointObjectId);
140     }
getDistance1(long manifoldPointObjectId)141     private native float getDistance1(long manifoldPointObjectId);
142 
getIndex0()143     public int getIndex0() {
144         return getIndex0(manifoldPointObjectId);
145     }
getIndex0(long manifoldPointObjectId)146     private native int getIndex0(long manifoldPointObjectId);
147 
getIndex1()148     public int getIndex1() {
149         return getIndex1(manifoldPointObjectId);
150     }
getIndex1(long manifoldPointObjectId)151     private native int getIndex1(long manifoldPointObjectId);
152 
getLateralFrictionDir1()153     public Vector3f getLateralFrictionDir1() {
154         return getLateralFrictionDir1(new Vector3f());
155     }
156 
getLateralFrictionDir1(Vector3f lateralFrictionDir1)157     public Vector3f getLateralFrictionDir1(Vector3f lateralFrictionDir1) {
158         getLateralFrictionDir1(manifoldPointObjectId, lateralFrictionDir1);
159         return lateralFrictionDir1;
160     }
getLateralFrictionDir1(long manifoldPointObjectId, Vector3f lateralFrictionDir1)161     private native void getLateralFrictionDir1(long manifoldPointObjectId, Vector3f lateralFrictionDir1);
162 
getLateralFrictionDir2()163     public Vector3f getLateralFrictionDir2() {
164         return getLateralFrictionDir2(new Vector3f());
165     }
166 
getLateralFrictionDir2(Vector3f lateralFrictionDir2)167     public Vector3f getLateralFrictionDir2(Vector3f lateralFrictionDir2) {
168         getLateralFrictionDir2(manifoldPointObjectId, lateralFrictionDir2);
169         return lateralFrictionDir2;
170     }
getLateralFrictionDir2(long manifoldPointObjectId, Vector3f lateralFrictionDir2)171     private native void getLateralFrictionDir2(long manifoldPointObjectId, Vector3f lateralFrictionDir2);
172 
isLateralFrictionInitialized()173     public boolean isLateralFrictionInitialized() {
174         return isLateralFrictionInitialized(manifoldPointObjectId);
175     }
isLateralFrictionInitialized(long manifoldPointObjectId)176     private native boolean isLateralFrictionInitialized(long manifoldPointObjectId);
177 
getLifeTime()178     public int getLifeTime() {
179         return getLifeTime(manifoldPointObjectId);
180     }
getLifeTime(long manifoldPointObjectId)181     private native int getLifeTime(long manifoldPointObjectId);
182 
getLocalPointA()183     public Vector3f getLocalPointA() {
184         return getLocalPointA(new Vector3f());
185     }
186 
getLocalPointA(Vector3f localPointA)187     public Vector3f getLocalPointA(Vector3f localPointA) {
188         getLocalPointA(manifoldPointObjectId, localPointA);
189         return localPointA;
190     }
getLocalPointA(long manifoldPointObjectId, Vector3f localPointA)191     private native void getLocalPointA(long manifoldPointObjectId, Vector3f localPointA);
192 
getLocalPointB()193     public Vector3f getLocalPointB() {
194         return getLocalPointB(new Vector3f());
195     }
196 
getLocalPointB(Vector3f localPointB)197     public Vector3f getLocalPointB(Vector3f localPointB) {
198         getLocalPointB(manifoldPointObjectId, localPointB);
199         return localPointB;
200     }
getLocalPointB(long manifoldPointObjectId, Vector3f localPointB)201     private native void getLocalPointB(long manifoldPointObjectId, Vector3f localPointB);
202 
getNormalWorldOnB()203     public Vector3f getNormalWorldOnB() {
204         return getNormalWorldOnB(new Vector3f());
205     }
206 
getNormalWorldOnB(Vector3f normalWorldOnB)207     public Vector3f getNormalWorldOnB(Vector3f normalWorldOnB) {
208         getNormalWorldOnB(manifoldPointObjectId, normalWorldOnB);
209         return normalWorldOnB;
210     }
getNormalWorldOnB(long manifoldPointObjectId, Vector3f normalWorldOnB)211     private native void getNormalWorldOnB(long manifoldPointObjectId, Vector3f normalWorldOnB);
212 
getPartId0()213     public int getPartId0() {
214         return getPartId0(manifoldPointObjectId);
215     }
getPartId0(long manifoldPointObjectId)216     private native int getPartId0(long manifoldPointObjectId);
217 
getPartId1()218     public int getPartId1() {
219         return getPartId1(manifoldPointObjectId);
220     }
221 
getPartId1(long manifoldPointObjectId)222     private native int getPartId1(long manifoldPointObjectId);
223 
getPositionWorldOnA()224     public Vector3f getPositionWorldOnA() {
225         return getPositionWorldOnA(new Vector3f());
226     }
227 
getPositionWorldOnA(Vector3f positionWorldOnA)228     public Vector3f getPositionWorldOnA(Vector3f positionWorldOnA) {
229         getPositionWorldOnA(positionWorldOnA);
230         return positionWorldOnA;
231     }
getPositionWorldOnA(long manifoldPointObjectId, Vector3f positionWorldOnA)232     private native void getPositionWorldOnA(long manifoldPointObjectId, Vector3f positionWorldOnA);
233 
getPositionWorldOnB()234     public Vector3f getPositionWorldOnB() {
235         return getPositionWorldOnB(new Vector3f());
236     }
237 
getPositionWorldOnB(Vector3f positionWorldOnB)238     public Vector3f getPositionWorldOnB(Vector3f positionWorldOnB) {
239         getPositionWorldOnB(manifoldPointObjectId, positionWorldOnB);
240         return positionWorldOnB;
241     }
getPositionWorldOnB(long manifoldPointObjectId, Vector3f positionWorldOnB)242     private native void getPositionWorldOnB(long manifoldPointObjectId, Vector3f positionWorldOnB);
243 
244 //    public Object getUserPersistentData() {
245 //        return userPersistentData;
246 //    }
247 }
248