OSDN Git Service

Add CollisionWorldTest
authorXoppa <contact@xoppa.nl>
Thu, 31 Jan 2013 19:20:41 +0000 (20:20 +0100)
committerXoppa <contact@xoppa.nl>
Thu, 31 Jan 2013 19:20:41 +0000 (20:20 +0100)
tests/gdx-tests/src/com/badlogic/gdx/tests/BulletTestCollection.java
tests/gdx-tests/src/com/badlogic/gdx/tests/bullet/CollisionWorldTest.java [new file with mode: 0644]

index ae256e1..3cd5701 100644 (file)
@@ -32,6 +32,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Label;
 import com.badlogic.gdx.scenes.scene2d.utils.Align;
 import com.badlogic.gdx.tests.box2d.Box2DTest;
 import com.badlogic.gdx.tests.bullet.BulletTest;
+import com.badlogic.gdx.tests.bullet.CollisionWorldTest;
 import com.badlogic.gdx.tests.bullet.ConstraintsTest;
 import com.badlogic.gdx.tests.bullet.ConvexHullTest;
 import com.badlogic.gdx.tests.bullet.InternalTickTest;
@@ -48,7 +49,7 @@ import com.badlogic.gdx.tests.utils.GdxTest;
 public class BulletTestCollection extends GdxTest implements InputProcessor, GestureListener {
        protected final BulletTest[] tests = {new ShootTest(), new KinematicTest(), new ConstraintsTest(), 
                new MeshShapeTest(), new ConvexHullTest(), new RayCastTest(), new RayPickRagdollTest(), new InternalTickTest(), 
-               new SoftBodyTest(), new SoftMeshTest()};
+               new CollisionWorldTest(), new SoftBodyTest(), new SoftMeshTest()};
        
        protected int testIndex = 0;
        
diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/bullet/CollisionWorldTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/bullet/CollisionWorldTest.java
new file mode 100644 (file)
index 0000000..9611f94
--- /dev/null
@@ -0,0 +1,120 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+
+package com.badlogic.gdx.tests.bullet;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.graphics.Color;
+import com.badlogic.gdx.graphics.g3d.model.Model;
+import com.badlogic.gdx.math.Matrix4;
+import com.badlogic.gdx.math.Vector3;
+import com.badlogic.gdx.physics.bullet.ContactResultCallback;
+import com.badlogic.gdx.physics.bullet.btCollisionDispatcher;
+import com.badlogic.gdx.physics.bullet.btCollisionObject;
+import com.badlogic.gdx.physics.bullet.btCollisionObjectWrapper;
+import com.badlogic.gdx.physics.bullet.btCollisionWorld;
+import com.badlogic.gdx.physics.bullet.btDbvtBroadphase;
+import com.badlogic.gdx.physics.bullet.btDefaultCollisionConfiguration;
+import com.badlogic.gdx.physics.bullet.btManifoldPoint;
+import com.badlogic.gdx.physics.bullet.gdxBulletJNI;
+
+/** @author xoppa */
+public class CollisionWorldTest extends BaseBulletTest {
+       BulletEntity movingBox;
+       boolean hit = false;
+       Color normalColor = new Color();
+       btCollisionObject other;
+               
+       public class TestContactResultCallback extends ContactResultCallback
+       {
+               @Override
+               public float addSingleResult (btManifoldPoint cp, btCollisionObjectWrapper colObj0Wrap, int partId0, int index0,
+                       btCollisionObjectWrapper colObj1Wrap, int partId1, int index1) {
+                       hit = true;
+                       other = colObj0Wrap.getM_collisionObject() == movingBox.body ?
+                                       colObj1Wrap.getM_collisionObject() : colObj0Wrap.getM_collisionObject();
+                       
+                       return 0f;
+               }
+       }
+       TestContactResultCallback contactCB;
+       
+       
+       @Override
+       public BulletWorld createWorld () {
+               btDefaultCollisionConfiguration collisionConfig = new btDefaultCollisionConfiguration();
+               btCollisionDispatcher dispatcher = new btCollisionDispatcher(collisionConfig);
+               btDbvtBroadphase broadphase = new btDbvtBroadphase();
+               btCollisionWorld collisionWorld = new btCollisionWorld(dispatcher, broadphase, collisionConfig);
+               return new BulletWorld(collisionConfig, dispatcher, broadphase, null, collisionWorld);
+       }
+       
+       @Override
+       public void create () {
+               super.create();
+               
+               contactCB = new TestContactResultCallback();
+               
+               Model groundModel = world.getConstructor("ground").model;
+               Model boxModel = world.getConstructor("box").model;
+               
+               world.addConstructor("collisionGround", new BulletConstructor(groundModel));
+               world.addConstructor("collisionBox", new BulletConstructor(boxModel));
+               
+               world.add("collisionGround", 0f, 0f, 0f)
+                       .color.set(0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(), 1f);
+               
+               world.add("collisionBox", 0f, 1f, 5f)
+                       .color.set(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 1f);
+               world.add("collisionBox", 0f, 1f, -5f)
+                       .color.set(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 1f);
+               world.add("collisionBox", 5f, 1f, 0f)
+                       .color.set(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 1f);
+               world.add("collisionBox", -5f, 1f, 0f)
+                       .color.set(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 1f);
+               movingBox = world.add("collisionBox", -5f, 1f, 0f);
+               normalColor.set(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 1f);
+       }
+       
+       Color tmpColor = new Color();
+       @Override
+       public void render () {
+               movingBox.transform.val[Matrix4.M03] = movingBox.transform.val[Matrix4.M13] = movingBox.transform.val[Matrix4.M23] = 0f;
+               movingBox.transform.rotate(Vector3.Y, Gdx.graphics.getDeltaTime() * 45f);
+               movingBox.transform.translate(-5f, 1f, 0f);
+               movingBox.body.setWorldTransform(movingBox.transform);
+               world.collisionWorld.performDiscreteCollisionDetection();
+               hit = false;
+               other = null;
+               world.collisionWorld.contactTest(movingBox.body, contactCB);
+               movingBox.color.set(hit ? Color.RED : normalColor);
+               BulletEntity e = null;
+               if (other != null && other.userData != null && other.userData instanceof BulletEntity) { 
+                       e = (BulletEntity)(other.userData);
+                       tmpColor.set(e.color);
+                       e.color.set(Color.RED);
+               }
+               super.render();
+               if (e != null)
+                       e.color.set(tmpColor);
+       }
+       
+       @Override
+       public void dispose () {
+               super.dispose();
+               movingBox = null;
+       }
+}