diff --git a/jme3-examples/src/main/java/jme3test/bullet/PhysicsTestHelper.java b/jme3-examples/src/main/java/jme3test/bullet/PhysicsTestHelper.java index c0fa6f065..4d8a5a92f 100644 --- a/jme3-examples/src/main/java/jme3test/bullet/PhysicsTestHelper.java +++ b/jme3-examples/src/main/java/jme3test/bullet/PhysicsTestHelper.java @@ -36,6 +36,7 @@ import com.jme3.asset.AssetManager; import com.jme3.asset.TextureKey; import com.jme3.bullet.PhysicsSpace; import com.jme3.bullet.collision.shapes.CollisionShape; +import com.jme3.bullet.collision.shapes.GImpactCollisionShape; import com.jme3.bullet.collision.shapes.MeshCollisionShape; import com.jme3.bullet.control.RigidBodyControl; import com.jme3.input.MouseInput; @@ -44,13 +45,18 @@ import com.jme3.input.controls.MouseButtonTrigger; import com.jme3.light.AmbientLight; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; +import com.jme3.math.FastMath; +import com.jme3.math.Vector3f; import com.jme3.renderer.queue.RenderQueue.ShadowMode; import com.jme3.scene.Geometry; +import com.jme3.scene.Mesh; import com.jme3.scene.Node; +import com.jme3.scene.VertexBuffer; import com.jme3.scene.shape.Box; import com.jme3.scene.shape.Sphere; import com.jme3.scene.shape.Sphere.TextureMode; import com.jme3.texture.Texture; +import com.jme3.util.BufferUtils; /** * @@ -59,8 +65,7 @@ import com.jme3.texture.Texture; public class PhysicsTestHelper { /** - * creates a simple physics test world with a floor, an obstacle and some - * test boxes + * creates a simple physics test world with a floor, an obstacle and some test boxes * * @param rootNode where lights and geometries should be added * @param assetManager for loading assets @@ -107,7 +112,7 @@ public class PhysicsTestHelper { space.add(sphereGeometry); } - + public static void createPhysicsTestWorldSoccer(Node rootNode, AssetManager assetManager, PhysicsSpace space) { AmbientLight light = new AmbientLight(); light.setColor(ColorRGBA.LightGray); @@ -140,24 +145,24 @@ public class PhysicsTestHelper { space.add(ballGeometry); } { - //immovable Box with mesh collision shape - Box box = new Box(1, 1, 1); - Geometry boxGeometry = new Geometry("Box", box); - boxGeometry.setMaterial(material); - boxGeometry.setLocalTranslation(4, 1, 2); - boxGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(box), 0)); - rootNode.attachChild(boxGeometry); - space.add(boxGeometry); + //immovable Box with mesh collision shape + Box box = new Box(1, 1, 1); + Geometry boxGeometry = new Geometry("Box", box); + boxGeometry.setMaterial(material); + boxGeometry.setLocalTranslation(4, 1, 2); + boxGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(box), 0)); + rootNode.attachChild(boxGeometry); + space.add(boxGeometry); } { - //immovable Box with mesh collision shape - Box box = new Box(1, 1, 1); - Geometry boxGeometry = new Geometry("Box", box); - boxGeometry.setMaterial(material); - boxGeometry.setLocalTranslation(4, 3, 4); - boxGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(box), 0)); - rootNode.attachChild(boxGeometry); - space.add(boxGeometry); + //immovable Box with mesh collision shape + Box box = new Box(1, 1, 1); + Geometry boxGeometry = new Geometry("Box", box); + boxGeometry.setMaterial(material); + boxGeometry.setLocalTranslation(4, 3, 4); + boxGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(box), 0)); + rootNode.attachChild(boxGeometry); + space.add(boxGeometry); } } @@ -211,8 +216,7 @@ public class PhysicsTestHelper { } /** - * creates the necessary inputlistener and action to shoot balls from the - * camera + * creates the necessary inputlistener and action to shoot balls from the camera * * @param app the application that's running * @param rootNode where ball geometries should be added @@ -246,4 +250,116 @@ public class PhysicsTestHelper { app.getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); app.getInputManager().addListener(actionListener, "shoot"); } + + /** + * Creates a curved "floor" with a GImpactCollisionShape provided as the RigidBodyControl's collision + * shape. Surface has four slightly concave corners to allow for multiple tests and minimize falling off + * the edge of the floor. + * + * @param assetManager for loading assets + * @param floorDimensions width/depth of the "floor" (X/Z) + * @param position sets the floor's local translation + * @return + */ + public static Geometry createGImpactTestFloor(AssetManager assetManager, float floorDimensions, Vector3f position) { + Geometry floor = createTestFloor(assetManager, floorDimensions, position, ColorRGBA.Red); + RigidBodyControl floorControl = new RigidBodyControl(new GImpactCollisionShape(floor.getMesh()), 0); + floor.addControl(floorControl); + return floor; + } + + /** + * Creates a curved "floor" with a MeshCollisionShape provided as the RigidBodyControl's collision shape. + * Surface has four slightly concave corners to allow for multiple tests and minimize falling off the edge + * of the floor. + * + * @param assetManager for loading assets + * @param floorDimensions width/depth of the "floor" (X/Z) + * @param position sets the floor's local translation + * @return + */ + public static Geometry createMeshTestFloor(AssetManager assetManager, float floorDimensions, Vector3f position) { + Geometry floor = createTestFloor(assetManager, floorDimensions, position, new ColorRGBA(0.5f, 0.5f, 0.9f, 1)); + RigidBodyControl floorControl = new RigidBodyControl(new MeshCollisionShape(floor.getMesh()), 0); + floor.addControl(floorControl); + return floor; + } + + private static Geometry createTestFloor(AssetManager assetManager, float floorDimensions, Vector3f position, ColorRGBA color) { + Geometry floor = new Geometry("floor", createFloorMesh(20, floorDimensions)); + Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); + material.getAdditionalRenderState().setWireframe(true); + material.setColor("Color", color); + floor.setMaterial(material); + floor.setLocalTranslation(position); + return floor; + } + + private static Mesh createFloorMesh(int meshDetail, float floorDimensions) { + if (meshDetail < 10) { + meshDetail = 10; + } + int numVertices = meshDetail * meshDetail * 2 * 3;//width * depth * two tris * 3 verts per tri + + int[] indexBuf = new int[numVertices]; + int i = 0; + for (int x = 0; x < meshDetail; x++) { + for (int z = 0; z < meshDetail; z++) { + indexBuf[i] = i++; + indexBuf[i] = i++; + indexBuf[i] = i++; + indexBuf[i] = i++; + indexBuf[i] = i++; + indexBuf[i] = i++; + } + } + + float[] vertBuf = new float[numVertices * 3]; + float xIncrement = floorDimensions / meshDetail; + float zIncrement = floorDimensions / meshDetail; + int j = 0; + for (int x = 0; x < meshDetail; x++) { + float xPos = x * xIncrement; + for (int z = 0; z < meshDetail; z++) { + float zPos = z * zIncrement; + //First tri + vertBuf[j++] = xPos; + vertBuf[j++] = getY(xPos, zPos, floorDimensions); + vertBuf[j++] = zPos; + vertBuf[j++] = xPos; + vertBuf[j++] = getY(xPos, zPos + zIncrement, floorDimensions); + vertBuf[j++] = zPos + zIncrement; + vertBuf[j++] = xPos + xIncrement; + vertBuf[j++] = getY(xPos + xIncrement, zPos, floorDimensions); + vertBuf[j++] = zPos; + //Second tri + vertBuf[j++] = xPos; + vertBuf[j++] = getY(xPos, zPos + zIncrement, floorDimensions); + vertBuf[j++] = zPos + zIncrement; + vertBuf[j++] = xPos + xIncrement; + vertBuf[j++] = getY(xPos + xIncrement, zPos + zIncrement, floorDimensions); + vertBuf[j++] = zPos + zIncrement; + vertBuf[j++] = xPos + xIncrement; + vertBuf[j++] = getY(xPos + xIncrement, zPos, floorDimensions); + vertBuf[j++] = zPos; + } + } + + Mesh m = new Mesh(); + m.setBuffer(VertexBuffer.Type.Index, 1, BufferUtils.createIntBuffer(indexBuf)); + m.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertBuf)); + m.updateBound(); + return m; + } + + private static float getY(float x, float z, float max) { + float yMaxHeight = 8; + float xv = FastMath.unInterpolateLinear(FastMath.abs(x - (max / 2)), 0, max) * FastMath.TWO_PI; + float zv = FastMath.unInterpolateLinear(FastMath.abs(z - (max / 2)), 0, max) * FastMath.TWO_PI; + + float xComp = (FastMath.sin(xv) + 1) * 0.5f; + float zComp = (FastMath.sin(zv) + 1) * 0.5f; + + return -yMaxHeight * xComp * zComp; + } } diff --git a/jme3-examples/src/main/java/jme3test/bullet/shape/TestGimpactShape.java b/jme3-examples/src/main/java/jme3test/bullet/shape/TestGimpactShape.java new file mode 100644 index 000000000..3c919268a --- /dev/null +++ b/jme3-examples/src/main/java/jme3test/bullet/shape/TestGimpactShape.java @@ -0,0 +1,342 @@ +/* + * Copyright (c) 2009-2019 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package jme3test.bullet.shape; + +import com.jme3.app.SimpleApplication; +import com.jme3.bullet.BulletAppState; +import com.jme3.bullet.PhysicsSpace; +import com.jme3.bullet.collision.shapes.GImpactCollisionShape; +import com.jme3.bullet.control.RigidBodyControl; +import com.jme3.bullet.debug.BulletDebugAppState; +import com.jme3.font.BitmapFont; +import com.jme3.font.BitmapText; +import com.jme3.input.KeyInput; +import com.jme3.input.controls.ActionListener; +import com.jme3.input.controls.KeyTrigger; +import com.jme3.light.DirectionalLight; +import com.jme3.material.Material; +import com.jme3.math.ColorRGBA; +import com.jme3.math.FastMath; +import com.jme3.math.Vector3f; +import com.jme3.scene.Geometry; +import com.jme3.scene.Mesh; +import com.jme3.scene.Node; +import com.jme3.scene.Spatial; +import com.jme3.scene.shape.PQTorus; +import com.jme3.scene.shape.Torus; +import com.jme3.system.AppSettings; +import java.util.ArrayList; +import java.util.List; +import jme3test.bullet.PhysicsTestHelper; + +/** + * This test demonstrates various GImpactCollisionShapes colliding against two identical curved surfaces. The + * left surface is a MeshCollisionShape, right surface is another GImpactCollisionShape. An ideal result is + * for all objects to land and change to a blue colored mesh indicating they are inactive. Falling through the + * floor, or never going inactive (bouncing forever) are failure conditions. + *
+ * Observations as of June 2019 (JME v3.3.0-alpha2): + *