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.graphics.Mesh; 22 import com.badlogic.gdx.graphics.VertexAttribute; 23 import com.badlogic.gdx.graphics.VertexAttributes.Usage; 24 import com.badlogic.gdx.math.Vector3; 25 import com.badlogic.gdx.math.collision.Ray; 26 import com.badlogic.gdx.physics.bullet.collision.btCollisionObject; 27 import com.badlogic.gdx.physics.bullet.collision.ClosestRayResultCallback; 28 import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody; 29 30 /** @author xoppa */ 31 public class RayCastTest extends BaseBulletTest { 32 final int BOXCOUNT_X = 5; 33 final int BOXCOUNT_Y = 5; 34 final int BOXCOUNT_Z = 1; 35 36 final float BOXOFFSET_X = 0f; 37 final float BOXOFFSET_Y = 0.5f; 38 final float BOXOFFSET_Z = 2.5f; 39 40 ClosestRayResultCallback rayTestCB; 41 Vector3 rayFrom = new Vector3(); 42 Vector3 rayTo = new Vector3(); 43 44 @Override create()45 public void create () { 46 super.create(); 47 instructions = "Tap a box to ray cast\nLong press to toggle debug mode\nSwipe for next test\nCtrl+drag to rotate\nScroll to zoom"; 48 49 // Create the entities 50 world.add("ground", -7f, 0f, -7f).setColor(0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(), 51 0.25f + 0.5f * (float)Math.random(), 1f); 52 53 for (int x = 0; x < BOXCOUNT_X; x++) { 54 for (int y = 0; y < BOXCOUNT_Y; y++) { 55 for (int z = 0; z < BOXCOUNT_Z; z++) { 56 world.add("box", BOXOFFSET_X + x, BOXOFFSET_Y + y, BOXOFFSET_Z + z).setColor(0.5f + 0.5f * (float)Math.random(), 57 0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 1f); 58 } 59 } 60 } 61 62 rayTestCB = new ClosestRayResultCallback(Vector3.Zero, Vector3.Z); 63 } 64 65 @Override dispose()66 public void dispose () { 67 if (rayTestCB != null) rayTestCB.dispose(); 68 rayTestCB = null; 69 super.dispose(); 70 } 71 72 @Override tap(float x, float y, int count, int button)73 public boolean tap (float x, float y, int count, int button) { 74 Ray ray = camera.getPickRay(x, y); 75 rayFrom.set(ray.origin); 76 rayTo.set(ray.direction).scl(50f).add(rayFrom); // 50 meters max from the origin 77 78 // Because we reuse the ClosestRayResultCallback, we need reset it's values 79 rayTestCB.setCollisionObject(null); 80 rayTestCB.setClosestHitFraction(1f); 81 rayTestCB.setRayFromWorld(rayFrom); 82 rayTestCB.setRayToWorld(rayTo); 83 84 world.collisionWorld.rayTest(rayFrom, rayTo, rayTestCB); 85 86 if (rayTestCB.hasHit()) { 87 final btCollisionObject obj = rayTestCB.getCollisionObject(); 88 if (!obj.isStaticOrKinematicObject()) { 89 final btRigidBody body = (btRigidBody)(obj); 90 body.activate(); 91 body.applyCentralImpulse(tmpV2.set(ray.direction).scl(20f)); 92 } 93 } 94 return true; 95 } 96 } 97