• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.g3d.Model;
23 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
24 import com.badlogic.gdx.physics.bullet.collision.btConvexHullShape;
25 import com.badlogic.gdx.physics.bullet.collision.btShapeHull;
26 
27 /** @author xoppa */
28 public class ConvexHullTest extends BaseBulletTest {
29 
30 	@Override
create()31 	public void create () {
32 		super.create();
33 
34 		final Model carModel = objLoader.loadModel(Gdx.files.internal("data/car.obj"));
35 		disposables.add(carModel);
36 		carModel.materials.get(0).clear();
37 		carModel.materials.get(0).set(ColorAttribute.createDiffuse(Color.WHITE), ColorAttribute.createSpecular(Color.WHITE));
38 		world.addConstructor("car", new BulletConstructor(carModel, 5f, createConvexHullShape(carModel, true)));
39 
40 		// Create the entities
41 		world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(),
42 			0.25f + 0.5f * (float)Math.random(), 1f);
43 
44 		for (float y = 10f; y < 50f; y += 5f)
45 			world.add("car", -2f + (float)Math.random() * 4f, y, -2f + (float)Math.random() * 4f).setColor(
46 				0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(), 1f);
47 	}
48 
49 	@Override
tap(float x, float y, int count, int button)50 	public boolean tap (float x, float y, int count, int button) {
51 		shoot(x, y);
52 		return true;
53 	}
54 
createConvexHullShape(final Model model, boolean optimize)55 	public static btConvexHullShape createConvexHullShape (final Model model, boolean optimize) {
56 		final Mesh mesh = model.meshes.get(0);
57 		final btConvexHullShape shape = new btConvexHullShape(mesh.getVerticesBuffer(), mesh.getNumVertices(), mesh.getVertexSize());
58 		if (!optimize) return shape;
59 		// now optimize the shape
60 		final btShapeHull hull = new btShapeHull(shape);
61 		hull.buildHull(shape.getMargin());
62 		final btConvexHullShape result = new btConvexHullShape(hull);
63 		// delete the temporary shape
64 		shape.dispose();
65 		hull.dispose();
66 		return result;
67 	}
68 }
69