com.badlogic.gdx.physics.box2d.Shape Java Examples
The following examples show how to use
com.badlogic.gdx.physics.box2d.Shape.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpawnBodiesSample.java From Codelabs with MIT License | 6 votes |
@Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { /* Checks whether the max amount of balls were spawned */ if (spawnedBalls < MAX_SPAWNED_BALLS) { spawnedBalls++; /* Translate camera point to world point */ Vector3 unprojectedVector = new Vector3(); camera.unproject(unprojectedVector.set(screenX, screenY, 0)); /* Create a new ball */ Shape shape = Box2DFactory.createCircleShape(1); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f, 0.75f, false); Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(unprojectedVector.x, unprojectedVector.y)); } return true; }
Example #2
Source File: GravityAccelerometerSample.java From Codelabs with MIT License | 6 votes |
@Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { /* Checks whether the max amount of balls were spawned */ if (spawnedBalls < MAX_SPAWNED_BALLS) { spawnedBalls++; /* Translate camera point to world point */ Vector3 unprojectedVector = new Vector3(); camera.unproject(unprojectedVector.set(screenX, screenY, 0)); /* Create a new ball */ Shape shape = Box2DFactory.createCircleShape(1); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f, 0.75f, false); Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(unprojectedVector.x, unprojectedVector.y)); } return true; }
Example #3
Source File: Box2DFactory.java From Codelabs with MIT License | 6 votes |
public static Body createWalls(World world, float viewportWidth, float viewportHeight, float offset) { float halfWidth = viewportWidth / 2 - offset; float halfHeight = viewportHeight / 2 - offset; Vector2[] vertices = new Vector2[] { new Vector2(-halfWidth, -halfHeight), new Vector2(halfWidth, -halfHeight), new Vector2(halfWidth, halfHeight), new Vector2(-halfWidth, halfHeight), new Vector2(-halfWidth, -halfHeight) }; Shape shape = createChainShape(vertices); FixtureDef fixtureDef = createFixture(shape, 1, 0.5f, 0, false); return createBody(world, BodyType.StaticBody, fixtureDef, new Vector2( 0, 0)); }
Example #4
Source File: Box2DFactory.java From Codelabs with MIT License | 5 votes |
public static Shape createTriangleShape(float halfWidth, float halfHeight) { PolygonShape triangleShape = new PolygonShape(); triangleShape .set(new Vector2[] { new Vector2(-halfWidth, -halfHeight), new Vector2(0, halfHeight), new Vector2(halfWidth, -halfHeight) }); return triangleShape; }
Example #5
Source File: Box2DFactory.java From Codelabs with MIT License | 5 votes |
public static FixtureDef createFixture(Shape shape, float density, float friction, float restitution, boolean isSensor) { FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.isSensor = isSensor; fixtureDef.density = density; fixtureDef.friction = friction; fixtureDef.restitution = restitution; return fixtureDef; }
Example #6
Source File: Box2DFactory.java From Codelabs with MIT License | 4 votes |
public static Shape createPolygonShape(Vector2[] vertices) { PolygonShape polygonShape = new PolygonShape(); polygonShape.set(vertices); return polygonShape; }
Example #7
Source File: Piece.java From fluid-simulator-v2 with Apache License 2.0 | 4 votes |
public void addShape(Shape newShape) { if (shapes == null) shapes = new ArrayList<Shape>(); shapes.add(newShape); }
Example #8
Source File: BouncingBallSample.java From Codelabs with MIT License | 4 votes |
@Override public void show() { /* * This line is found in every sample but is not necessary for the sample * functionality. calls Sample.show() method. That method set the sample to * receive all touch and key input events. Also prevents the app from be * closed whenever the user press back button and instead returns to * main menu. */ super.show(); /* * Create world with a common gravity vector (9.81 m/s2 downwards force) * and tell world that we want objects to sleep. This last value * conserves CPU usage. */ world = new World(new Vector2(0, -9.81f), true); /* Create renderer */ debugRenderer = new Box2DDebugRenderer(); /* * Define camera viewport. Box2D uses meters internally so the camera * must be defined also in meters. We set a desired width and adjust * height to different resolutions. */ camera = new OrthographicCamera(20, 20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics .getWidth())); /* Create the ball */ Shape shape = Box2DFactory.createCircleShape(0.5f); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f, 0.75f, false); Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(6, 5)); /* Create the ramp */ Vector2[] rampVertices = new Vector2[] { new Vector2(-2.5f, -1), new Vector2(2.5f, 1) }; shape = Box2DFactory.createChainShape(rampVertices); fixtureDef = Box2DFactory.createFixture(shape, 0, 0.3f, 0.3f, false); Box2DFactory.createBody(world, BodyType.StaticBody, fixtureDef, new Vector2(6.5f, 0)); /* Create the walls */ Box2DFactory.createWalls(world, camera.viewportWidth, camera.viewportHeight, 1); }
Example #9
Source File: BuoyancySample.java From Codelabs with MIT License | 4 votes |
@Override public void show() { /* * This line is found in every sample but is not necessary for the * sample functionality. calls Sample.show() method. That method set the * sample to receive all touch and key input events. Also prevents the * app from be closed whenever the user press back button and instead * returns to main menu. */ super.show(); /* * Create world with a common gravity vector (9.81 m/s2 downwards force) * and tell world that we want objects to sleep. This last value * conserves CPU usage. */ world = new World(new Vector2(0, -9.81f), true); /* Create renderer */ debugRenderer = new Box2DDebugRenderer(); /* * Define camera viewport. Box2D uses meters internally so the camera * must be defined also in meters. We set a desired width and adjust * height to different resolutions. */ camera = new OrthographicCamera(20, 20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics .getWidth())); /* * Next line must remain commented because we do this in its parent (See * Sample class). In case you are not using Sample class, uncomment this * line to set input processor to handle events. */ // Gdx.input.setInputProcessor(this); Box2DFactory.createWalls(world, camera.viewportWidth, camera.viewportHeight, 1); /* Create the water */ Shape shape = Box2DFactory.createBoxShape( (camera.viewportWidth / 2 - 1), (camera.viewportHeight / 2 - 2) / 2, new Vector2(0, 0), 0); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 1, 0.1f, 0, true); Body water = Box2DFactory.createBody(world, BodyType.StaticBody, fixtureDef, new Vector2(0, -(camera.viewportHeight / 2 - 1) / 2 - 0.5f)); /* * Create a buoyancy controller using the previous body as a fluid * sensor */ buoyancyController = new BuoyancyController(world, water .getFixtureList().first()); world.setContactListener(this); }
Example #10
Source File: CollisionsSample.java From Codelabs with MIT License | 4 votes |
@Override public void show() { /* * This line is found in every sample but is not necessary for the * sample functionality. calls Sample.show() method. That method set the * sample to receive all touch and key input events. Also prevents the * app from be closed whenever the user press back button and instead * returns to main menu. */ super.show(); /* * Create world with a common gravity vector (9.81 m/s2 downwards force) * and tell world that we want objects to sleep. This last value * conserves CPU usage. As we use the accelerometer and the world * gravity to change bodies positions, we can't let bodies to sleep. */ world = new World(new Vector2(0, -9.81f), false); /* Create renderer */ debugRenderer = new Box2DDebugRenderer(); /* * Define camera viewport. Box2D uses meters internally so the camera * must be defined also in meters. We set a desired width and adjust * height to different resolutions. */ camera = new OrthographicCamera(20, 20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics .getWidth())); /* Create the ball */ Shape shape = Box2DFactory.createCircleShape(1); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f, 0.75f, false); Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(5, 0)); /* Create the box */ shape = Box2DFactory.createBoxShape(0.5f, 0.5f, new Vector2(0, 0), 0); fixtureDef = Box2DFactory.createFixture(shape, 1, 0.5f, 0.5f, false); Box2DFactory.createBody(world, BodyType.StaticBody, fixtureDef, new Vector2(0, 0)); /* Create the walls */ walls = Box2DFactory.createWalls(world, camera.viewportWidth, camera.viewportHeight, 1); world.setContactListener(this); }
Example #11
Source File: SpritesSample.java From Codelabs with MIT License | 4 votes |
@Override public void show() { /* * This line is found in every sample but is not necessary for the * sample functionality. calls Sample.show() method. That method set the * sample to receive all touch and key input events. Also prevents the * app from be closed whenever the user press back button and instead * returns to main menu. */ super.show(); /* * Create world with a common gravity vector (9.81 m/s2 downwards force) * and tell world that we want objects to sleep. This last value * conserves CPU usage. */ world = new World(new Vector2(0, -9.81f), true); /* Create renderer */ debugRenderer = new Box2DDebugRenderer(); batch = new SpriteBatch(); /* * Define camera viewport. Box2D uses meters internally so the camera * must be defined also in meters. We set a desired width and adjust * height to different resolutions. */ camera = new OrthographicCamera(20, 20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics .getWidth())); /* * Next line must remain commented because we do this in its parent (See * Sample class). In case you are not using Sample class, uncomment this * line to set input processor to handle events. */ // Gdx.input.setInputProcessor(this); /* Create the box */ Shape shape = Box2DFactory.createBoxShape(1.5f, 1.5f, new Vector2(0, 0), 0); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 0.3f, 0.5f, 0.5f, false); box = Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(0, 0)); /* Create the walls */ Box2DFactory.createWalls(world, camera.viewportWidth, camera.viewportHeight, 1); /* Set box texture */ sprite = new Sprite(new Texture("data/images/crab.png")); /* * Set the size of the sprite. We have to remember that we are not * working in pixels, but with meters. The size of the sprite will be * the same as the size of the box; 2 meter wide, 2 meters tall. */ sprite.setSize(3, 3); /* * Sets the origin in relation to the sprite's position for scaling and * rotation. */ sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2); /* Set sprite as a user data of the body to draw it in each render step */ box.setUserData(sprite); /* Instantiate the array of bodies that will be used during render step */ worldBodies = new Array<Body>(); }
Example #12
Source File: ImpulsesSample.java From Codelabs with MIT License | 4 votes |
@Override public void show() { /* * This line is found in every sample but is not necessary for the * sample functionality. calls Sample.show() method. That method set the * sample to receive all touch and key input events. Also prevents the * app from be closed whenever the user press back button and instead * returns to main menu. */ super.show(); /* * Create world with a common gravity vector (9.81 m/s2 downwards force) * and tell world that we want objects to sleep. This last value * conserves CPU usage. */ world = new World(new Vector2(0, -9.81f), true); /* Create renderer */ debugRenderer = new Box2DDebugRenderer(); /* * Define camera viewport. Box2D uses meters internally so the camera * must be defined also in meters. We set a desired width and adjust * height to different resolutions. */ camera = new OrthographicCamera(20, 20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics .getWidth())); /* * Next line must remain commented because we do this in its parent (See * Sample class). In case you are not using Sample class, uncomment this * line to set input processor to handle events. */ // Gdx.input.setInputProcessor(this); /* Create the box */ Shape shape = Box2DFactory.createBoxShape(1.5f, 1.5f, new Vector2(0, 0), 0); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 0.3f, 0.5f, 0.5f, false); box = Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(0, 0)); /* Create the walls */ Box2DFactory.createWalls(world, camera.viewportWidth, camera.viewportHeight, 1); }
Example #13
Source File: Box2DFactory.java From Codelabs with MIT License | 4 votes |
public static Shape createCircleShape(float radius) { CircleShape circleShape = new CircleShape(); circleShape.setRadius(radius); return circleShape; }
Example #14
Source File: Box2DFactory.java From Codelabs with MIT License | 4 votes |
public static Shape createChainShape(Vector2[] vertices) { ChainShape chainShape = new ChainShape(); chainShape.createChain(vertices); return chainShape; }
Example #15
Source File: Box2DFactory.java From Codelabs with MIT License | 4 votes |
public static Shape createBoxShape(float halfWidth, float halfHeight, Vector2 center, float angle) { PolygonShape boxShape = new PolygonShape(); boxShape.setAsBox(halfWidth, halfHeight, center, angle); return boxShape; }
Example #16
Source File: DragAndDropSample.java From Codelabs with MIT License | 4 votes |
@Override public void show() { /* * This line is found in every sample but is not necessary for the * sample functionality. calls Sample.show() method. That method set the * sample to receive all touch and key input events. Also prevents the * app from be closed whenever the user press back button and instead * returns to main menu. */ super.show(); /* * Create world with a common gravity vector (9.81 m/s2 downwards force) * and tell world that we want objects to sleep. This last value * conserves CPU usage. */ world = new World(new Vector2(0, -9.81f), true); /* Create renderer */ debugRenderer = new Box2DDebugRenderer(); /* * Define camera viewport. Box2D uses meters internally so the camera * must be defined also in meters. We set a desired width and adjust * height to different resolutions. */ camera = new OrthographicCamera(20, 20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics .getWidth())); /* * Next line must remain commented because we do this in its parent (See * Sample class). In case you are not using Sample class, uncomment this * line to set input processor to handle events. */ // Gdx.input.setInputProcessor(this); /* * Instantiate the vector that will be used to store click/touch * positions. */ touchPosition = new Vector3(); /* Create the ball */ Shape shape = Box2DFactory.createCircleShape(1); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f, 0.75f, false); Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(0, 0)); /* Create the walls */ Body walls = Box2DFactory.createWalls(world, camera.viewportWidth, camera.viewportHeight, 1); /* Define the mouse joint. We use walls as the first body of the joint */ createMouseJointDefinition(walls); }
Example #17
Source File: JumpingSample.java From Codelabs with MIT License | 4 votes |
@Override public void show() { /* * This line is found in every sample but is not necessary for the * sample functionality. calls Sample.show() method. That method set the * sample to receive all touch and key input events. Also prevents the * app from be closed whenever the user press back button and instead * returns to main menu. */ super.show(); /* * Create world with a common gravity vector (9.81 m/s2 downwards force) * and tell world that we want objects to sleep. This last value * conserves CPU usage. */ world = new World(new Vector2(0, -9.81f), true); /* Create renderer */ debugRenderer = new Box2DDebugRenderer(); /* * Define camera viewport. Box2D uses meters internally so the camera * must be defined also in meters. We set a desired width and adjust * height to different resolutions. */ camera = new OrthographicCamera(20, 20 * (Gdx.graphics.getHeight() / (float) Gdx.graphics .getWidth())); /* * Next line must remain commented because we do this in its parent (See * Sample class). In case you are not using Sample class, uncomment this * line to set input processor to handle events. */ // Gdx.input.setInputProcessor(this); /* Create the player */ Shape shape = Box2DFactory.createBoxShape(0.35f, 1, new Vector2(0, 0), 0); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 1, 0, 0, false); player = Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(0, 0)); /* * Create foot sensor. This sensor will be used to detect when the * player is standing on something. */ shape = Box2DFactory.createBoxShape(0.1f, 0.1f, new Vector2(0, -1), 0); fixtureDef = Box2DFactory.createFixture(shape, 0, 0, 0, true); /* * Set user data to the player. In this case we are using an integer. * This will help us to identify the fixture during collision handling. */ player.createFixture(fixtureDef).setUserData(new Integer(3)); /* * Fix the rotation of player's body. We don't want our player to fall * every time he/she touch an object :) */ player.setFixedRotation(true); /* Create the walls */ Box2DFactory.createWalls(world, camera.viewportWidth, camera.viewportHeight, 1); world.setContactListener(this); }