2 * Copyright (c) 2009-2010 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
32 package com.jme3.bullet.collision;
34 import com.jme3.asset.AssetManager;
35 import com.jme3.bullet.collision.shapes.CollisionShape;
36 import com.jme3.bullet.util.DebugShapeFactory;
37 import com.jme3.export.InputCapsule;
38 import com.jme3.export.JmeExporter;
39 import com.jme3.export.JmeImporter;
40 import com.jme3.export.OutputCapsule;
41 import com.jme3.export.Savable;
42 import com.jme3.material.Material;
43 import com.jme3.math.ColorRGBA;
44 import com.jme3.math.Vector3f;
45 import com.jme3.scene.Geometry;
46 import com.jme3.scene.Node;
47 import com.jme3.scene.Spatial;
48 import com.jme3.scene.debug.Arrow;
49 import java.io.IOException;
50 import java.util.Iterator;
51 import java.util.List;
52 import java.util.logging.Level;
53 import java.util.logging.Logger;
56 * Base class for collision objects (PhysicsRigidBody, PhysicsGhostObject)
57 * @author normenhansen
59 public abstract class PhysicsCollisionObject implements Savable {
61 protected long objectId = 0;
62 protected Spatial debugShape;
63 protected Arrow debugArrow;
64 protected Geometry debugArrowGeom;
65 protected Material debugMaterialBlue;
66 protected Material debugMaterialRed;
67 protected Material debugMaterialGreen;
68 protected Material debugMaterialYellow;
69 protected CollisionShape collisionShape;
70 public static final int COLLISION_GROUP_NONE = 0x00000000;
71 public static final int COLLISION_GROUP_01 = 0x00000001;
72 public static final int COLLISION_GROUP_02 = 0x00000002;
73 public static final int COLLISION_GROUP_03 = 0x00000004;
74 public static final int COLLISION_GROUP_04 = 0x00000008;
75 public static final int COLLISION_GROUP_05 = 0x00000010;
76 public static final int COLLISION_GROUP_06 = 0x00000020;
77 public static final int COLLISION_GROUP_07 = 0x00000040;
78 public static final int COLLISION_GROUP_08 = 0x00000080;
79 public static final int COLLISION_GROUP_09 = 0x00000100;
80 public static final int COLLISION_GROUP_10 = 0x00000200;
81 public static final int COLLISION_GROUP_11 = 0x00000400;
82 public static final int COLLISION_GROUP_12 = 0x00000800;
83 public static final int COLLISION_GROUP_13 = 0x00001000;
84 public static final int COLLISION_GROUP_14 = 0x00002000;
85 public static final int COLLISION_GROUP_15 = 0x00004000;
86 public static final int COLLISION_GROUP_16 = 0x00008000;
87 protected int collisionGroup = 0x00000001;
88 protected int collisionGroupsMask = 0x00000001;
89 private Object userObject;
92 * Sets a CollisionShape to this physics object, note that the object should
93 * not be in the physics space when adding a new collision shape as it is rebuilt
94 * on the physics side.
95 * @param collisionShape the CollisionShape to set
97 public void setCollisionShape(CollisionShape collisionShape) {
98 this.collisionShape = collisionShape;
103 * @return the CollisionShape of this PhysicsNode, to be able to reuse it with
104 * other physics nodes (increases performance)
106 public CollisionShape getCollisionShape() {
107 return collisionShape;
111 * Returns the collision group for this collision shape
114 public int getCollisionGroup() {
115 return collisionGroup;
119 * Sets the collision group number for this physics object. <br>
120 * The groups are integer bit masks and some pre-made variables are available in CollisionObject.
121 * All physics objects are by default in COLLISION_GROUP_01.<br>
122 * Two object will collide when <b>one</b> of the partys has the
123 * collisionGroup of the other in its collideWithGroups set.
124 * @param collisionGroup the collisionGroup to set
126 public void setCollisionGroup(int collisionGroup) {
127 this.collisionGroup = collisionGroup;
129 setCollisionGroup(objectId, collisionGroup);
134 * Add a group that this object will collide with.<br>
135 * Two object will collide when <b>one</b> of the partys has the
136 * collisionGroup of the other in its collideWithGroups set.<br>
137 * @param collisionGroup
139 public void addCollideWithGroup(int collisionGroup) {
140 this.collisionGroupsMask = this.collisionGroupsMask | collisionGroup;
142 setCollideWithGroups(objectId, this.collisionGroupsMask);
147 * Remove a group from the list this object collides with.
148 * @param collisionGroup
150 public void removeCollideWithGroup(int collisionGroup) {
151 this.collisionGroupsMask = this.collisionGroupsMask & ~collisionGroup;
153 setCollideWithGroups(this.collisionGroupsMask);
158 * Directly set the bitmask for collision groups that this object collides with.
159 * @param collisionGroup
161 public void setCollideWithGroups(int collisionGroups) {
162 this.collisionGroupsMask = collisionGroups;
164 setCollideWithGroups(objectId, this.collisionGroupsMask);
169 * Gets the bitmask of collision groups that this object collides with.
172 public int getCollideWithGroups() {
173 return collisionGroupsMask;
176 protected void initUserPointer() {
177 Logger.getLogger(this.getClass().getName()).log(Level.FINE, "initUserPointer() objectId = {0}", Long.toHexString(objectId));
178 initUserPointer(objectId, collisionGroup, collisionGroupsMask);
180 native void initUserPointer(long objectId, int group, int groups);
182 * Creates a visual debug shape of the current collision shape of this physics object<br/>
183 * <b>Does not work with detached physics, please switch to PARALLEL or SEQUENTIAL for debugging</b>
184 * @param manager AssetManager to load the default wireframe material for the debug shape
186 protected Spatial attachDebugShape(AssetManager manager) {
187 debugMaterialBlue = new Material(manager, "Common/MatDefs/Misc/Unshaded.j3md");
188 debugMaterialBlue.getAdditionalRenderState().setWireframe(true);
189 debugMaterialBlue.setColor("Color", ColorRGBA.Blue);
190 debugMaterialGreen = new Material(manager, "Common/MatDefs/Misc/Unshaded.j3md");
191 debugMaterialGreen.getAdditionalRenderState().setWireframe(true);
192 debugMaterialGreen.setColor("Color", ColorRGBA.Green);
193 debugMaterialRed = new Material(manager, "Common/MatDefs/Misc/Unshaded.j3md");
194 debugMaterialRed.getAdditionalRenderState().setWireframe(true);
195 debugMaterialRed.setColor("Color", ColorRGBA.Red);
196 debugMaterialYellow = new Material(manager, "Common/MatDefs/Misc/Unshaded.j3md");
197 debugMaterialYellow.getAdditionalRenderState().setWireframe(true);
198 debugMaterialYellow.setColor("Color", ColorRGBA.Yellow);
199 debugArrow = new Arrow(Vector3f.UNIT_XYZ);
200 debugArrowGeom = new Geometry("DebugArrow", debugArrow);
201 debugArrowGeom.setMaterial(debugMaterialGreen);
202 return attachDebugShape();
206 * creates a debug shape for this CollisionObject
210 public Spatial createDebugShape(AssetManager manager){
211 return attachDebugShape(manager);
214 protected Spatial attachDebugShape(Material material) {
215 debugMaterialBlue = material;
216 debugMaterialGreen = material;
217 debugMaterialRed = material;
218 debugMaterialYellow = material;
219 debugArrow = new Arrow(Vector3f.UNIT_XYZ);
220 debugArrowGeom = new Geometry("DebugArrow", debugArrow);
221 debugArrowGeom.setMaterial(debugMaterialGreen);
222 return attachDebugShape();
225 public Spatial debugShape() {
230 * Creates a visual debug shape of the current collision shape of this physics object<br/>
231 * <b>Does not work with detached physics, please switch to PARALLEL or SEQUENTIAL for debugging</b>
232 * @param material Material to use for the debug shape
234 protected Spatial attachDebugShape() {
235 if (debugShape != null) {
238 Spatial spatial = getDebugShape();
239 this.debugShape = spatial;
243 protected void updateDebugShape() {
244 if (debugShape != null) {
250 protected Spatial getDebugShape() {
251 Spatial spatial = DebugShapeFactory.getDebugShape(collisionShape);
252 if (spatial == null) {
253 return new Node("nullnode");
255 if (spatial instanceof Node) {
256 List<Spatial> children = ((Node) spatial).getChildren();
257 for (Iterator<Spatial> it1 = children.iterator(); it1.hasNext();) {
258 Spatial spatial1 = it1.next();
259 Geometry geom = ((Geometry) spatial1);
260 geom.setMaterial(debugMaterialBlue);
261 geom.setCullHint(Spatial.CullHint.Never);
264 Geometry geom = ((Geometry) spatial);
265 geom.setMaterial(debugMaterialBlue);
266 geom.setCullHint(Spatial.CullHint.Never);
268 spatial.setCullHint(Spatial.CullHint.Never);
273 * Removes the debug shape
275 public void detachDebugShape() {
280 * @return the userObject
282 public Object getUserObject() {
287 * @param userObject the userObject to set
289 public void setUserObject(Object userObject) {
290 this.userObject = userObject;
293 public long getObjectId(){
297 protected native void attachCollisionShape(long objectId, long collisionShapeId);
298 native void setCollisionGroup(long objectId, int collisionGroup);
299 native void setCollideWithGroups(long objectId, int collisionGroups);
302 public void write(JmeExporter e) throws IOException {
303 OutputCapsule capsule = e.getCapsule(this);
304 capsule.write(collisionGroup, "collisionGroup", 0x00000001);
305 capsule.write(collisionGroupsMask, "collisionGroupsMask", 0x00000001);
306 capsule.write(debugShape, "debugShape", null);
307 capsule.write(collisionShape, "collisionShape", null);
311 public void read(JmeImporter e) throws IOException {
312 InputCapsule capsule = e.getCapsule(this);
313 collisionGroup = capsule.readInt("collisionGroup", 0x00000001);
314 collisionGroupsMask = capsule.readInt("collisionGroupsMask", 0x00000001);
315 debugShape = (Spatial) capsule.readSavable("debugShape", null);
316 CollisionShape shape = (CollisionShape) capsule.readSavable("collisionShape", null);
317 collisionShape = shape;
321 protected void finalize() throws Throwable {
323 Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Finalizing CollisionObject {0}", Long.toHexString(objectId));
324 finalizeNative(objectId);
327 protected native void finalizeNative(long objectId);