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 * Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com) 18 * 19 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the 20 * License. You may obtain a copy of the License at 21 * 22 * http://www.apache.org/licenses/LICENSE-2.0 23 * 24 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" 25 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language 26 * governing permissions and limitations under the License. 27 */ 28 29 package com.badlogic.gdx.tests.box2d; 30 31 import com.badlogic.gdx.Input; 32 import com.badlogic.gdx.math.Vector2; 33 import com.badlogic.gdx.physics.box2d.Body; 34 import com.badlogic.gdx.physics.box2d.BodyDef; 35 import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; 36 import com.badlogic.gdx.physics.box2d.CircleShape; 37 import com.badlogic.gdx.physics.box2d.EdgeShape; 38 import com.badlogic.gdx.physics.box2d.FixtureDef; 39 import com.badlogic.gdx.physics.box2d.PolygonShape; 40 import com.badlogic.gdx.physics.box2d.World; 41 42 public class VerticalStack extends Box2DTest { 43 static final int e_columnCount = 5; 44 static final int e_rowCount = 16; 45 46 Body m_bullet; 47 Body[] m_bodies = new Body[e_rowCount * e_columnCount]; 48 int[] m_indices = new int[e_rowCount * e_columnCount]; 49 50 @Override createWorld(World world)51 protected void createWorld (World world) { 52 { 53 BodyDef bd = new BodyDef(); 54 Body ground = world.createBody(bd); 55 56 EdgeShape shape = new EdgeShape(); 57 shape.set(new Vector2(-40, 0), new Vector2(40, 0)); 58 ground.createFixture(shape, 0.0f); 59 60 shape.set(new Vector2(20, 0), new Vector2(20, 20)); 61 ground.createFixture(shape, 0); 62 shape.dispose(); 63 } 64 65 float xs[] = {0, -10, -5, 5, 10}; 66 67 for (int j = 0; j < e_columnCount; j++) { 68 PolygonShape shape = new PolygonShape(); 69 shape.setAsBox(0.5f, 0.5f); 70 71 FixtureDef fd = new FixtureDef(); 72 fd.shape = shape; 73 fd.density = 1.0f; 74 fd.friction = 0.3f; 75 76 for (int i = 0; i < e_rowCount; i++) { 77 BodyDef bd = new BodyDef(); 78 bd.type = BodyType.DynamicBody; 79 80 int n = j * e_rowCount + i; 81 m_indices[n] = n; 82 83 float x = 0; 84 bd.position.set(xs[j] + x, 0.752f + 1.54f * i); 85 Body body = world.createBody(bd); 86 body.setUserData(n); 87 88 m_bodies[n] = body; 89 body.createFixture(fd); 90 } 91 92 shape.dispose(); 93 } 94 95 m_bullet = null; 96 } 97 98 @Override keyDown(int keyCode)99 public boolean keyDown (int keyCode) { 100 if (keyCode == Input.Keys.COMMA) { 101 if (m_bullet != null) { 102 world.destroyBody(m_bullet); 103 m_bullet = null; 104 } 105 106 { 107 CircleShape shape = new CircleShape(); 108 shape.setRadius(0.25f); 109 110 FixtureDef fd = new FixtureDef(); 111 fd.shape = shape; 112 fd.density = 20.0f; 113 fd.restitution = 0.05f; 114 115 BodyDef bd = new BodyDef(); 116 bd.type = BodyType.DynamicBody; 117 bd.bullet = true; 118 bd.position.set(-31, 5); 119 120 m_bullet = world.createBody(bd); 121 m_bullet.createFixture(fd); 122 123 m_bullet.setLinearVelocity(new Vector2(400, 0)); 124 } 125 } 126 127 return false; 128 } 129 render()130 public void render () { 131 super.render(); 132 133 // if (renderer.batch != null) { 134 // renderer.batch.begin(); 135 // // renderer.batch.drawText(renderer.font, "Press: (,) to launch a bullet", 0, Gdx.app.getGraphics().getHeight(), 136 // // Color.WHITE); 137 // renderer.batch.end(); 138 // } 139 } 140 } 141