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.math.Vector2; 32 import com.badlogic.gdx.physics.box2d.Body; 33 import com.badlogic.gdx.physics.box2d.BodyDef; 34 import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; 35 import com.badlogic.gdx.physics.box2d.CircleShape; 36 import com.badlogic.gdx.physics.box2d.EdgeShape; 37 import com.badlogic.gdx.physics.box2d.FixtureDef; 38 import com.badlogic.gdx.physics.box2d.PolygonShape; 39 import com.badlogic.gdx.physics.box2d.World; 40 import com.badlogic.gdx.physics.box2d.joints.WeldJointDef; 41 42 public class Cantilever extends Box2DTest { 43 Body m_middle; 44 final int e_count = 8; 45 46 @Override createWorld(World world)47 protected void createWorld (World world) { 48 Body ground; 49 { 50 BodyDef bd = new BodyDef(); 51 ground = world.createBody(bd); 52 53 EdgeShape shape = new EdgeShape(); 54 shape.set(new Vector2(-40, 0), new Vector2(40, 0)); 55 ground.createFixture(shape, 0); 56 shape.dispose(); 57 } 58 59 { 60 PolygonShape shape = new PolygonShape(); 61 shape.setAsBox(0.5f, 0.125f); 62 63 FixtureDef fd = new FixtureDef(); 64 fd.shape = shape; 65 fd.density = 20.0f; 66 67 WeldJointDef jd = new WeldJointDef(); 68 69 Body prevBody = ground; 70 for (int i = 0; i < e_count; i++) { 71 BodyDef bd = new BodyDef(); 72 bd.type = BodyType.DynamicBody; 73 bd.position.set(-14.5f + 1.0f * i, 5.0f); 74 Body body = world.createBody(bd); 75 body.createFixture(fd); 76 77 Vector2 anchor = new Vector2(-15.0f + 1 * i, 5.0f); 78 jd.initialize(prevBody, body, anchor); 79 world.createJoint(jd); 80 prevBody = body; 81 } 82 83 shape.dispose(); 84 } 85 86 { 87 PolygonShape shape = new PolygonShape(); 88 shape.setAsBox(0.5f, 0.125f); 89 90 FixtureDef fd = new FixtureDef(); 91 fd.shape = shape; 92 fd.density = 20.0f; 93 94 WeldJointDef jd = new WeldJointDef(); 95 96 Body prevBody = ground; 97 for (int i = 0; i < e_count; i++) { 98 BodyDef bd = new BodyDef(); 99 bd.type = BodyType.DynamicBody; 100 bd.position.set(-14.5f + 1.0f * i, 15.0f); 101 bd.gravityScale = 10.0f; 102 Body body = world.createBody(bd); 103 body.createFixture(fd); 104 105 Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 15.0f); 106 jd.initialize(prevBody, body, anchor); 107 world.createJoint(jd); 108 109 prevBody = body; 110 } 111 112 shape.dispose(); 113 } 114 115 { 116 PolygonShape shape = new PolygonShape(); 117 shape.setAsBox(0.5f, 0.125f); 118 119 FixtureDef fd = new FixtureDef(); 120 fd.shape = shape; 121 fd.density = 20.0f; 122 123 WeldJointDef jd = new WeldJointDef(); 124 125 Body prevBody = ground; 126 for (int i = 0; i < e_count; i++) { 127 BodyDef bd = new BodyDef(); 128 bd.type = BodyType.DynamicBody; 129 bd.position.set(-4.5f + 1.0f * i, 5.0f); 130 Body body = world.createBody(bd); 131 body.createFixture(fd); 132 133 if (i > 0) { 134 Vector2 anchor = new Vector2(-5.0f + 1.0f * i, 5.0f); 135 jd.initialize(prevBody, body, anchor); 136 world.createJoint(jd); 137 } 138 139 prevBody = body; 140 } 141 142 shape.dispose(); 143 } 144 145 { 146 PolygonShape shape = new PolygonShape(); 147 shape.setAsBox(0.5f, 0.125f); 148 149 FixtureDef fd = new FixtureDef(); 150 fd.shape = shape; 151 fd.density = 20.0f; 152 153 WeldJointDef jd = new WeldJointDef(); 154 155 Body prevBody = ground; 156 for (int i = 0; i < e_count; i++) { 157 BodyDef bd = new BodyDef(); 158 bd.type = BodyType.DynamicBody; 159 bd.position.set(5.5f + 1.0f * i, 10.0f); 160 bd.gravityScale = 10.0f; 161 Body body = world.createBody(bd); 162 body.createFixture(fd); 163 164 if (i > 0) { 165 Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f); 166 jd.initialize(prevBody, body, anchor); 167 world.createJoint(jd); 168 } 169 170 prevBody = body; 171 } 172 173 shape.dispose(); 174 } 175 176 for (int i = 0; i < 2; i++) { 177 Vector2[] vertices = new Vector2[3]; 178 vertices[0] = new Vector2(-0.5f, 0); 179 vertices[1] = new Vector2(0.5f, 0); 180 vertices[2] = new Vector2(0, 1.5f); 181 182 PolygonShape shape = new PolygonShape(); 183 shape.set(vertices); 184 185 FixtureDef fd = new FixtureDef(); 186 fd.shape = shape; 187 fd.density = 1.0f; 188 189 BodyDef bd = new BodyDef(); 190 bd.type = BodyType.DynamicBody; 191 bd.position.set(-8.0f + 8.0f * i, 12.0f); 192 Body body = world.createBody(bd); 193 body.createFixture(fd); 194 195 shape.dispose(); 196 } 197 198 for (int i = 0; i < 2; i++) { 199 CircleShape shape = new CircleShape(); 200 shape.setRadius(0.5f); 201 202 FixtureDef fd = new FixtureDef(); 203 fd.shape = shape; 204 fd.density = 1.0f; 205 206 BodyDef bd = new BodyDef(); 207 bd.type = BodyType.DynamicBody; 208 bd.position.set(-6.0f + 6.0f * i, 10.0f); 209 Body body = world.createBody(bd); 210 body.createFixture(fd); 211 shape.dispose(); 212 } 213 } 214 215 } 216