Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.dermetfan.someLibgdxTests.screens;
- import net.dermetfan.libgdx.graphics.FixtureSprite;
- import net.dermetfan.someLibgdxTests.Assets;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.physics.box2d.Body;
- import com.badlogic.gdx.physics.box2d.BodyDef;
- import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
- import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
- import com.badlogic.gdx.physics.box2d.CircleShape;
- import com.badlogic.gdx.physics.box2d.Fixture;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.World;
- import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
- public class Box2DRopeTest implements Screen {
- private SpriteBatch batch;
- private Box2DDebugRenderer debugRenderer;
- private OrthographicCamera camera;
- private World world;
- @Override
- public void render(float delta) {
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- world.step(1 / 60f, 8 * 5, 3 * 50);
- camera.update();
- batch.setProjectionMatrix(camera.combined);
- batch.begin();
- FixtureSprite.draw(batch, world);
- batch.end();
- debugRenderer.render(world, camera.combined);
- }
- @Override
- public void resize(int width, int height) {
- camera.viewportWidth = width / 25;
- camera.viewportHeight = height / 25;
- }
- @Override
- public void show() {
- batch = new SpriteBatch();
- debugRenderer = new Box2DDebugRenderer();
- camera = new OrthographicCamera();
- world = new World(new Vector2(0, -9.81f), true);
- BodyDef bodyDef = new BodyDef();
- FixtureDef fixtureDef = new FixtureDef();
- // box (the rope will hang from this)
- bodyDef.type = BodyType.StaticBody;
- bodyDef.position.y = 7;
- PolygonShape boxShape = new PolygonShape();
- boxShape.setAsBox(.5f, .5f);
- Body box = world.createBody(bodyDef);
- box.createFixture(boxShape, 0);
- // ball (will hang on the rope)
- bodyDef.type = BodyType.DynamicBody;
- bodyDef.position.y = 0;
- CircleShape ballShape = new CircleShape();
- ballShape.setRadius(.5f);
- fixtureDef.shape = ballShape;
- fixtureDef.density = 2;
- fixtureDef.friction = .3f;
- fixtureDef.restitution = .2f;
- Body ball = world.createBody(bodyDef);
- ball.createFixture(fixtureDef);
- ballShape.dispose();
- ball.applyLinearImpulse(new Vector2(25, 0), ball.getPosition(), true);
- // rope
- short segments = 20;
- float segWidth = .1f, segHeight = .3f;
- // segment definitions
- bodyDef.type = BodyType.DynamicBody;
- bodyDef.position.y = 6;
- PolygonShape segmentShape = new PolygonShape();
- segmentShape.setAsBox(segWidth / 2, segHeight / 2);
- fixtureDef.shape = segmentShape;
- fixtureDef.density = 1;
- fixtureDef.friction = .3f;
- fixtureDef.restitution = .05f;
- // revolute joint definitions
- RevoluteJointDef ropeDef = new RevoluteJointDef();
- ropeDef.localAnchorA.set(0, -segHeight / 2); // top
- ropeDef.localAnchorB.set(0, segHeight / 2); // bottom
- FixtureSprite segmentSprite = new FixtureSprite(Assets.japanischeFlagge);
- // first segment
- Body lastSegment = world.createBody(bodyDef);
- lastSegment.createFixture(fixtureDef).setUserData(segmentSprite);
- ropeDef.bodyA = box;
- ropeDef.bodyB = lastSegment;
- float oldLocalAnchorAY = ropeDef.localAnchorA.y;
- ropeDef.localAnchorA.y = -.5f; // set to the bottom of the box
- world.createJoint(ropeDef);
- ropeDef.localAnchorA.y = oldLocalAnchorAY;
- // other segments
- for(int s = 1; s < segments; s++) {
- bodyDef.position.y -= segHeight; // position the segments one under another
- ropeDef.bodyA = lastSegment;
- ropeDef.bodyB = world.createBody(bodyDef);
- Fixture segmentFixture = ropeDef.bodyB.createFixture(fixtureDef);
- segmentFixture.setUserData(segmentSprite);
- world.createJoint(ropeDef);
- lastSegment = ropeDef.bodyB;
- }
- segmentShape.dispose();
- // last joint
- ropeDef.bodyA = lastSegment;
- ropeDef.bodyB = ball;
- ropeDef.localAnchorB.y = ballShape.getRadius(); // set to the top of the ball
- world.createJoint(ropeDef);
- }
- @Override
- public void hide() {
- dispose();
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void dispose() {
- batch.dispose();
- debugRenderer.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement