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.graphics.Mesh; 20 import com.badlogic.gdx.graphics.g3d.Model; 21 import com.badlogic.gdx.math.Matrix4; 22 import com.badlogic.gdx.math.Vector3; 23 import com.badlogic.gdx.math.collision.BoundingBox; 24 import com.badlogic.gdx.physics.bullet.collision.btBoxShape; 25 import com.badlogic.gdx.physics.bullet.collision.btCollisionObject; 26 import com.badlogic.gdx.physics.bullet.collision.btCollisionShape; 27 import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody.btRigidBodyConstructionInfo; 28 29 /** @author xoppa Holds the information necessary to create a bullet btRigidBody. This class should outlive the btRigidBody (entity) 30 * itself. */ 31 public class BulletConstructor extends BaseWorld.Constructor<BulletEntity> { 32 public btRigidBodyConstructionInfo bodyInfo = null; 33 public btCollisionShape shape = null; 34 private final static Vector3 tmpV = new Vector3(); 35 36 /** Specify null for the shape to use only the renderable part of this entity and not the physics part. */ 37 public BulletConstructor (final Model model, final float mass, final btCollisionShape shape) { 38 create(model, mass, shape); 39 } 40 41 /** Specify null for the shape to use only the renderable part of this entity and not the physics part. */ 42 public BulletConstructor (final Model model, final btCollisionShape shape) { 43 this(model, -1f, shape); 44 } 45 46 /** Creates a btBoxShape with the specified dimensions. */ 47 public BulletConstructor (final Model model, final float mass, final float width, final float height, final float depth) { 48 create(model, mass, width, height, depth); 49 } 50 51 /** Creates a btBoxShape with the specified dimensions and NO rigidbody. */ 52 public BulletConstructor (final Model model, final float width, final float height, final float depth) { 53 this(model, -1f, width, height, depth); 54 } 55 56 /** Creates a btBoxShape with the same dimensions as the shape. */ 57 public BulletConstructor (final Model model, final float mass) { 58 final BoundingBox boundingBox = new BoundingBox(); 59 model.calculateBoundingBox(boundingBox); 60 create(model, mass, boundingBox.getWidth(), boundingBox.getHeight(), boundingBox.getDepth()); 61 } 62 63 /** Creates a btBoxShape with the same dimensions as the shape and NO rigidbody. */ 64 public BulletConstructor (final Model model) { 65 this(model, -1f); 66 } 67 68 private void create (final Model model, final float mass, final float width, final float height, final float depth) { 69 // Create a simple boxshape 70 create(model, mass, new btBoxShape(tmpV.set(width * 0.5f, height * 0.5f, depth * 0.5f))); 71 } 72 73 private void create (final Model model, final float mass, final btCollisionShape shape) { 74 this.model = model; 75 this.shape = shape; 76 77 if (shape != null && mass >= 0) { 78 // Calculate the local inertia, bodies with no mass are static 79 Vector3 localInertia; 80 if (mass == 0) 81 localInertia = Vector3.Zero; 82 else { 83 shape.calculateLocalInertia(mass, tmpV); 84 localInertia = tmpV; 85 } 86 87 // For now just pass null as the motionstate, we'll add that to the body in the entity itself 88 bodyInfo = new btRigidBodyConstructionInfo(mass, null, shape, localInertia); 89 } 90 } 91 92 @Override 93 public void dispose () { 94 // Don't rely on the GC 95 if (bodyInfo != null) bodyInfo.dispose(); 96 if (shape != null) shape.dispose(); 97 // Remove references so the GC can do it's work 98 bodyInfo = null; 99 shape = null; 100 } 101 102 @Override 103 public BulletEntity construct (float x, float y, float z) { 104 if (bodyInfo == null && shape != null) { 105 btCollisionObject obj = new btCollisionObject(); 106 obj.setCollisionShape(shape); 107 return new BulletEntity(model, obj, x, y, z); 108 } else 109 return new BulletEntity(model, bodyInfo, x, y, z); 110 } 111 112 @Override 113 public BulletEntity construct (final Matrix4 transform) { 114 if (bodyInfo == null && shape != null) { 115 btCollisionObject obj = new btCollisionObject(); 116 obj.setCollisionShape(shape); 117 return new BulletEntity(model, obj, transform); 118 } else 119 return new BulletEntity(model, bodyInfo, transform); 120 } 121 } 122