List of usage examples for com.badlogic.gdx.physics.box2d Body getPosition
public Vector2 getPosition()
From source file:com.agateau.pixelwheels.racer.Vehicle.java
License:Open Source License
public WheelInfo addWheel(TextureRegion region, float x, float y, float angle) { WheelInfo info = new WheelInfo(); info.wheel = new Wheel(mGameWorld, this, region, getX() + x, getY() + y, angle); mWheels.add(info);/*from w w w . j a va2 s .c o m*/ Body body = info.wheel.getBody(); body.setUserData(mBody.getUserData()); RevoluteJointDef jointDef = new RevoluteJointDef(); // Call initialize() instead of defining bodies and anchors manually. Defining anchors manually // causes Box2D to move the car a bit while it solves the constraints defined by the joints jointDef.initialize(mBody, body, body.getPosition()); jointDef.lowerAngle = 0; jointDef.upperAngle = 0; jointDef.enableLimit = true; info.joint = (RevoluteJoint) mGameWorld.getBox2DWorld().createJoint(jointDef); return info; }
From source file:com.agateau.pixelwheels.racer.VehicleRenderer.java
License:Open Source License
private void drawTurbo(Batch batch) { TextureRegion region = mAssets.turboFlame.getKeyFrame(mVehicle.getTurboTime(), true); Body body = mVehicle.getBody(); Vector2 center = body.getPosition(); float angle = body.getAngle() * MathUtils.radiansToDegrees; float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth(); float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight(); float refH = -mVehicle.getWidth() / 2; float x = center.x + refH * MathUtils.cosDeg(angle); float y = center.y + refH * MathUtils.sinDeg(angle); batch.draw(region, x - w / 2, y - h, // pos w / 2, h, // origin w, h, // size 1, 1, // scale angle - 90);//w ww.ja v a2 s.c o m }
From source file:com.agateau.pixelwheels.utils.BodyRegionDrawer.java
License:Open Source License
public void draw(Body body, TextureRegion region) { Vector2 center = body.getPosition(); float angle = body.getAngle(); float x = center.x + mOffsetX * MathUtils.cos(angle) - mOffsetY * MathUtils.sin(angle); float y = center.y + mOffsetX * MathUtils.sin(angle) + mOffsetY * MathUtils.cos(angle); float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth(); float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight(); mBatch.draw(region, x - w / 2, y - h / 2, // pos w / 2, h / 2, // origin w, h, // size mScale, mScale, angle * MathUtils.radDeg); }
From source file:com.agateau.pixelwheels.utils.BodyRegionDrawer.java
License:Open Source License
public void drawShadow(Body body, TextureRegion region) { Vector2 center = body.getPosition(); float angle = body.getAngle() * MathUtils.radiansToDegrees; float offset = (SHADOW_OFFSET_PX + mZ * Z_MAX_SHADOW_OFFSET_PX + (mScale - 1) * SCALE_MAX_SHADOW_OFFSET_PX) * Constants.UNIT_FOR_PIXEL;/*from w ww. ja v a 2 s.c om*/ float x = center.x + offset; float y = center.y - offset; float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth(); float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight(); Color old = mBatch.getColor(); mBatch.setColor(0, 0, 0, SHADOW_ALPHA); mBatch.draw(region, x - w / 2, y - h / 2, // pos w / 2, h / 2, // origin w, h, // size 1, 1, // scale angle); mBatch.setColor(old); }
From source file:com.blindtigergames.werescrewed.debug.SBox2DDebugRenderer.java
License:Apache License
protected void renderBody(Body body) { Transform transform = body.getTransform(); int len = body.getFixtureList().size(); List<Fixture> fixtures = body.getFixtureList(); for (int i = 0; i < len; i++) { Fixture fixture = fixtures.get(i); if (drawBodies) { if (body.isActive() == false) drawShape(fixture, transform, SHAPE_NOT_ACTIVE); else if (body.getType() == BodyType.StaticBody) drawShape(fixture, transform, SHAPE_STATIC); else if (body.getType() == BodyType.KinematicBody) drawShape(fixture, transform, SHAPE_KINEMATIC); else if (body.isAwake() == false) drawShape(fixture, transform, SHAPE_NOT_AWAKE); else/*from ww w .ja v a 2s.co m*/ drawShape(fixture, transform, SHAPE_AWAKE); if (drawVelocities) { Vector2 position = body.getPosition(); drawSegment(position, body.getLinearVelocity().add(position), VELOCITY_COLOR); } } if (drawAABBs) { drawAABB(fixture, transform); } } }
From source file:com.cafeitvn.myballgame.screen.Box2DMapObjectParser.java
License:Apache License
/** * creates a {@link Fixture} from a {@link MapObject} * @param mapObject the {@link MapObject} which to parse * @return the parsed {@link Fixture}/* ww w . jav a 2s. c om*/ */ public Fixture createFixture(MapObject mapObject) { MapProperties properties = mapObject.getProperties(); String type = properties.get("type", String.class); Body body = bodies.get( type.equals(aliases.object) ? mapObject.getName() : properties.get(aliases.body, String.class)); if (!type.equals(aliases.fixture) && !type.equals(aliases.object)) throw new IllegalArgumentException("type of " + mapObject + " is \"" + type + "\" instead of \"" + aliases.fixture + "\" or \"" + aliases.object + "\""); FixtureDef fixtureDef = new FixtureDef(); Shape shape = null; if (mapObject instanceof RectangleMapObject) { shape = new PolygonShape(); Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle(); rectangle.x *= unitScale; rectangle.y *= unitScale; rectangle.width *= unitScale; rectangle.height *= unitScale; ((PolygonShape) shape).setAsBox(rectangle.width / 2, rectangle.height / 2, new Vector2(rectangle.x - body.getPosition().x + rectangle.width / 2, rectangle.y - body.getPosition().y + rectangle.height / 2), body.getAngle()); } else if (mapObject instanceof PolygonMapObject) { shape = new PolygonShape(); Polygon polygon = ((PolygonMapObject) mapObject).getPolygon(); polygon.setPosition(polygon.getX() * unitScale - body.getPosition().x, polygon.getY() * unitScale - body.getPosition().y); polygon.setScale(unitScale, unitScale); ((PolygonShape) shape).set(polygon.getTransformedVertices()); } else if (mapObject instanceof PolylineMapObject) { shape = new ChainShape(); Polyline polyline = ((PolylineMapObject) mapObject).getPolyline(); polyline.setPosition(polyline.getX() * unitScale - body.getPosition().x, polyline.getY() * unitScale - body.getPosition().y); polyline.setScale(unitScale, unitScale); ((ChainShape) shape).createChain(polyline.getTransformedVertices()); } else if (mapObject instanceof CircleMapObject) { shape = new CircleShape(); Circle circle = ((CircleMapObject) mapObject).getCircle(); circle.setPosition(circle.x * unitScale - body.getPosition().x, circle.y * unitScale - body.getPosition().y); circle.radius *= unitScale; ((CircleShape) shape).setPosition(new Vector2(circle.x, circle.y)); ((CircleShape) shape).setRadius(circle.radius); } else if (mapObject instanceof EllipseMapObject) { Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse(); if (ellipse.width == ellipse.height) { CircleMapObject circleMapObject = new CircleMapObject(ellipse.x, ellipse.y, ellipse.width / 2); circleMapObject.setName(mapObject.getName()); circleMapObject.getProperties().putAll(mapObject.getProperties()); circleMapObject.setColor(mapObject.getColor()); circleMapObject.setVisible(mapObject.isVisible()); circleMapObject.setOpacity(mapObject.getOpacity()); return createFixture(circleMapObject); } IllegalArgumentException exception = new IllegalArgumentException( "Cannot parse " + mapObject.getName() + " because " + mapObject.getClass().getSimpleName() + "s that are not circles are not supported"); Gdx.app.error(getClass().getSimpleName(), exception.getMessage(), exception); throw exception; } else if (mapObject instanceof TextureMapObject) { IllegalArgumentException exception = new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because " + mapObject.getClass().getSimpleName() + "s are not supported"); Gdx.app.error(getClass().getSimpleName(), exception.getMessage(), exception); throw exception; } else assert false : mapObject + " is a not known subclass of " + MapObject.class.getName(); fixtureDef.shape = shape; fixtureDef.density = (Float) getProperty(properties, aliases.density, fixtureDef.density, Float.class); fixtureDef.filter.categoryBits = (Short) getProperty(properties, aliases.categoryBits, fixtureDef.filter.categoryBits, Short.class); fixtureDef.filter.groupIndex = (Short) getProperty(properties, aliases.groupIndex, fixtureDef.filter.groupIndex, Short.class); fixtureDef.filter.maskBits = (Short) getProperty(properties, aliases.maskBits, fixtureDef.filter.maskBits, Short.class); fixtureDef.friction = (Float) getProperty(properties, aliases.friciton, fixtureDef.friction, Float.class); fixtureDef.isSensor = (Boolean) getProperty(properties, aliases.isSensor, fixtureDef.isSensor, Boolean.class); fixtureDef.restitution = (Float) getProperty(properties, aliases.restitution, fixtureDef.restitution, Float.class); Fixture fixture = body.createFixture(fixtureDef); shape.dispose(); String name = mapObject.getName(); if (fixtures.containsKey(name)) { int duplicate = 1; while (fixtures.containsKey(name + duplicate)) duplicate++; name += duplicate; } fixtures.put(name, fixture); return fixture; }
From source file:com.dongbat.game.util.MovementUtil.java
public static void newMovementMechanism(World world, Entity e) { UnitMovement unitMovement = EntityUtil.getComponent(world, e, UnitMovement.class); Physics physicsComponent = EntityUtil.getComponent(world, e, Physics.class); Body body = physicsComponent.getBody(); float mass = body.getMass(); if (unitMovement.getDirectionVelocity() == null) { // body.setLinearVelocity(new Vector2(0, 0)); return;/*from w w w . ja v a 2 s. com*/ } Vector2 directionVel = unitMovement.getDirectionVelocity().cpy(); //heading float angleRad = body.getLinearVelocity().angleRad(); body.setTransform(body.getPosition(), angleRad); // TODO: calculate based on radius, use fixed mass // or make a steering-like behavior with real mass float desiredSpd = calculalteDesiredSpeed(world, e); Vector2 currentVector = body.getLinearVelocity(); directionVel.nor().scl(desiredSpd).sub(currentVector); Vector2 impulse = directionVel.scl(mass); PhysicsUtil.applyImpulse(world, e, impulse); }
From source file:com.dongbat.game.util.MovementUtil.java
/** * Move unit to specific location on map * * @param entity entity want to move/* w w w . j a va2 s. c om*/ * @param destination location that entity will move to */ public static void moveTo(Entity entity, Vector2 destination) { World world = entity.getWorld(); Physics physicsComponent = EntityUtil.getComponent(world, entity, Physics.class); Stats stat = EntityUtil.getComponent(world, entity, Stats.class); Body body = physicsComponent.getBody(); float mass = body.getMass(); Vector2 currentVelocity = body.getLinearVelocity(); Vector2 position = body.getPosition(); float scale = stat.getBaseMovementSpeed() + stat.getModifierSpeed(); Vector2 desiredVelocity = destination.cpy().sub(position).nor().scl(scale); Vector2 impulse = desiredVelocity.sub(currentVelocity).scl(mass * 10); PhysicsUtil.applyImpulse(entity.getWorld(), entity, impulse); }
From source file:com.dongbat.game.util.WorldQueryUtil.java
/** * Find any entity in radius/*from ww w .j av a 2 s . c o m*/ * * @param world artemis world * @param location location to find * @param radius radius to find * @return Array of nearest Entity in radius */ public static Array<Entity> findAnyInRadius(final com.artemis.World world, final Vector2 location, final float radius) { final Array<Entity> entities = new Array<Entity>(); QueryCallback callback = new QueryCallback() { @Override public boolean reportFixture(Fixture fixture) { Body body = fixture.getBody(); Entity entity = UuidUtil.getEntityByUuid(world, (UUID) body.getUserData()); float distanceSq = new Vector2(body.getPosition()).sub(location).len2(); if (distanceSq <= radius * radius) { entities.add(entity); } return true; } }; Vector2 lowerLeft = new Vector2(location).sub(radius, radius); Vector2 upperRight = new Vector2(location).add(radius, radius); PhysicsUtil.getPhysicsWorld(world).QueryAABB(callback, lowerLeft.x, lowerLeft.y, upperRight.x, upperRight.y); return entities; }
From source file:com.dongbat.game.util.WorldQueryUtil.java
/** * Find food in radius, centre is an point on map * * @param world artemis world//from w w w . j a va 2 s .c o m * @param location centre of circle * @param radius radius to find * @return Food entity list */ public static Array<Entity> findFoodInRadius(final com.artemis.World world, final Vector2 location, final float radius) { final Array<Entity> food = new Array<Entity>(); QueryCallback callback = new QueryCallback() { @Override public boolean reportFixture(Fixture fixture) { Body body = fixture.getBody(); Entity entity = UuidUtil.getEntityByUuid(world, (UUID) body.getUserData()); if (entity != null) { if (isFood(world, entity.getId())) { float distanceSq = new Vector2(body.getPosition()).sub(location).len2(); if (distanceSq <= radius * radius) { food.add(entity); } } } return true; } }; Vector2 lowerLeft = new Vector2(location).sub(radius, radius); Vector2 upperRight = new Vector2(location).add(radius, radius); getPhysicsWorld(world).QueryAABB(callback, lowerLeft.x, lowerLeft.y, upperRight.x, upperRight.y); return food; }