import com.badlogic.gdx.physics.bullet.btCollisionObject;
import com.badlogic.gdx.physics.bullet.btCollisionObjectWrapper;
import com.badlogic.gdx.physics.bullet.btManifoldPoint;
+import com.badlogic.gdx.physics.bullet.btPersistentManifold;
import com.badlogic.gdx.tests.bullet.CollisionWorldTest.TestContactResultCallback;
import com.badlogic.gdx.utils.Array;
public class CollisionTest extends ShootTest {
BulletEntity projectile;
Array<BulletEntity> hits = new Array<BulletEntity>();
+ Array<BulletEntity> contacts = new Array<BulletEntity>();
Array<Color> colors = new Array<Color>();
public class TestContactResultCallback extends ContactResultCallback
btCollisionObject other = colObj0Wrap.getM_collisionObject() == projectile.body ?
colObj1Wrap.getM_collisionObject() : colObj0Wrap.getM_collisionObject();
if (other != null && other.userData != null && other.userData instanceof BulletEntity) {
- if (!hits.contains((BulletEntity)other.userData, true))
+ BulletEntity ent = (BulletEntity)other.userData;
+ if (ent != ground && !hits.contains(ent, true))
hits.add((BulletEntity)other.userData);
}
return 0f;
}
}
TestContactResultCallback contactCB;
+
+ public void updateContactInfo() {
+ int n = world.dispatcher.getNumManifolds();
+ for (int i = 0; i < n; i++) {
+ btPersistentManifold manifold = world.dispatcher.getManifoldByIndexInternal(i);
+ btCollisionObject objA = manifold.getBody0();
+ btCollisionObject objB = manifold.getBody1();
+ if (objA != ground.body && objB != ground.body) {
+ if (objA.userData != null && objA.userData instanceof BulletEntity) {
+ BulletEntity ent = (BulletEntity)objA.userData;
+ if (ent != projectile && !contacts.contains(ent, true) && !hits.contains(ent, true))
+ contacts.add(ent);
+ }
+ if (objB.userData != null && objB.userData instanceof BulletEntity) {
+ BulletEntity ent = (BulletEntity)objB.userData;
+ if (ent != projectile && !contacts.contains(ent, true) && !hits.contains(ent, true))
+ contacts.add(ent);
+ }
+ }
+ }
+ }
@Override
public void create () {
Color color = null;
update();
hits.clear();
+ contacts.clear();
colors.clear();
+
+ // Note that this might miss collisions, use InternalTickCallback to check for collision on every tick.
+ // See InternalTickTest on how to implement it.
+
+ // Check what the projectile hits
if (projectile != null)
world.collisionWorld.contactTest(projectile.body, contactCB);
+ // Check for other collisions
+ updateContactInfo();
+
+ color = projectile.color;
+ projectile.color = Color.RED;
if (hits.size > 0) {
- color = projectile.color;
- projectile.color = Color.RED;
for (int i = 0; i < hits.size; i++) {
colors.add(hits.get(i).color);
hits.get(i).color = Color.RED;
}
}
+ if (contacts.size > 0) {
+ for (int i = 0; i < contacts.size; i++) {
+ colors.add(contacts.get(i).color);
+ contacts.get(i).color = Color.BLUE;
+ }
+ }
render(false);
if (hits.size > 0) {
projectile.color = color;
for (int i = 0; i < hits.size; i++)
hits.get(i).color = colors.get(i);
}
+ if (contacts.size > 0) {
+ for (int i = 0; i < contacts.size; i++)
+ contacts.get(i).color = colors.get(hits.size+i);
+ }
}
@Override
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.math.collision.Ray;
+import com.badlogic.gdx.physics.bullet.btGhostObject;
/** @author xoppa */
public class ShootTest extends BaseBulletTest {
final float BOXOFFSET_Y = 0.5f;
final float BOXOFFSET_Z = 0f;
+ protected BulletEntity ground;
+
@Override
public void create () {
super.create();
// Create the entities
- world.add("ground", 0f, 0f, 0f)
+ (ground = world.add("ground", 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);
for (int x = 0; x < BOXCOUNT_X; x++) {