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 41 public class CharacterCollision extends Box2DTest { 42 43 @Override createWorld(World world)44 protected void createWorld (World world) { 45 { 46 BodyDef bd = new BodyDef(); 47 Body ground = world.createBody(bd); 48 49 EdgeShape shape = new EdgeShape(); 50 shape.set(new Vector2(-20, 0), new Vector2(20, 0)); 51 ground.createFixture(shape, 0); 52 shape.dispose(); 53 } 54 55 { 56 BodyDef bd = new BodyDef(); 57 Body ground = world.createBody(bd); 58 59 EdgeShape shape = new EdgeShape(); 60 shape.setRadius(0); 61 shape.set(new Vector2(-8, 1), new Vector2(-6, 1)); 62 ground.createFixture(shape, 0); 63 shape.set(new Vector2(-6, 1), new Vector2(-4, 1)); 64 ground.createFixture(shape, 0); 65 shape.set(new Vector2(-4, 1), new Vector2(-2, 1)); 66 ground.createFixture(shape, 0); 67 shape.dispose(); 68 } 69 70 { 71 BodyDef bd = new BodyDef(); 72 Body ground = world.createBody(bd); 73 74 PolygonShape shape = new PolygonShape(); 75 shape.setAsBox(1, 1, new Vector2(4, 3), 0); 76 ground.createFixture(shape, 0); 77 shape.setAsBox(1, 1, new Vector2(6, 3), 0); 78 ground.createFixture(shape, 0); 79 shape.setAsBox(1, 1, new Vector2(8, 3), 0); 80 ground.createFixture(shape, 0); 81 shape.dispose(); 82 } 83 84 { 85 BodyDef bd = new BodyDef(); 86 Body ground = world.createBody(bd); 87 88 EdgeShape shape = new EdgeShape(); 89 float d = 2 * 2 * 0.005f; 90 shape.setRadius(0); 91 shape.set(new Vector2(-1 + d, 3), new Vector2(1 - d, 3)); 92 ground.createFixture(shape, 0); 93 shape.set(new Vector2(1, 3 + d), new Vector2(1, 5 - d)); 94 ground.createFixture(shape, 0); 95 shape.set(new Vector2(1 - d, 5), new Vector2(-1 + d, 5)); 96 ground.createFixture(shape, 0); 97 shape.set(new Vector2(-1, 5 - d), new Vector2(-1, 3 + d)); 98 ground.createFixture(shape, 0); 99 shape.dispose(); 100 } 101 102 { 103 BodyDef bd = new BodyDef(); 104 bd.position.set(-3, 5); 105 bd.type = BodyType.DynamicBody; 106 bd.fixedRotation = true; 107 bd.allowSleep = false; 108 109 Body body = world.createBody(bd); 110 111 PolygonShape shape = new PolygonShape(); 112 shape.setAsBox(0.5f, 0.5f); 113 114 FixtureDef fd = new FixtureDef(); 115 fd.shape = shape; 116 fd.density = 20.0f; 117 body.createFixture(fd); 118 shape.dispose(); 119 } 120 121 { 122 BodyDef bd = new BodyDef(); 123 bd.position.set(-5, 5); 124 bd.type = BodyType.DynamicBody; 125 bd.fixedRotation = true; 126 bd.allowSleep = false; 127 128 Body body = world.createBody(bd); 129 130 float angle = 0; 131 float delta = (float)Math.PI / 3; 132 Vector2[] vertices = new Vector2[6]; 133 for (int i = 0; i < 6; i++) { 134 vertices[i] = new Vector2(0.5f * (float)Math.cos(angle), 0.5f * (float)Math.sin(angle)); 135 angle += delta; 136 } 137 138 PolygonShape shape = new PolygonShape(); 139 shape.set(vertices); 140 141 FixtureDef fd = new FixtureDef(); 142 fd.shape = shape; 143 fd.density = 20.0f; 144 body.createFixture(fd); 145 shape.dispose(); 146 } 147 148 { 149 BodyDef bd = new BodyDef(); 150 bd.position.set(3, 5); 151 bd.type = BodyType.DynamicBody; 152 bd.fixedRotation = true; 153 bd.allowSleep = false; 154 155 Body body = world.createBody(bd); 156 157 CircleShape shape = new CircleShape(); 158 shape.setRadius(0.5f); 159 160 FixtureDef fd = new FixtureDef(); 161 fd.shape = shape; 162 fd.density = 20.0f; 163 body.createFixture(fd); 164 shape.dispose(); 165 } 166 } 167 } 168