• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.shapes;
33 
34 import com.bulletphysics.collision.shapes.IndexedMesh;
35 import com.bulletphysics.collision.shapes.TriangleIndexVertexArray;
36 import com.bulletphysics.extras.gimpact.GImpactMeshShape;
37 import com.jme3.bullet.util.Converter;
38 import com.jme3.export.InputCapsule;
39 import com.jme3.export.JmeExporter;
40 import com.jme3.export.JmeImporter;
41 import com.jme3.export.OutputCapsule;
42 import com.jme3.math.Vector3f;
43 import com.jme3.scene.Mesh;
44 import java.io.IOException;
45 import java.nio.ByteBuffer;
46 
47 /**
48  * Basic mesh collision shape
49  * @author normenhansen
50  */
51 public class GImpactCollisionShape extends CollisionShape{
52 
53     protected Vector3f worldScale;
54     protected int numVertices, numTriangles, vertexStride, triangleIndexStride;
55     protected ByteBuffer triangleIndexBase, vertexBase;
56     protected IndexedMesh bulletMesh;
57 
GImpactCollisionShape()58     public GImpactCollisionShape() {
59     }
60 
61     /**
62      * creates a collision shape from the given Mesh
63      * @param mesh the Mesh to use
64      */
GImpactCollisionShape(Mesh mesh)65     public GImpactCollisionShape(Mesh mesh) {
66         createCollisionMesh(mesh, new Vector3f(1,1,1));
67     }
68 
69 
createCollisionMesh(Mesh mesh, Vector3f worldScale)70     private void createCollisionMesh(Mesh mesh, Vector3f worldScale) {
71         this.worldScale = worldScale;
72         bulletMesh = Converter.convert(mesh);
73         this.numVertices = bulletMesh.numVertices;
74         this.numTriangles = bulletMesh.numTriangles;
75         this.vertexStride = bulletMesh.vertexStride;
76         this.triangleIndexStride = bulletMesh.triangleIndexStride;
77         this.triangleIndexBase = bulletMesh.triangleIndexBase;
78         this.vertexBase = bulletMesh.vertexBase;
79         createShape();
80     }
81 
82     /**
83      * creates a jme mesh from the collision shape, only needed for debugging
84      */
createJmeMesh()85     public Mesh createJmeMesh(){
86         return Converter.convert(bulletMesh);
87     }
88 
write(JmeExporter ex)89     public void write(JmeExporter ex) throws IOException {
90         super.write(ex);
91         OutputCapsule capsule = ex.getCapsule(this);
92         capsule.write(worldScale, "worldScale", new Vector3f(1, 1, 1));
93         capsule.write(numVertices, "numVertices", 0);
94         capsule.write(numTriangles, "numTriangles", 0);
95         capsule.write(vertexStride, "vertexStride", 0);
96         capsule.write(triangleIndexStride, "triangleIndexStride", 0);
97 
98         capsule.write(triangleIndexBase.array(), "triangleIndexBase", new byte[0]);
99         capsule.write(vertexBase.array(), "vertexBase", new byte[0]);
100     }
101 
read(JmeImporter im)102     public void read(JmeImporter im) throws IOException {
103         super.read(im);
104         InputCapsule capsule = im.getCapsule(this);
105         worldScale = (Vector3f) capsule.readSavable("worldScale", new Vector3f(1, 1, 1));
106         numVertices = capsule.readInt("numVertices", 0);
107         numTriangles = capsule.readInt("numTriangles", 0);
108         vertexStride = capsule.readInt("vertexStride", 0);
109         triangleIndexStride = capsule.readInt("triangleIndexStride", 0);
110 
111         triangleIndexBase = ByteBuffer.wrap(capsule.readByteArray("triangleIndexBase", new byte[0]));
112         vertexBase = ByteBuffer.wrap(capsule.readByteArray("vertexBase", new byte[0]));
113         createShape();
114     }
115 
createShape()116     protected void createShape() {
117         bulletMesh = new IndexedMesh();
118         bulletMesh.numVertices = numVertices;
119         bulletMesh.numTriangles = numTriangles;
120         bulletMesh.vertexStride = vertexStride;
121         bulletMesh.triangleIndexStride = triangleIndexStride;
122         bulletMesh.triangleIndexBase = triangleIndexBase;
123         bulletMesh.vertexBase = vertexBase;
124         bulletMesh.triangleIndexBase = triangleIndexBase;
125         TriangleIndexVertexArray tiv = new TriangleIndexVertexArray(numTriangles, triangleIndexBase, triangleIndexStride, numVertices, vertexBase, vertexStride);
126         cShape = new GImpactMeshShape(tiv);
127         cShape.setLocalScaling(Converter.convert(worldScale));
128         ((GImpactMeshShape)cShape).updateBound();
129         cShape.setLocalScaling(Converter.convert(getScale()));
130         cShape.setMargin(margin);
131     }
132 
133 }
134