159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/* 259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine 359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved. 459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without 659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are 759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met: 859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright 1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer. 1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright 1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer in the 1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * documentation and/or other materials provided with the distribution. 1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * may be used to endorse or promote products derived from this software 1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * without specific prior written permission. 1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.bullet.collision.shapes; 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.bulletphysics.collision.shapes.CompoundShape; 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.bulletphysics.linearmath.Transform; 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.collision.shapes.infos.ChildCollisionShape; 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.util.Converter; 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.InputCapsule; 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.JmeExporter; 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.JmeImporter; 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.OutputCapsule; 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Matrix3f; 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f; 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.IOException; 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.ArrayList; 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.Iterator; 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.List; 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Level; 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.logging.Logger; 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * A CompoundCollisionShape allows combining multiple base shapes 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * to generate a more sophisticated shape. 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author normenhansen 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class CompoundCollisionShape extends CollisionShape { 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected ArrayList<ChildCollisionShape> children = new ArrayList<ChildCollisionShape>(); 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public CompoundCollisionShape() { 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta cShape = new CompoundShape(); 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * adds a child shape at the given local translation 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param shape the child shape to add 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param location the local location of the child shape 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void addChildShape(CollisionShape shape, Vector3f location) { 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Transform transA = new Transform(Converter.convert(new Matrix3f())); 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Converter.convert(location, transA.origin); 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta children.add(new ChildCollisionShape(location.clone(), new Matrix3f(), shape)); 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ((CompoundShape) cShape).addChildShape(transA, shape.getCShape()); 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * adds a child shape at the given local translation 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param shape the child shape to add 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param location the local location of the child shape 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void addChildShape(CollisionShape shape, Vector3f location, Matrix3f rotation) { 8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if(shape instanceof CompoundCollisionShape){ 8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta throw new IllegalStateException("CompoundCollisionShapes cannot have CompoundCollisionShapes as children!"); 8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Transform transA = new Transform(Converter.convert(rotation)); 8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Converter.convert(location, transA.origin); 8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Converter.convert(rotation, transA.basis); 8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta children.add(new ChildCollisionShape(location.clone(), rotation.clone(), shape)); 8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ((CompoundShape) cShape).addChildShape(transA, shape.getCShape()); 9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private void addChildShapeDirect(CollisionShape shape, Vector3f location, Matrix3f rotation) { 9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if(shape instanceof CompoundCollisionShape){ 9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta throw new IllegalStateException("CompoundCollisionShapes cannot have CompoundCollisionShapes as children!"); 9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Transform transA = new Transform(Converter.convert(rotation)); 9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Converter.convert(location, transA.origin); 9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Converter.convert(rotation, transA.basis); 9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ((CompoundShape) cShape).addChildShape(transA, shape.getCShape()); 10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * removes a child shape 10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param shape the child shape to remove 10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void removeChildShape(CollisionShape shape) { 10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ((CompoundShape) cShape).removeChildShape(shape.getCShape()); 10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (Iterator<ChildCollisionShape> it = children.iterator(); it.hasNext();) { 10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ChildCollisionShape childCollisionShape = it.next(); 11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (childCollisionShape.shape == shape) { 11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta it.remove(); 11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public List<ChildCollisionShape> getChildren() { 11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return children; 11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * WARNING - CompoundCollisionShape scaling has no effect. 12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void setScale(Vector3f scale) { 12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "CompoundCollisionShape cannot be scaled"); 12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void write(JmeExporter ex) throws IOException { 12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta super.write(ex); 13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta OutputCapsule capsule = ex.getCapsule(this); 13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta capsule.writeSavableArrayList(children, "children", new ArrayList<ChildCollisionShape>()); 13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void read(JmeImporter im) throws IOException { 13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta super.read(im); 13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta InputCapsule capsule = im.getCapsule(this); 13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta children = capsule.readSavableArrayList("children", new ArrayList<ChildCollisionShape>()); 13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta cShape.setLocalScaling(Converter.convert(getScale())); 13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta cShape.setMargin(margin); 14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta loadChildren(); 14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private void loadChildren() { 14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (Iterator<ChildCollisionShape> it = children.iterator(); it.hasNext();) { 14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ChildCollisionShape child = it.next(); 14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta addChildShapeDirect(child.shape, child.location, child.rotation); 14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta} 151