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.RevoluteJointDef; 41 42 public class Bridge extends Box2DTest { 43 int e_count = 30; 44 45 @Override createWorld(World world)46 protected void createWorld (World world) { 47 Body ground; 48 { 49 BodyDef bd = new BodyDef(); 50 ground = world.createBody(bd); 51 52 EdgeShape shape = new EdgeShape(); 53 shape.set(new Vector2(-40, 0), new Vector2(40.0f, 0)); 54 55 ground.createFixture(shape, 0); 56 shape.dispose(); 57 } 58 59 { 60 PolygonShape shape = new PolygonShape(); 61 shape.setAsBox(0.5f, 0.125f); 62 FixtureDef fd = new FixtureDef(); 63 fd.shape = shape; 64 fd.density = 20.0f; 65 fd.friction = 0.2f; 66 67 RevoluteJointDef jd = new RevoluteJointDef(); 68 69 Body prevBody = ground; 70 71 for (int i = 0; i < e_count; i++) { 72 BodyDef bd = new BodyDef(); 73 bd.type = BodyType.DynamicBody; 74 bd.position.set(-14.5f + 1.0f * i, 5.0f); 75 Body body = world.createBody(bd); 76 body.createFixture(fd); 77 78 Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f); 79 jd.initialize(prevBody, body, anchor); 80 world.createJoint(jd); 81 prevBody = body; 82 } 83 84 Vector2 anchor = new Vector2(-15.0f + 1.0f * e_count, 5.0f); 85 jd.initialize(prevBody, ground, anchor); 86 world.createJoint(jd); 87 shape.dispose(); 88 } 89 90 for (int i = 0; i < 2; i++) { 91 Vector2[] vertices = new Vector2[3]; 92 vertices[0] = new Vector2(-0.5f, 0); 93 vertices[1] = new Vector2(0.5f, 0); 94 vertices[2] = new Vector2(0, 1.5f); 95 96 PolygonShape shape = new PolygonShape(); 97 shape.set(vertices); 98 99 FixtureDef fd = new FixtureDef(); 100 fd.shape = shape; 101 fd.density = 1.0f; 102 103 BodyDef bd = new BodyDef(); 104 bd.type = BodyType.DynamicBody; 105 bd.position.set(-8.0f + 8.0f * i, 12.0f); 106 Body body = world.createBody(bd); 107 body.createFixture(fd); 108 109 shape.dispose(); 110 } 111 112 for (int i = 0; i < 3; i++) { 113 CircleShape shape = new CircleShape(); 114 shape.setRadius(0.5f); 115 116 FixtureDef fd = new FixtureDef(); 117 fd.shape = shape; 118 fd.density = 1.0f; 119 120 BodyDef bd = new BodyDef(); 121 bd.type = BodyType.DynamicBody; 122 bd.position.set(-6.0f + 6.0f * i, 10.0f); 123 124 Body body = world.createBody(bd); 125 body.createFixture(fd); 126 127 shape.dispose(); 128 } 129 } 130 131 } 132