• 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.graphics.Color;
20 import com.badlogic.gdx.graphics.VertexAttributes.Usage;
21 import com.badlogic.gdx.graphics.g3d.Material;
22 import com.badlogic.gdx.graphics.g3d.Model;
23 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
24 import com.badlogic.gdx.math.Vector3;
25 import com.badlogic.gdx.physics.bullet.dynamics.btDynamicsWorld;
26 import com.badlogic.gdx.physics.bullet.dynamics.btPoint2PointConstraint;
27 import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody;
28 import com.badlogic.gdx.physics.bullet.dynamics.btTypedConstraint;
29 import com.badlogic.gdx.utils.Array;
30 
31 /** @author xoppa */
32 public class ConstraintsTest extends BaseBulletTest {
33 
34 	final Array<btTypedConstraint> constraints = new Array<btTypedConstraint>();
35 
36 	@Override
create()37 	public void create () {
38 		super.create();
39 
40 		final Model barModel = modelBuilder.createBox(10f, 1f, 1f, new Material(new ColorAttribute(ColorAttribute.Diffuse,
41 			Color.WHITE)), Usage.Position | Usage.Normal);
42 		disposables.add(barModel);
43 		world.addConstructor("bar", new BulletConstructor(barModel, 0f)); // mass = 0: static body
44 
45 		// Create the entities
46 		world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(),
47 			0.25f + 0.5f * (float)Math.random(), 1f);
48 
49 		BulletEntity bar = world.add("bar", 0f, 7f, 0f);
50 		bar.setColor(0.75f + 0.25f * (float)Math.random(), 0.75f + 0.25f * (float)Math.random(),
51 			0.75f + 0.25f * (float)Math.random(), 1f);
52 
53 		BulletEntity box1 = world.add("box", -4.5f, 6f, 0f);
54 		box1.setColor(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(),
55 			1f);
56 		btPoint2PointConstraint constraint = new btPoint2PointConstraint((btRigidBody)bar.body, (btRigidBody)box1.body,
57 			tmpV1.set(-5, -0.5f, -0.5f), tmpV2.set(-0.5f, 0.5f, -0.5f));
58 		((btDynamicsWorld)world.collisionWorld).addConstraint(constraint, false);
59 		constraints.add(constraint);
60 		BulletEntity box2 = null;
61 		for (int i = 0; i < 10; i++) {
62 			if (i % 2 == 0) {
63 				box2 = world.add("box", -3.5f + (float)i, 6f, 0f);
64 				box2.setColor(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(),
65 					0.5f + 0.5f * (float)Math.random(), 1f);
66 				constraint = new btPoint2PointConstraint((btRigidBody)box1.body, (btRigidBody)box2.body, tmpV1.set(0.5f, -0.5f,
67 					0.5f), tmpV2.set(-0.5f, -0.5f, 0.5f));
68 			} else {
69 				box1 = world.add("box", -3.5f + (float)i, 6f, 0f);
70 				box1.setColor(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(),
71 					0.5f + 0.5f * (float)Math.random(), 1f);
72 				constraint = new btPoint2PointConstraint((btRigidBody)box2.body, (btRigidBody)box1.body, tmpV1.set(0.5f, 0.5f,
73 					-0.5f), tmpV2.set(-0.5f, 0.5f, -0.5f));
74 			}
75 			((btDynamicsWorld)world.collisionWorld).addConstraint(constraint, false);
76 			constraints.add(constraint);
77 		}
78 		constraint = new btPoint2PointConstraint((btRigidBody)bar.body, (btRigidBody)box1.body, tmpV1.set(5f, -0.5f, -0.5f),
79 			tmpV2.set(0.5f, 0.5f, -0.5f));
80 		((btDynamicsWorld)world.collisionWorld).addConstraint(constraint, false);
81 		constraints.add(constraint);
82 	}
83 
84 	@Override
dispose()85 	public void dispose () {
86 		for (int i = 0; i < constraints.size; i++) {
87 			((btDynamicsWorld)world.collisionWorld).removeConstraint(constraints.get(i));
88 			constraints.get(i).dispose();
89 		}
90 		constraints.clear();
91 		super.dispose();
92 	}
93 
94 	@Override
tap(float x, float y, int count, int button)95 	public boolean tap (float x, float y, int count, int button) {
96 		shoot(x, y);
97 		return true;
98 	}
99 }
100