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 */
32package com.jme3.bullet.collision.shapes;
33
34import com.jme3.bullet.collision.shapes.infos.ChildCollisionShape;
35import com.jme3.export.InputCapsule;
36import com.jme3.export.JmeExporter;
37import com.jme3.export.JmeImporter;
38import com.jme3.export.OutputCapsule;
39import com.jme3.math.Matrix3f;
40import com.jme3.math.Vector3f;
41import java.io.IOException;
42import java.util.ArrayList;
43import java.util.Iterator;
44import java.util.List;
45import java.util.logging.Level;
46import java.util.logging.Logger;
47
48/**
49 * A CompoundCollisionShape allows combining multiple base shapes
50 * to generate a more sophisticated shape.
51 * @author normenhansen
52 */
53public class CompoundCollisionShape extends CollisionShape {
54
55    protected ArrayList<ChildCollisionShape> children = new ArrayList<ChildCollisionShape>();
56
57    public CompoundCollisionShape() {
58        objectId = createShape();//new CompoundShape();
59        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId));
60    }
61
62    /**
63     * adds a child shape at the given local translation
64     * @param shape the child shape to add
65     * @param location the local location of the child shape
66     */
67    public void addChildShape(CollisionShape shape, Vector3f location) {
68//        Transform transA = new Transform(Converter.convert(new Matrix3f()));
69//        Converter.convert(location, transA.origin);
70//        children.add(new ChildCollisionShape(location.clone(), new Matrix3f(), shape));
71//        ((CompoundShape) objectId).addChildShape(transA, shape.getObjectId());
72        addChildShape(shape, location, new Matrix3f());
73    }
74
75    /**
76     * adds a child shape at the given local translation
77     * @param shape the child shape to add
78     * @param location the local location of the child shape
79     */
80    public void addChildShape(CollisionShape shape, Vector3f location, Matrix3f rotation) {
81        if(shape instanceof CompoundCollisionShape){
82            throw new IllegalStateException("CompoundCollisionShapes cannot have CompoundCollisionShapes as children!");
83        }
84//        Transform transA = new Transform(Converter.convert(rotation));
85//        Converter.convert(location, transA.origin);
86//        Converter.convert(rotation, transA.basis);
87        children.add(new ChildCollisionShape(location.clone(), rotation.clone(), shape));
88        addChildShape(objectId, shape.getObjectId(), location, rotation);
89//        ((CompoundShape) objectId).addChildShape(transA, shape.getObjectId());
90    }
91
92    private void addChildShapeDirect(CollisionShape shape, Vector3f location, Matrix3f rotation) {
93        if(shape instanceof CompoundCollisionShape){
94            throw new IllegalStateException("CompoundCollisionShapes cannot have CompoundCollisionShapes as children!");
95        }
96//        Transform transA = new Transform(Converter.convert(rotation));
97//        Converter.convert(location, transA.origin);
98//        Converter.convert(rotation, transA.basis);
99        addChildShape(objectId, shape.getObjectId(), location, rotation);
100//        ((CompoundShape) objectId).addChildShape(transA, shape.getObjectId());
101    }
102
103    /**
104     * removes a child shape
105     * @param shape the child shape to remove
106     */
107    public void removeChildShape(CollisionShape shape) {
108        removeChildShape(objectId, shape.getObjectId());
109//        ((CompoundShape) objectId).removeChildShape(shape.getObjectId());
110        for (Iterator<ChildCollisionShape> it = children.iterator(); it.hasNext();) {
111            ChildCollisionShape childCollisionShape = it.next();
112            if (childCollisionShape.shape == shape) {
113                it.remove();
114            }
115        }
116    }
117
118    public List<ChildCollisionShape> getChildren() {
119        return children;
120    }
121
122    /**
123     * WARNING - CompoundCollisionShape scaling has no effect.
124     */
125    @Override
126    public void setScale(Vector3f scale) {
127        Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "CompoundCollisionShape cannot be scaled");
128    }
129
130    private native long createShape();
131
132    private native long addChildShape(long objectId, long childId, Vector3f location, Matrix3f rotation);
133
134    private native long removeChildShape(long objectId, long childId);
135
136    public void write(JmeExporter ex) throws IOException {
137        super.write(ex);
138        OutputCapsule capsule = ex.getCapsule(this);
139        capsule.writeSavableArrayList(children, "children", new ArrayList<ChildCollisionShape>());
140    }
141
142    public void read(JmeImporter im) throws IOException {
143        super.read(im);
144        InputCapsule capsule = im.getCapsule(this);
145        children = capsule.readSavableArrayList("children", new ArrayList<ChildCollisionShape>());
146        setScale(scale);
147        setMargin(margin);
148        loadChildren();
149    }
150
151    private void loadChildren() {
152        for (Iterator<ChildCollisionShape> it = children.iterator(); it.hasNext();) {
153            ChildCollisionShape child = it.next();
154            addChildShapeDirect(child.shape, child.location, child.rotation);
155        }
156    }
157
158}
159