use of com.badlogic.gdx.physics.box2d.Body in project libgdx-inGameConsole by StrongJoshua.
the class Box2DTest method createExplosion.
/**
* Creates an explosion that applies forces to the bodies relative to their
* position and the given x and y values.
*
* @param maxForce
* The maximum force to be applied to the bodies (diminishes as
* distance from touch increases).
*/
private void createExplosion(float x, float y, float maxForce) {
float force;
Vector2 touch = new Vector2(x, y);
for (int i = 0; i < bodies.length; i++) {
Body b = bodies[i];
Vector2 v = b.getPosition();
float dist = v.dst2(touch);
if (dist == 0)
force = maxForce;
else
force = MathUtils.clamp(maxForce / dist, 0, maxForce);
float angle = v.cpy().sub(touch).angle();
float xForce = force * MathUtils.cosDeg(angle);
float yForce = force * MathUtils.sinDeg(angle);
Vector3 touch3, v3, boundMin, boundMax, intersection;
touch3 = new Vector3(touch.x, touch.y, 0);
v3 = new Vector3(v.x, v.y, 0);
boundMin = new Vector3(v.x - 1, v.y - 1, 0);
boundMax = new Vector3(v.x + 1, v.y + 1, 0);
intersection = Vector3.Zero;
Intersector.intersectRayBounds(new Ray(touch3, v3), new BoundingBox(boundMin, boundMax), intersection);
b.applyForce(new Vector2(xForce, yForce), new Vector2(intersection.x, intersection.y), true);
}
}
use of com.badlogic.gdx.physics.box2d.Body in project libgdx by libgdx.
the class Box2DCharacterControllerTest method createWorld.
private void createWorld() {
// (float)Math.random() * 0.1f + 1;
float y1 = 1;
float y2 = y1;
for (int i = 0; i < 50; i++) {
Body ground = createEdge(BodyType.StaticBody, -50 + i * 2, y1, -50 + i * 2 + 2, y2, 0);
y1 = y2;
// (float)Math.random() + 1;
y2 = 1;
}
Body box = createBox(BodyType.StaticBody, 1, 1, 0);
box.setTransform(30, 3, 0);
box = createBox(BodyType.StaticBody, 1.2f, 1.2f, 0);
box.setTransform(5, 2.4f, 0);
player = createPlayer();
player.setTransform(-40.0f, 4.0f, 0);
player.setFixedRotation(true);
for (int i = 0; i < 20; i++) {
box = createBox(BodyType.DynamicBody, (float) Math.random(), (float) Math.random(), 3);
box.setTransform((float) Math.random() * 10f - (float) Math.random() * 10f, (float) Math.random() * 10 + 6, (float) (Math.random() * 2 * Math.PI));
}
for (int i = 0; i < 20; i++) {
Body circle = createCircle(BodyType.DynamicBody, (float) Math.random() * 0.5f, 3);
circle.setTransform((float) Math.random() * 10f - (float) Math.random() * 10f, (float) Math.random() * 10 + 6, (float) (Math.random() * 2 * Math.PI));
}
platforms.add(new CirclePlatform(-24, -5, 10, (float) Math.PI / 4));
platforms.add(new MovingPlatform(-2, 3, 2, 0.5f, 2, 0, (float) Math.PI / 10f, 4));
platforms.add(new MovingPlatform(17, 2, 5, 0.5f, 2, 0, 0, 5));
platforms.add(new MovingPlatform(-7, 5, 2, 0.5f, -2, 2, 0, 8));
// platforms.add(new MovingPlatform(40, 3, 20, 0.5f, 0, 2, 5));
}
use of com.badlogic.gdx.physics.box2d.Body in project libgdx by libgdx.
the class Box2DCharacterControllerTest method createCircle.
Body createCircle(BodyType type, float radius, float density) {
BodyDef def = new BodyDef();
def.type = type;
Body box = world.createBody(def);
CircleShape poly = new CircleShape();
poly.setRadius(radius);
box.createFixture(poly, density);
poly.dispose();
return box;
}
use of com.badlogic.gdx.physics.box2d.Body in project libgdx by libgdx.
the class Box2DTest method touchDown.
@Override
public boolean touchDown(int x, int y, int pointer, int newParam) {
// translate the mouse coordinates to world coordinates
testPoint.set(x, y, 0);
camera.unproject(testPoint);
// ask the world which bodies are within the given
// bounding box around the mouse pointer
hitBody = null;
world.QueryAABB(callback, testPoint.x - 0.1f, testPoint.y - 0.1f, testPoint.x + 0.1f, testPoint.y + 0.1f);
// and attach it to the hit body.
if (hitBody != null) {
MouseJointDef def = new MouseJointDef();
def.bodyA = groundBody;
def.bodyB = hitBody;
def.collideConnected = true;
def.target.set(testPoint.x, testPoint.y);
def.maxForce = 1000.0f * hitBody.getMass();
mouseJoint = (MouseJoint) world.createJoint(def);
hitBody.setAwake(true);
} else {
for (Body box : boxes) world.destroyBody(box);
boxes.clear();
createBoxes();
}
return false;
}
use of com.badlogic.gdx.physics.box2d.Body in project libgdx by libgdx.
the class Box2DTest method render.
@Override
public void render() {
// first we update the world. For simplicity
// we use the delta time provided by the Graphics
// instance. Normally you'll want to fix the time
// step.
long start = TimeUtils.nanoTime();
world.step(Gdx.graphics.getDeltaTime(), 8, 3);
float updateTime = (TimeUtils.nanoTime() - start) / 1000000000.0f;
// next we clear the color buffer and set the camera
// matrices
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
// next we render the ground body
renderBox(groundBody, 50, 1);
// next we render each box via the SpriteBatch.
// for this we have to set the projection matrix of the
// spritebatch to the camera's combined matrix. This will
// make the spritebatch work in world coordinates
batch.getProjectionMatrix().set(camera.combined);
batch.begin();
for (int i = 0; i < boxes.size(); i++) {
Body box = boxes.get(i);
// that's the box's center position
Vector2 position = box.getPosition();
// the rotation angle around the center
float angle = MathUtils.radiansToDegrees * box.getAngle();
// the bottom left corner of the box, unrotated
batch.draw(// the bottom left corner of the box, unrotated
textureRegion, // the bottom left corner of the box, unrotated
position.x - 1, // the bottom left corner of the box, unrotated
position.y - 1, // the rotation center relative to the bottom left corner of the box
1f, // the rotation center relative to the bottom left corner of the box
1f, // the width and height of the box
2, // the width and height of the box
2, // the scale on the x- and y-axis
1, // the scale on the x- and y-axis
1, // the rotation angle
angle);
}
batch.end();
// next we use the debug renderer. Note that we
// simply apply the camera again and then call
// the renderer. the camera.apply() call is actually
// not needed as the opengl matrices are already set
// by the spritebatch which in turn uses the camera matrices :)
debugRenderer.render(world, camera.combined);
// finally we render all contact points
renderer.setProjectionMatrix(camera.combined);
renderer.begin(ShapeType.Point);
renderer.setColor(0, 1, 0, 1);
for (int i = 0; i < world.getContactCount(); i++) {
Contact contact = world.getContactList().get(i);
// we only render the contact if it actually touches
if (contact.isTouching()) {
// get the world manifold from which we get the
// contact points. A manifold can have 0, 1 or 2
// contact points.
WorldManifold manifold = contact.getWorldManifold();
int numContactPoints = manifold.getNumberOfContactPoints();
for (int j = 0; j < numContactPoints; j++) {
Vector2 point = manifold.getPoints()[j];
renderer.point(point.x, point.y, 0);
}
}
}
renderer.end();
// finally we render the time it took to update the world
// for this we have to set the projection matrix again, so
// we work in pixel coordinates
batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.begin();
font.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond() + " update time: " + updateTime, 0, 20);
batch.end();
}
Aggregations