daily pastebin goal
8%
SHARE
TWEET

Rope.java

a guest Aug 17th, 2016 28 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.santivaldez.ropistgame.entities;
  2.  
  3. import com.badlogic.gdx.physics.box2d.Body;
  4. import com.badlogic.gdx.physics.box2d.BodyDef;
  5. import com.badlogic.gdx.physics.box2d.PolygonShape;
  6. import com.badlogic.gdx.physics.box2d.World;
  7. import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
  8.  
  9. import java.util.ArrayList;
  10.  
  11. /**
  12.  * Created by SantiValdez on 17-Aug-16.
  13.  */
  14. public class Rope {
  15.  
  16.     private World world;
  17.     private ArrayList<Body> segments;
  18.  
  19.     public Rope(World world) {
  20.         this.world = world;
  21.     }
  22.  
  23.     public void create(Body ceiling, Body player, int length) {
  24.  
  25.         float width = 0.10f;
  26.         float height = 0.25f;
  27.  
  28.         segments = new ArrayList<Body>();
  29.  
  30.         BodyDef segmentDef = new BodyDef();
  31.         segmentDef.type = BodyDef.BodyType.DynamicBody;
  32.  
  33.         PolygonShape segmentShape = new PolygonShape();
  34.         segmentShape.setAsBox(width / 2, height / 2);
  35.  
  36.         //create segments
  37.         for (int i = 0; i < length; i++) {
  38.             segments.add(world.createBody(segmentDef));
  39.             segments.get(i).createFixture(segmentShape, 0.1f);
  40.         }
  41.  
  42.         RevoluteJointDef revJointDef = new RevoluteJointDef();
  43.         revJointDef.localAnchorA.y = -height / 2;
  44.         revJointDef.localAnchorB.y = height / 2;
  45.  
  46.         //sticking them together
  47.         for (int i = 0; i < length; i++) {
  48.             if (i > 0) {
  49.                 revJointDef.bodyA = segments.get(i);
  50.                 revJointDef.bodyB = segments.get(i - 1);
  51.                 revJointDef.localAnchorB.y = height / 2;
  52.                 world.createJoint(revJointDef);
  53.             }
  54.             //attaching player to rope
  55.             if (i == length - 1) {
  56.                 revJointDef.bodyA = segments.get(i);
  57.                 revJointDef.bodyB = player;
  58.                 revJointDef.localAnchorA.y = height / 2;
  59.                 world.createJoint(revJointDef);
  60.             }
  61.             //ceiling to rope
  62.             if (i == 0) {
  63.                 revJointDef.bodyA = segments.get(i);
  64.                 revJointDef.bodyB = ceiling;
  65.                 revJointDef.localAnchorB.y = -(0.25f / 2);
  66.                 world.createJoint(revJointDef);
  67.             }
  68.         }
  69.  
  70.         //RopeJointDef ropeJointDef = new RopeJointDef();
  71.         //ropeJointDef.maxLength =
  72.  
  73.     }
  74.  
  75.     public void destroyRope(){
  76.         world.destroyBody(segments.get(segments.size()-1));
  77.     }
  78. }
RAW Paste Data
Top