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.box2d; 18 19 import com.badlogic.gdx.math.Vector2; 20 import com.badlogic.gdx.physics.box2d.Body; 21 import com.badlogic.gdx.physics.box2d.BodyDef; 22 import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; 23 import com.badlogic.gdx.physics.box2d.Contact; 24 import com.badlogic.gdx.physics.box2d.ContactImpulse; 25 import com.badlogic.gdx.physics.box2d.ContactListener; 26 import com.badlogic.gdx.physics.box2d.EdgeShape; 27 import com.badlogic.gdx.physics.box2d.Fixture; 28 import com.badlogic.gdx.physics.box2d.FixtureDef; 29 import com.badlogic.gdx.physics.box2d.Manifold; 30 import com.badlogic.gdx.physics.box2d.PolygonShape; 31 import com.badlogic.gdx.physics.box2d.World; 32 33 public class ConveyorBelt extends Box2DTest implements ContactListener { 34 Fixture m_platform; 35 36 @Override createWorld(World world)37 protected void createWorld (World world) { 38 world.setContactListener(this); 39 40 // Ground 41 { 42 BodyDef bodyDef = new BodyDef(); 43 groundBody = world.createBody(bodyDef); 44 45 EdgeShape shape = new EdgeShape(); 46 shape.set(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f)); 47 groundBody.createFixture(shape, 0.0f); 48 } 49 50 // Platform 51 { 52 BodyDef bd = new BodyDef(); 53 bd.position.set(-5.0f, 5.0f); 54 Body body = world.createBody(bd); 55 56 PolygonShape shape = new PolygonShape(); 57 shape.setAsBox(10.0f, 0.5f); 58 59 FixtureDef fd = new FixtureDef(); 60 fd.shape = shape; 61 fd.friction = 0.8f; 62 m_platform = body.createFixture(fd); 63 } 64 65 // Boxes 66 for (int i = 0; i < 5; ++i) { 67 BodyDef bd = new BodyDef(); 68 bd.type = BodyType.DynamicBody; 69 bd.position.set(-10.0f + 2.0f * i, 7.0f); 70 Body body = world.createBody(bd); 71 72 PolygonShape shape = new PolygonShape(); 73 shape.setAsBox(0.5f, 0.5f); 74 body.createFixture(shape, 20.0f); 75 } 76 } 77 preSolve(Contact contact, Manifold oldManifold)78 public void preSolve (Contact contact, Manifold oldManifold) { 79 Fixture fixtureA = contact.getFixtureA(); 80 Fixture fixtureB = contact.getFixtureB(); 81 82 if (fixtureA == m_platform || fixtureB == m_platform) { 83 contact.setTangentSpeed(5.0f); 84 } 85 } 86 87 @Override beginContact(Contact contact)88 public void beginContact (Contact contact) { 89 90 } 91 92 @Override endContact(Contact contact)93 public void endContact (Contact contact) { 94 } 95 96 @Override postSolve(Contact contact, ContactImpulse impulse)97 public void postSolve (Contact contact, ContactImpulse impulse) { 98 } 99 } 100