Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright 2013 Robin Stumm (serverkorken@googlemail.com, http://dermetfan.bplaced.net)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package net.dermetfan.libgdx.box2d;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.physics.box2d.Body;
- import com.badlogic.gdx.physics.box2d.ChainShape;
- import com.badlogic.gdx.physics.box2d.CircleShape;
- import com.badlogic.gdx.physics.box2d.EdgeShape;
- import com.badlogic.gdx.physics.box2d.Fixture;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.Shape;
- import com.badlogic.gdx.physics.box2d.Shape.Type;
- import com.badlogic.gdx.physics.box2d.Transform;
- import com.google.common.base.Objects;
- import java.io.Serializable;
- import java.util.ArrayList;
- import net.dermetfan.libgdx.math.GeometryUtils;
- import org.eclipse.xtext.xbase.lib.Conversions;
- import org.eclipse.xtext.xbase.lib.ExclusiveRange;
- /**
- * provides methods for geometric operations with Box2D fodies, fixtures and shapes
- */
- @SuppressWarnings("all")
- public class Box2DUtils {
- /**
- * @return the vertices of all fixtures of the given body
- * @see {@link #vertices(Shape)}
- */
- public static Vector2[] vertices(final Body body) {
- Vector2[] _xblockexpression = null;
- {
- ArrayList<Fixture> _fixtureList = body.getFixtureList();
- int _length = ((Object[])Conversions.unwrapArray(_fixtureList, Object.class)).length;
- final Serializable[] fixtureVertices = new Serializable[_length];
- int _length_1 = fixtureVertices.length;
- ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, _length_1, true);
- for (final Integer it : _doubleDotLessThan) {
- ArrayList<Fixture> _fixtureList_1 = body.getFixtureList();
- Fixture _get = _fixtureList_1.get((it).intValue());
- Vector2[] _vertices = Box2DUtils.vertices(_get);
- fixtureVertices[(it).intValue()] = _vertices;
- }
- int vertexCount = 0;
- int fvi = (-1);
- ArrayList<Fixture> _fixtureList_2 = body.getFixtureList();
- for (final Fixture fixture : _fixtureList_2) {
- Shape _shape = fixture.getShape();
- Type _type = _shape.getType();
- boolean _equals = Objects.equal(_type, Type.Circle);
- if (_equals) {
- int _plus = (vertexCount + 4);
- vertexCount = _plus;
- } else {
- int _plus_1 = (fvi + 1);
- int _fvi = fvi = _plus_1;
- Serializable _get_1 = fixtureVertices[_fvi];
- int _length_2 = ((Vector2[]) _get_1).length;
- int _plus_2 = (vertexCount + _length_2);
- vertexCount = _plus_2;
- }
- }
- final Vector2[] vertices = new Vector2[vertexCount];
- fvi = 0;
- int vi = (-1);
- ArrayList<Fixture> _fixtureList_3 = body.getFixtureList();
- for (final Fixture fixture_1 : _fixtureList_3) {
- int _plus_3 = (fvi + 1);
- int _fvi_1 = fvi = _plus_3;
- Serializable _get_2 = fixtureVertices[_fvi_1];
- for (final Vector2 vertice : ((Vector2[]) _get_2)) {
- int _plus_4 = (vi + 1);
- int _vi = vi = _plus_4;
- vertices[_vi] = vertice;
- }
- }
- _xblockexpression = (vertices);
- }
- return _xblockexpression;
- }
- /**
- * @see {@link #vertices(Shape)}
- */
- public static Vector2[] vertices(final Fixture fixture) {
- Shape _shape = fixture.getShape();
- Vector2[] _vertices = Box2DUtils.vertices(_shape);
- return _vertices;
- }
- /**
- * @return the vertices of the given Shape
- */
- public static Vector2[] vertices(final Shape shape) {
- Vector2[] _xblockexpression = null;
- {
- Vector2[] vertices = null;
- Type _type = shape.getType();
- final Type _switchValue = _type;
- boolean _matched = false;
- if (!_matched) {
- if (Objects.equal(_switchValue,Type.Polygon)) {
- _matched=true;
- final PolygonShape polygonShape = ((PolygonShape) shape);
- int _vertexCount = polygonShape.getVertexCount();
- Vector2[] _newArrayOfSize = new Vector2[_vertexCount];
- vertices = _newArrayOfSize;
- int _length = vertices.length;
- ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, _length, true);
- for (final Integer it : _doubleDotLessThan) {
- {
- Vector2 _vector2 = new Vector2();
- vertices[(it).intValue()] = _vector2;
- Vector2 _get = vertices[(it).intValue()];
- polygonShape.getVertex((it).intValue(), _get);
- }
- }
- }
- }
- if (!_matched) {
- if (Objects.equal(_switchValue,Type.Edge)) {
- _matched=true;
- final EdgeShape edgeShape = ((EdgeShape) shape);
- Vector2 _vector2 = new Vector2();
- final Vector2 vertex1 = _vector2;
- Vector2 _vector2_1 = new Vector2();
- final Vector2 vertex2 = _vector2_1;
- edgeShape.getVertex1(vertex1);
- edgeShape.getVertex2(vertex2);
- vertices = new Vector2[] { vertex1, vertex2 };
- }
- }
- if (!_matched) {
- if (Objects.equal(_switchValue,Type.Chain)) {
- _matched=true;
- final ChainShape chainShape = ((ChainShape) shape);
- int _vertexCount_1 = chainShape.getVertexCount();
- Vector2[] _newArrayOfSize_1 = new Vector2[_vertexCount_1];
- vertices = _newArrayOfSize_1;
- int _length_1 = vertices.length;
- ExclusiveRange _doubleDotLessThan_1 = new ExclusiveRange(0, _length_1, true);
- for (final Integer it_1 : _doubleDotLessThan_1) {
- {
- Vector2 _vector2_2 = new Vector2();
- vertices[(it_1).intValue()] = _vector2_2;
- Vector2 _get = vertices[(it_1).intValue()];
- chainShape.getVertex((it_1).intValue(), _get);
- }
- }
- }
- }
- if (!_matched) {
- if (Objects.equal(_switchValue,Type.Circle)) {
- _matched=true;
- final CircleShape circleShape = ((CircleShape) shape);
- Vector2 _positionRelative = Box2DUtils.positionRelative(circleShape);
- float _radius = circleShape.getRadius();
- float _minus = (_positionRelative.x - _radius);
- Vector2 _positionRelative_1 = Box2DUtils.positionRelative(circleShape);
- float _radius_1 = circleShape.getRadius();
- float _plus = (_positionRelative_1.y + _radius_1);
- Vector2 _vector2_2 = new Vector2(_minus, _plus);
- Vector2 _positionRelative_2 = Box2DUtils.positionRelative(circleShape);
- float _radius_2 = circleShape.getRadius();
- float _minus_1 = (_positionRelative_2.x - _radius_2);
- Vector2 _positionRelative_3 = Box2DUtils.positionRelative(circleShape);
- float _radius_3 = circleShape.getRadius();
- float _minus_2 = (_positionRelative_3.y - _radius_3);
- Vector2 _vector2_3 = new Vector2(_minus_1, _minus_2);
- Vector2 _positionRelative_4 = Box2DUtils.positionRelative(circleShape);
- float _radius_4 = circleShape.getRadius();
- float _plus_1 = (_positionRelative_4.x + _radius_4);
- Vector2 _positionRelative_5 = Box2DUtils.positionRelative(circleShape);
- float _radius_5 = circleShape.getRadius();
- float _minus_3 = (_positionRelative_5.y - _radius_5);
- Vector2 _vector2_4 = new Vector2(_plus_1, _minus_3);
- Vector2 _positionRelative_6 = Box2DUtils.positionRelative(circleShape);
- float _radius_6 = circleShape.getRadius();
- float _plus_2 = (_positionRelative_6.x + _radius_6);
- Vector2 _positionRelative_7 = Box2DUtils.positionRelative(circleShape);
- float _radius_7 = circleShape.getRadius();
- float _plus_3 = (_positionRelative_7.y + _radius_7);
- Vector2 _vector2_5 = new Vector2(_plus_2, _plus_3);
- vertices = new Vector2[] { _vector2_2, _vector2_3, _vector2_4, _vector2_5 };
- }
- }
- if (!_matched) {
- Type _type_1 = shape.getType();
- String _name = _type_1.name();
- String _plus_4 = ("Shapes of the type \'" + _name);
- String _plus_5 = (_plus_4 + "\' are not supported");
- IllegalArgumentException _illegalArgumentException = new IllegalArgumentException(_plus_5);
- throw _illegalArgumentException;
- }
- _xblockexpression = (vertices);
- }
- return _xblockexpression;
- }
- /**
- * @return the minimal x value of the vertices of all fixtures of the the given Body
- */
- public static float minX(final Body body) {
- float _xblockexpression = (float) 0;
- {
- float x = Float.POSITIVE_INFINITY;
- float tmp = 0;
- ArrayList<Fixture> _fixtureList = body.getFixtureList();
- for (final Fixture fixture : _fixtureList) {
- float _xifexpression = (float) 0;
- float _minX = Box2DUtils.minX(fixture);
- float _tmp = tmp = _minX;
- boolean _lessThan = (_tmp < x);
- if (_lessThan) {
- _xifexpression = tmp;
- } else {
- _xifexpression = x;
- }
- x = _xifexpression;
- }
- _xblockexpression = (x);
- }
- return _xblockexpression;
- }
- /**
- * @return the minimal y value of the vertices of all fixtures of the the given Body
- */
- public static float minY(final Body body) {
- float _xblockexpression = (float) 0;
- {
- float y = Float.POSITIVE_INFINITY;
- float tmp = 0;
- ArrayList<Fixture> _fixtureList = body.getFixtureList();
- for (final Fixture fixture : _fixtureList) {
- float _xifexpression = (float) 0;
- float _minY = Box2DUtils.minY(fixture);
- float _tmp = tmp = _minY;
- boolean _lessThan = (_tmp < y);
- if (_lessThan) {
- _xifexpression = tmp;
- } else {
- _xifexpression = y;
- }
- y = _xifexpression;
- }
- _xblockexpression = (y);
- }
- return _xblockexpression;
- }
- /**
- * @return the maximal x value of the vertices of all fixtures of the the given Body
- */
- public static float maxX(final Body body) {
- float _xblockexpression = (float) 0;
- {
- float x = Float.NEGATIVE_INFINITY;
- float tmp = 0;
- ArrayList<Fixture> _fixtureList = body.getFixtureList();
- for (final Fixture fixture : _fixtureList) {
- float _xifexpression = (float) 0;
- float _maxX = Box2DUtils.maxX(fixture);
- float _tmp = tmp = _maxX;
- boolean _greaterThan = (_tmp > x);
- if (_greaterThan) {
- _xifexpression = tmp;
- } else {
- _xifexpression = x;
- }
- x = _xifexpression;
- }
- _xblockexpression = (x);
- }
- return _xblockexpression;
- }
- /**
- * @return the maximal y value of the vertices of all fixtures of the the given Body
- */
- public static float maxY(final Body body) {
- float _xblockexpression = (float) 0;
- {
- float y = Float.NEGATIVE_INFINITY;
- float tmp = 0;
- ArrayList<Fixture> _fixtureList = body.getFixtureList();
- for (final Fixture fixture : _fixtureList) {
- float _xifexpression = (float) 0;
- float _maxY = Box2DUtils.maxY(fixture);
- float _tmp = tmp = _maxY;
- boolean _greaterThan = (_tmp > y);
- if (_greaterThan) {
- _xifexpression = tmp;
- } else {
- _xifexpression = y;
- }
- y = _xifexpression;
- }
- _xblockexpression = (y);
- }
- return _xblockexpression;
- }
- /**
- * @return the minimal x value of the vertices of the given Fixture
- */
- public static float minX(final Fixture fixture) {
- Vector2[] _vertices = Box2DUtils.vertices(fixture);
- float[] _filterX = GeometryUtils.filterX(_vertices);
- float _min = GeometryUtils.min(_filterX);
- return _min;
- }
- /**
- * @return the minimal y value of the vertices of the given Fixture
- */
- public static float minY(final Fixture fixture) {
- Vector2[] _vertices = Box2DUtils.vertices(fixture);
- float[] _filterY = GeometryUtils.filterY(_vertices);
- float _min = GeometryUtils.min(_filterY);
- return _min;
- }
- /**
- * @return the maximal x value of the vertices of the given Fixture
- */
- public static float maxX(final Fixture fixture) {
- Vector2[] _vertices = Box2DUtils.vertices(fixture);
- float[] _filterX = GeometryUtils.filterX(_vertices);
- float _max = GeometryUtils.max(_filterX);
- return _max;
- }
- /**
- * @return the maximal y value of the vertices of the given Fixture
- */
- public static float maxY(final Fixture fixture) {
- Vector2[] _vertices = Box2DUtils.vertices(fixture);
- float[] _filterY = GeometryUtils.filterY(_vertices);
- float _max = GeometryUtils.max(_filterY);
- return _max;
- }
- /**
- * @return the width of the given Body
- */
- public static float width(final Body body) {
- float _xblockexpression = (float) 0;
- {
- float min = Float.POSITIVE_INFINITY;
- float max = Float.NEGATIVE_INFINITY;
- float tmp = 0;
- ArrayList<Fixture> _fixtureList = body.getFixtureList();
- for (final Fixture fixture : _fixtureList) {
- {
- float _xifexpression = (float) 0;
- float _minX = Box2DUtils.minX(fixture);
- float _tmp = tmp = _minX;
- boolean _lessThan = (_tmp < min);
- if (_lessThan) {
- _xifexpression = tmp;
- } else {
- _xifexpression = min;
- }
- min = _xifexpression;
- float _xifexpression_1 = (float) 0;
- float _maxX = Box2DUtils.maxX(fixture);
- float _tmp_1 = tmp = _maxX;
- boolean _greaterThan = (_tmp_1 > max);
- if (_greaterThan) {
- _xifexpression_1 = tmp;
- } else {
- _xifexpression_1 = max;
- }
- max = _xifexpression_1;
- }
- }
- float _minus = (max - min);
- float _abs = Math.abs(_minus);
- _xblockexpression = (_abs);
- }
- return _xblockexpression;
- }
- /**
- * @return the height of the given Body
- */
- public static float height(final Body body) {
- float _xblockexpression = (float) 0;
- {
- float min = Float.POSITIVE_INFINITY;
- float max = Float.NEGATIVE_INFINITY;
- float tmp = 0;
- ArrayList<Fixture> _fixtureList = body.getFixtureList();
- for (final Fixture fixture : _fixtureList) {
- {
- float _xifexpression = (float) 0;
- float _minY = Box2DUtils.minY(fixture);
- float _tmp = tmp = _minY;
- boolean _lessThan = (_tmp < min);
- if (_lessThan) {
- _xifexpression = tmp;
- } else {
- _xifexpression = min;
- }
- min = _xifexpression;
- float _xifexpression_1 = (float) 0;
- float _maxY = Box2DUtils.maxY(fixture);
- float _tmp_1 = tmp = _maxY;
- boolean _greaterThan = (_tmp_1 > max);
- if (_greaterThan) {
- _xifexpression_1 = tmp;
- } else {
- _xifexpression_1 = max;
- }
- max = _xifexpression_1;
- }
- }
- float _minus = (max - min);
- float _abs = Math.abs(_minus);
- _xblockexpression = (_abs);
- }
- return _xblockexpression;
- }
- /**
- * @return the width of the given Fixture
- */
- public static float width(final Fixture fixture) {
- Shape _shape = fixture.getShape();
- float _width = Box2DUtils.width(_shape);
- return _width;
- }
- /**
- * @return the height of the given Fixture
- */
- public static float height(final Fixture fixture) {
- Shape _shape = fixture.getShape();
- float _height = Box2DUtils.height(_shape);
- return _height;
- }
- /**
- * @return the width of the given Shape
- */
- public static float width(final Shape shape) {
- Vector2[] _vertices = Box2DUtils.vertices(shape);
- float[] _filterX = GeometryUtils.filterX(_vertices);
- float _amplitude = GeometryUtils.amplitude(_filterX);
- return _amplitude;
- }
- /**
- * @return the height of the given Shape
- */
- public static float height(final Shape shape) {
- Vector2[] _vertices = Box2DUtils.vertices(shape);
- float[] _filterY = GeometryUtils.filterY(_vertices);
- float _amplitude = GeometryUtils.amplitude(_filterY);
- return _amplitude;
- }
- /**
- * @return the size of the given Shape
- */
- public static Vector2 size(final Shape shape) {
- Vector2 _xblockexpression = null;
- {
- Type _type = shape.getType();
- boolean _equals = Objects.equal(_type, Type.Circle);
- if (_equals) {
- float _radius = shape.getRadius();
- float _multiply = (_radius * 2);
- float _radius_1 = shape.getRadius();
- float _multiply_1 = (_radius_1 * 2);
- new Vector2(_multiply, _multiply_1);
- }
- float _width = Box2DUtils.width(shape);
- float _height = Box2DUtils.height(shape);
- Vector2 _vector2 = new Vector2(_width, _height);
- _xblockexpression = (_vector2);
- }
- return _xblockexpression;
- }
- /**
- * @return the relative position of the given CircleShape to its Body
- */
- public static Vector2 positionRelative(final CircleShape shape) {
- Vector2 _position = shape.getPosition();
- return _position;
- }
- /**
- * @return the relative position of the given Shape to its Body
- * @param rotation the rotation of the body in radians
- */
- public static Vector2 positionRelative(final Shape shape, final float rotation) {
- Vector2 _xblockexpression = null;
- {
- Vector2 _vector2 = new Vector2();
- final Vector2 position = _vector2;
- final Vector2[] vertices = Box2DUtils.vertices(shape);
- float[] _filterX = GeometryUtils.filterX(vertices);
- float _max = GeometryUtils.max(_filterX);
- float[] _filterX_1 = GeometryUtils.filterX(vertices);
- float _amplitude = GeometryUtils.amplitude(_filterX_1);
- float _divide = (_amplitude / 2);
- float _minus = (_max - _divide);
- float[] _filterY = GeometryUtils.filterY(vertices);
- float _max_1 = GeometryUtils.max(_filterY);
- float[] _filterY_1 = GeometryUtils.filterY(vertices);
- float _amplitude_1 = GeometryUtils.amplitude(_filterY_1);
- float _divide_1 = (_amplitude_1 / 2);
- float _minus_1 = (_max_1 - _divide_1);
- position.set(_minus, _minus_1);
- float xx = position.x;
- float xy = position.y;
- float yx = position.x;
- float yy = position.y;
- double _cos = Math.cos(rotation);
- float _multiply = (xx * ((float) _cos));
- double _sin = Math.sin(rotation);
- float _multiply_1 = (xy * ((float) _sin));
- float _minus_2 = (_multiply - _multiply_1);
- xx = _minus_2;
- double _sin_1 = Math.sin(rotation);
- float _multiply_2 = (yx * ((float) _sin_1));
- double _cos_1 = Math.cos(rotation);
- float _multiply_3 = (yy * ((float) _cos_1));
- float _plus = (_multiply_2 + _multiply_3);
- yy = _plus;
- Vector2 _set = position.set(xx, yy);
- _xblockexpression = (_set);
- }
- return _xblockexpression;
- }
- /**
- * @return the position of the given Fixture in world coordinates
- */
- public static Vector2 position(final Fixture fixture) {
- Shape _shape = fixture.getShape();
- Body _body = fixture.getBody();
- Vector2 _position = Box2DUtils.position(_shape, _body);
- return _position;
- }
- /**
- * @return the position of the given Shape in world coordinates
- * @param body the Body the given Shape is attached to
- */
- public static Vector2 position(final Shape shape, final Body body) {
- Vector2 _position = body.getPosition();
- Transform _transform = body.getTransform();
- float _rotation = _transform.getRotation();
- Vector2 _positionRelative = Box2DUtils.positionRelative(shape, _rotation);
- Vector2 _add = _position.add(_positionRelative);
- return _add;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement