• 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.math.Vector3;
21 import com.badlogic.gdx.physics.bullet.collision.btCollisionObjectArray;
22 import com.badlogic.gdx.physics.bullet.dynamics.InternalTickCallback;
23 import com.badlogic.gdx.physics.bullet.dynamics.btDynamicsWorld;
24 import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody;
25 
26 /** @author xoppa */
27 public class InternalTickTest extends BaseBulletTest {
28 	static class TestInternalTickCallback extends InternalTickCallback {
TestInternalTickCallback(btDynamicsWorld dynamicsWorld)29 		public TestInternalTickCallback (btDynamicsWorld dynamicsWorld) {
30 			super(dynamicsWorld, true);
31 		}
32 
33 		@Override
onInternalTick(btDynamicsWorld dynamicsWorld, float timeStep)34 		public void onInternalTick (btDynamicsWorld dynamicsWorld, float timeStep) {
35 			btCollisionObjectArray objs = dynamicsWorld.getCollisionObjectArray();
36 			dynamicsWorld.clearForces();
37 			int idx = 0;
38 			for (int i = 0; i < objs.size(); i++) {
39 				btRigidBody body = (btRigidBody)(objs.at(i));
40 				if (body == null || body.isStaticOrKinematicObject()) continue;
41 				body.applyGravity();
42 				body.applyCentralForce(tmpV1.set(0f, 8.0f + (float)(6.0 * Math.random()), 0f));
43 				idx++;
44 			}
45 		}
46 	}
47 
48 	final int BOXCOUNT_X = 5;
49 	final int BOXCOUNT_Y = 5;
50 	final int BOXCOUNT_Z = 1;
51 
52 	final float BOXOFFSET_X = -2.5f;
53 	final float BOXOFFSET_Y = 0.5f;
54 	final float BOXOFFSET_Z = 0f;
55 
56 	TestInternalTickCallback internalTickCallback;
57 
58 	@Override
create()59 	public void create () {
60 		super.create();
61 
62 		internalTickCallback = new TestInternalTickCallback((btDynamicsWorld)world.collisionWorld);
63 
64 		// Create the entities
65 		world.add("ground", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(),
66 			0.25f + 0.5f * (float)Math.random(), 1f);
67 
68 		for (int x = 0; x < BOXCOUNT_X; x++) {
69 			for (int y = 0; y < BOXCOUNT_Y; y++) {
70 				for (int z = 0; z < BOXCOUNT_Z; z++) {
71 					world.add("box", BOXOFFSET_X + x, BOXOFFSET_Y + y, BOXOFFSET_Z + z).setColor(0.5f + 0.5f * (float)Math.random(),
72 						0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 1f);
73 				}
74 			}
75 		}
76 	}
77 
78 	@Override
dispose()79 	public void dispose () {
80 		super.dispose();
81 
82 		if (internalTickCallback != null) internalTickCallback.dispose();
83 		internalTickCallback = null;
84 	}
85 
86 	float toggleTime = 0f;
87 	boolean toggleAttach = false;
88 
89 	@Override
render()90 	public void render () {
91 		super.render();
92 		if (internalTickCallback == null) return;
93 		if ((toggleTime += Gdx.graphics.getDeltaTime()) > 1.0f) {
94 			toggleTime -= 1.0f;
95 			if (toggleAttach)
96 				internalTickCallback.detach();
97 			else
98 				internalTickCallback.attach();
99 			toggleAttach = !toggleAttach;
100 		}
101 	}
102 
103 	@Override
tap(float x, float y, int count, int button)104 	public boolean tap (float x, float y, int count, int button) {
105 		shoot(x, y);
106 		return true;
107 	}
108 }
109