1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests.bullet; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.Color; 21 import com.badlogic.gdx.physics.bullet.collision.ContactListener; 22 import com.badlogic.gdx.utils.Array; 23 24 /** @author Xoppa */ 25 public class ContactCallbackTest extends BaseBulletTest { 26 // ContactProcessedListenerXXX is called AFTER the contact is processed. 27 // Use ContactAddedListenerXXX to get a callback BEFORE the contact processed, 28 // which allows you to alter the objects/manifold before it's processed. 29 public static class TestContactProcessedListener extends ContactListener { 30 public Array<BulletEntity> entities; 31 int c = 0; 32 33 @Override onContactProcessed(int userValue0, boolean match0, int userValue1, boolean match1)34 public void onContactProcessed (int userValue0, boolean match0, int userValue1, boolean match1) { 35 if (match0) { 36 final BulletEntity e = (BulletEntity)(entities.get(userValue0)); 37 // Disable future callbacks for this entity 38 e.body.setContactCallbackFilter(0); 39 e.setColor(Color.RED); 40 Gdx.app.log("ContactCallbackTest", "Contact processed " + (++c)); 41 } 42 if (match1) { 43 final BulletEntity e = (BulletEntity)(entities.get(userValue1)); 44 // Disable future callbacks for this entity 45 e.body.setContactCallbackFilter(0); 46 e.setColor(Color.RED); 47 Gdx.app.log("ContactCallbackTest", "Contact processed " + (++c)); 48 } 49 } 50 } 51 52 final int BOXCOUNT_X = 5; 53 final int BOXCOUNT_Y = 1; 54 final int BOXCOUNT_Z = 5; 55 56 final float BOXOFFSET_X = -5f; 57 final float BOXOFFSET_Y = 0.5f; 58 final float BOXOFFSET_Z = -5f; 59 60 TestContactProcessedListener contactProcessedListener; 61 62 @Override create()63 public void create () { 64 super.create(); 65 66 // Create the entities 67 world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(), 68 0.25f + 0.5f * (float)Math.random(), 1f); 69 70 for (int x = 0; x < BOXCOUNT_X; x++) { 71 for (int y = 0; y < BOXCOUNT_Y; y++) { 72 for (int z = 0; z < BOXCOUNT_Z; z++) { 73 final BulletEntity e = (BulletEntity)world.add("box", BOXOFFSET_X + x * 2f, BOXOFFSET_Y + y * 2f, BOXOFFSET_Z + z 74 * 2f); 75 e.setColor(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 76 0.5f + 0.5f * (float)Math.random(), 1f); 77 78 e.body.setContactCallbackFlag(2); 79 e.body.setContactCallbackFilter(2); 80 } 81 } 82 } 83 84 // Creating a contact listener, also enables that particular type of contact listener and sets it active. 85 contactProcessedListener = new TestContactProcessedListener(); 86 contactProcessedListener.entities = world.entities; 87 } 88 89 @Override tap(float x, float y, int count, int button)90 public boolean tap (float x, float y, int count, int button) { 91 shoot(x, y); 92 return true; 93 } 94 95 @Override dispose()96 public void dispose () { 97 // Deleting the active contact listener, also disables that particular type of contact listener. 98 if (contactProcessedListener != null) contactProcessedListener.dispose(); 99 contactProcessedListener = null; 100 super.dispose(); 101 } 102 } 103