Advertisement
Guest User

Untitled

a guest
Dec 30th, 2010
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.16 KB | None | 0 0
  1. package nbolton.paperboy2;
  2.  
  3. import com.badlogic.gdx.ApplicationListener;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.InputProcessor;
  6. import com.badlogic.gdx.graphics.GL10;
  7. import com.badlogic.gdx.graphics.OrthographicCamera;
  8. import com.badlogic.gdx.math.Vector2;
  9. import com.badlogic.gdx.physics.box2d.Body;
  10. import com.badlogic.gdx.physics.box2d.BodyDef;
  11. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  12. import com.badlogic.gdx.physics.box2d.CircleShape;
  13. import com.badlogic.gdx.physics.box2d.Fixture;
  14. import com.badlogic.gdx.physics.box2d.FixtureDef;
  15. import com.badlogic.gdx.physics.box2d.PolygonShape;
  16. import com.badlogic.gdx.physics.box2d.QueryCallback;
  17. import com.badlogic.gdx.physics.box2d.World;
  18. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  19. import com.badlogic.gdx.physics.box2d.joints.MouseJoint;
  20. import com.badlogic.gdx.physics.box2d.joints.MouseJointDef;
  21. import com.badlogic.gdx.physics.box2d.joints.RevoluteJoint;
  22. import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
  23.  
  24. public class PaperBoy2 implements ApplicationListener, InputProcessor {
  25.    
  26.     /** the camera **/
  27.     protected OrthographicCamera camera;
  28.  
  29.     /** the renderer **/
  30.     protected Box2DDebugRenderer renderer;
  31.  
  32.     /** our box2D world **/
  33.     protected World world;
  34.  
  35.     /** ground body to connect the mouse joint to **/
  36.     protected Body groundBody;
  37.     protected Body leftWall;
  38.     protected Body rightWall;
  39.  
  40.     /** our mouse joint **/
  41.     protected MouseJoint mouseJoint = null;
  42.  
  43.     /** a hit body **/
  44.     protected Body hitBody = null;
  45.  
  46.     final short FILTER_NONE = 0x0000;
  47.     final short FILTER_SUPPORT = 0x0001;
  48.     final short FILTER_WALL = 0x0002;
  49.     final short FILTER_BOY = 0x0004;
  50.     final short FILTER_STUFF = 0x0008;
  51.    
  52.     final float CAMERA_WIDTH = 12;
  53.     final float CAMERA_HEIGHT = 8;
  54.    
  55.     protected void createWorld() {
  56.        
  57.         groundBody = createWall(500, 0.1f, 0);
  58.  
  59.         //createCircles();
  60.         //createBoxes();
  61.        
  62.         createStickManSideOn(0, 2.5f);
  63.     }
  64.    
  65.     RevoluteJoint leftArmJoint, rightArmJoint,
  66.         leftLegTopJoint, rightLegTopJoint,
  67.         leftLegBottomJoint, rightLegBottomJoint;
  68.     Body torso, torsoSupport;
  69.    
  70.     float supportY;
  71.  
  72.     float legAngle = (float)Math.toRadians(100);
  73.     float armAngle = (float)Math.toRadians(60);
  74.     float headAngle = (float)Math.toRadians(30);
  75.  
  76.     private void createStickManSideOn(float x, float y) {
  77.        
  78.         torso = createRectangleBodyPart(x, y, 0.1f, 0.8f);
  79.         Body head = createRoundBodyPart(x, y + 1.2f, 0.4f);
  80.        
  81.         Body leftArm = createRectangleBodyPart(x, y, 0.1f, 0.65f);
  82.         Body rightArm = createRectangleBodyPart(x, y, 0.1f, 0.65f);
  83.        
  84.         Body leftLegTop = createRectangleBodyPart(x, y - 1.1f, 0.1f, 0.5f);
  85.         Body rightLegTop = createRectangleBodyPart(x, y - 1.1f, 0.1f, 0.5f);
  86.         Body leftLegBottom = createRectangleBodyPart(x, y - 1.9f, 0.1f, 0.5f);
  87.         Body rightLegBottom = createRectangleBodyPart(x, y - 1.9f, 0.1f, 0.5f);
  88.        
  89.         joinBodyParts(torso, head, new Vector2(0, 0.8f), headAngle);
  90.        
  91.         leftArmJoint = joinBodyParts(torso, leftArm, new Vector2(0, 0.6f), -armAngle * 0.7f, armAngle);
  92.         rightArmJoint = joinBodyParts(torso, rightArm, new Vector2(0, 0.6f), -armAngle * 0.7f, armAngle);
  93.        
  94.         leftLegTopJoint = joinBodyParts(torso, leftLegTop, new Vector2(0, -0.7f), 0.1f, legAngle);
  95.         rightLegTopJoint = joinBodyParts(torso, rightLegTop, new Vector2(0, -0.7f), 0.1f, legAngle);
  96.         leftLegBottomJoint = joinBodyParts(leftLegTop, leftLegBottom, new Vector2(0, -0.4f), -legAngle * 1.5f, 0);
  97.         rightLegBottomJoint = joinBodyParts(rightLegTop, rightLegBottom, new Vector2(0, -0.4f), -legAngle * 1.5f, 0);
  98.        
  99.         //torsoSupport = createSupportBody(new Vector2(x, y + 5));
  100.         //joinSupportBody(torso, torsoSupport, new Vector2(0, 0));
  101.        
  102.         //supportY = y + 5;
  103.         //supportLeft = createSupportBody(new Vector2(x - 6, supportY));
  104.         //supportRight = createSupportBody(new Vector2(x + 6, supportY));
  105.  
  106.         //joinSupportBody(head, supportLeft, new Vector2(0, 0));
  107.         //joinSupportBody(torso, supportLeft, new Vector2(0, -1f));
  108.         //joinSupportBody(head, supportRight, new Vector2(0, 0));
  109.         //joinSupportBody(torso, supportRight, new Vector2(0, -1f));
  110.     }
  111.  
  112.     /*private WeldJoint joinSupportBody(Body body, Body support, Vector2 bodyAnchor) {
  113.        
  114.         WeldJointDef jointDef = new WeldJointDef();
  115.        
  116.         jointDef.initialize(body, support, body.getWorldPoint(bodyAnchor));
  117.        
  118.         return (WeldJoint)world.createJoint(jointDef);
  119.     }*/
  120.    
  121.     private RevoluteJoint joinBodyParts(Body a, Body b, Vector2 anchor, float angle) {
  122.        
  123.         return joinBodyParts(a, b, anchor, -angle, angle);
  124.     }
  125.    
  126.     private RevoluteJoint joinBodyParts(
  127.         Body a, Body b, Vector2 anchor, float lowerAngle, float upperAngle) {
  128.  
  129.         RevoluteJointDef jointDef = new RevoluteJointDef();
  130.        
  131.         jointDef.initialize(a, b, a.getWorldPoint(anchor));
  132.        
  133.         jointDef.enableLimit = true;
  134.         jointDef.lowerAngle = lowerAngle;
  135.         jointDef.upperAngle = upperAngle;
  136.        
  137.         return (RevoluteJoint)world.createJoint(jointDef);
  138.     }
  139.  
  140.     private Body createRoundBodyPart(float x, float y, float radius) {
  141.  
  142.         CircleShape shape = new CircleShape();
  143.         shape.setRadius(radius);
  144.  
  145.         BodyDef bodyDef = new BodyDef();
  146.         bodyDef.type = BodyType.DynamicBody;
  147.         bodyDef.position.x = x;
  148.         bodyDef.position.y = y;
  149.         Body body = world.createBody(bodyDef);
  150.        
  151.         FixtureDef fixtureDef = new FixtureDef();
  152.         fixtureDef.shape = shape;
  153.         fixtureDef.density = 1;
  154.        
  155.         // -1 means no body parts collide
  156.         fixtureDef.filter.groupIndex = -1;
  157.         fixtureDef.filter.categoryBits = FILTER_BOY;
  158.         fixtureDef.filter.maskBits = FILTER_STUFF | FILTER_WALL;
  159.  
  160.         // add the boxPoly shape as a fixture
  161.         body.createFixture(fixtureDef);
  162.         shape.dispose();
  163.        
  164.         return body;
  165.     }
  166.  
  167.     private Body createRectangleBodyPart(float x, float y, float width, float height) {
  168.        
  169.         PolygonShape shape = new PolygonShape();
  170.         shape.setAsBox(width, height);
  171.        
  172.         BodyDef bodyDef = new BodyDef();
  173.         bodyDef.type = BodyType.DynamicBody;
  174.         bodyDef.position.y = y;
  175.         bodyDef.position.x = x;
  176.        
  177.         Body body = world.createBody(bodyDef);
  178.        
  179.         FixtureDef fixtureDef = new FixtureDef();
  180.         fixtureDef.shape = shape;
  181.         fixtureDef.density = 1;
  182.         //fixtureDef.friction = 100;
  183.        
  184.         // -1 means no body parts collide
  185.         fixtureDef.filter.groupIndex = -1;
  186.         fixtureDef.filter.categoryBits = FILTER_BOY;
  187.         fixtureDef.filter.maskBits = FILTER_STUFF | FILTER_WALL;
  188.        
  189.         body.createFixture(fixtureDef);
  190.         shape.dispose();
  191.        
  192.         return body;
  193.     }
  194.     /*
  195.     private Body createSupportBody(Vector2 position)
  196.     {
  197.         PolygonShape shape = new PolygonShape();
  198.         shape.setAsBox(1, 1);
  199.        
  200.         BodyDef bodyDef = new BodyDef();
  201.         bodyDef.type = BodyType.DynamicBody;
  202.         bodyDef.position.x = position.x;
  203.         bodyDef.position.y = position.y;
  204.        
  205.         Body body = world.createBody(bodyDef);
  206.        
  207.         FixtureDef fixtureDef = new FixtureDef();
  208.         fixtureDef.shape = shape;
  209.         fixtureDef.density = 10;
  210.        
  211.         // don't collide with anything
  212.         fixtureDef.filter.categoryBits = FILTER_SUPPORT;
  213.         fixtureDef.filter.maskBits = FILTER_WALL;
  214.        
  215.         body.createFixture(fixtureDef);
  216.         shape.dispose();
  217.        
  218.         return body;
  219.     }
  220.     */
  221.     private void createCircles() {
  222.         // next we add a few more circles
  223.         CircleShape circleShape = new CircleShape();
  224.         circleShape.setRadius(1);
  225.  
  226.         for (int i = 0; i < 5; i++) {
  227.             BodyDef circleBodyDef = new BodyDef();
  228.             circleBodyDef.type = BodyType.DynamicBody;
  229.             circleBodyDef.position.x = -24 + (float)(Math.random() * 48);
  230.             circleBodyDef.position.y = 10 + (float)(Math.random() * 100);
  231.             Body circleBody = world.createBody(circleBodyDef);
  232.  
  233.             FixtureDef fixtureDef = new FixtureDef();
  234.             fixtureDef.shape = circleShape;
  235.             fixtureDef.density = 10;
  236.            
  237.             fixtureDef.filter.categoryBits = FILTER_STUFF;
  238.             fixtureDef.filter.maskBits = FILTER_STUFF | FILTER_BOY | FILTER_WALL;
  239.            
  240.             // add the boxPoly shape as a fixture
  241.             circleBody.createFixture(fixtureDef);
  242.         }
  243.         circleShape.dispose();
  244.     }
  245.  
  246.     private void createBoxes() {
  247.         // next we create 50 boxes at random locations above the ground
  248.         // body. First we create a nice polygon representing a box 2 meters
  249.         // wide and high.
  250.         PolygonShape boxPoly = new PolygonShape();
  251.         boxPoly.setAsBox(1, 1);
  252.  
  253.         // next we create the 50 box bodies using the PolygonShape we just
  254.         // defined. This process is similar to the one we used for the ground
  255.         // body. Note that we reuse the polygon for each body fixture.
  256.         for (int i = 0; i < 5; i++) {
  257.             // Create the BodyDef, set a random position above the
  258.             // ground and create a new body
  259.             BodyDef boxBodyDef = new BodyDef();
  260.             boxBodyDef.type = BodyType.DynamicBody;
  261.             boxBodyDef.position.x = -24 + (float)(Math.random() * 48);
  262.             boxBodyDef.position.y = 10 + (float)(Math.random() * 100);
  263.             Body boxBody = world.createBody(boxBodyDef);
  264.  
  265.             FixtureDef fixtureDef = new FixtureDef();
  266.             fixtureDef.shape = boxPoly;
  267.             fixtureDef.density = 10;
  268.  
  269.             fixtureDef.filter.categoryBits = FILTER_STUFF;
  270.             fixtureDef.filter.maskBits = FILTER_STUFF | FILTER_BOY | FILTER_WALL;
  271.            
  272.             // add the boxPoly shape as a fixture
  273.             boxBody.createFixture(fixtureDef);
  274.         }
  275.  
  276.         // we are done, all that's left is disposing the boxPoly
  277.         boxPoly.dispose();
  278.     }
  279.  
  280.     private Body createWall(float width, float height, float xOffset) {
  281.         // next we create a static ground platform. This platform
  282.         // is not moveable and will not react to any influences from
  283.         // outside. It will however influence other bodies. First we
  284.         // create a PolygonShape that holds the form of the platform.
  285.         // it will be 100 meters wide and 2 meters high, centered
  286.         // around the origin
  287.         PolygonShape groundPoly = new PolygonShape();
  288.         groundPoly.setAsBox(width, height);
  289.  
  290.         // next we create the body for the ground platform. It's
  291.         // simply a static body.
  292.         BodyDef groundBodyDef = new BodyDef();
  293.         groundBodyDef.type = BodyType.StaticBody;
  294.         groundBodyDef.position.x = xOffset;
  295.        
  296.         Body body = world.createBody(groundBodyDef);
  297.  
  298.         FixtureDef fixtureDef = new FixtureDef();
  299.         fixtureDef.shape = groundPoly;
  300.         fixtureDef.density = 10;
  301.        
  302.         fixtureDef.filter.categoryBits = FILTER_WALL;
  303.         fixtureDef.filter.maskBits = FILTER_BOY | FILTER_STUFF | FILTER_SUPPORT;
  304.        
  305.         body.createFixture(fixtureDef);
  306.         groundPoly.dispose();
  307.        
  308.         return body;
  309.     }
  310.     /*
  311.     final float motorSpeed = 100f;
  312.     float leftMotorVelocity;
  313.     float rightMotorVelocity;
  314.     int leftMotorDirection = 1;
  315.     int rightMotorDirection = -1;
  316.     */
  317.     /** temp vector **/
  318.     protected Vector2 tmp = new Vector2();
  319.  
  320.     @Override public void render () {
  321.        
  322.         float timeStep = 1.0f / 60.f;
  323.         int velocityIterations = 10;
  324.         int positionIterations = 8;
  325.        
  326.         // update the world with a fixed time step
  327.         world.step(timeStep, velocityIterations, positionIterations);
  328.        
  329.         // follow the boy!
  330.         camera.getPosition().set(torso.getPosition().x, torso.getPosition().y + 1, 0);
  331.  
  332.         // clear the screen and setup the projection matrix
  333.         GL10 gl = Gdx.app.getGraphics().getGL10();
  334.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  335.         camera.setMatrices();
  336.        
  337.         /*float leftAngle = leftLegTopJoint.getJointAngle();
  338.         float legAngleShort = legAngle * 0.8f;
  339.  
  340.         if (leftAngle > legAngleShort) {
  341.            
  342.             leftMotorDirection = -1;
  343.         }
  344.        
  345.         if (leftAngle < -legAngleShort) {
  346.            
  347.             leftMotorDirection = 1;
  348.         }
  349.        
  350.         System.out.println("a: " + leftAngle + " / m: " + leftMotorDirection);
  351.        
  352.         rightLegBottomJoint.setMotorSpeed(8);
  353.         leftLegBottomJoint.setMotorSpeed(-8);
  354.  
  355.         leftMotorVelocity = delta * leftMotorDirection * motorSpeed;
  356.         rightMotorVelocity = -leftMotorVelocity;
  357.        
  358.         leftLegTopJoint.setMotorSpeed(leftMotorVelocity * 0.9f);
  359.         rightLegTopJoint.setMotorSpeed(rightMotorVelocity * 0.9f);
  360.        
  361.         leftArmJoint.setMotorSpeed(-leftMotorVelocity * 0.5f);
  362.         rightArmJoint.setMotorSpeed(-rightMotorVelocity * 0.5f);
  363.        
  364.         // ensure the support block stays on the same y plane, but follows the head.
  365.         //float supportY = torso.getPosition().y;
  366.         float supportY = 7;
  367.         supportLeft.setTransform(new Vector2(torso.getPosition().x - 10, supportY), 0);
  368.         supportRight.setTransform(new Vector2(torso.getPosition().x + 10, supportY), 0);*/
  369.        
  370.         //torso.setTransform(torso.getPosition(), 0);
  371.         //torsoSupport.setTransform(new Vector2(torso.getPosition().x, groundBody.getPosition().y + 6), 0);
  372.  
  373.         // render the world using the debug renderer
  374.         renderer.render(world);
  375.     }
  376.  
  377.     /*Body aabbHit = null;
  378.     private Body getBodyBelow(Body body) {
  379.        
  380.         Body found = null;
  381.        
  382.         Iterator<Body> bodies = world.getBodies();
  383.         while (bodies.hasNext()) {
  384.            
  385.             QueryCallback aabb = new QueryCallback() {
  386.                
  387.                 @Override
  388.                 public boolean reportFixture(Fixture arg0) {
  389.                    
  390.                     aabbHit = arg0.getBody();
  391.                     return true;
  392.                 }
  393.             };
  394.            
  395.             Body test = bodies.next();
  396.             Vector2 testPos = test.getPosition();
  397.             world.QueryAABB(aabb, testPos.x, testPos.y, testPos.x, testPos.y);
  398.            
  399.             if ((testPos.y < body.getPosition().y) && (aabbHit != null)) {
  400.                
  401.                 float distanceLast = 0;
  402.                 if (found != null)
  403.                     distanceLast = Math.abs(found.getPosition().x - body.getPosition().x);
  404.                 else
  405.                     found = test;
  406.                
  407.                 float distanceThis = Math.abs(testPos.x - body.getPosition().x);
  408.                
  409.                 if (distanceThis < distanceLast)
  410.                     found = test;
  411.             }
  412.         }
  413.        
  414.         return found;
  415.     }*/
  416.  
  417.     @Override public void create () {
  418.        
  419.         camera = new OrthographicCamera();
  420.         camera.setViewport(CAMERA_WIDTH, CAMERA_HEIGHT);
  421.  
  422.         // create the debug renderer
  423.         renderer = new Box2DDebugRenderer();
  424.  
  425.         // create the world
  426.         world = new World(new Vector2(0, -9.8f), true);
  427.  
  428.         // we also need an invisible zero size ground body
  429.         // to which we can connect the mouse joint
  430.         BodyDef bodyDef = new BodyDef();
  431.         groundBody = world.createBody(bodyDef);
  432.  
  433.         // call abstract method to populate the world
  434.         createWorld();
  435.        
  436.         Gdx.input.setInputProcessor(this);
  437.     }
  438.  
  439.     @Override public void dispose () {     
  440.         renderer.dispose();
  441.         world.dispose();
  442.  
  443.         renderer = null;
  444.         world = null;
  445.         mouseJoint = null;
  446.         hitBody = null;
  447.     }
  448.  
  449.     @Override public boolean keyDown (int keycode) {
  450.         return false;
  451.     }
  452.  
  453.     @Override public boolean keyTyped (char character) {
  454.         return false;
  455.     }
  456.  
  457.     @Override public boolean keyUp (int keycode) {
  458.         return false;
  459.     }
  460.  
  461.     /** we instantiate this vector and the callback here so we don't irritate the GC **/
  462.     Vector2 testPoint = new Vector2();
  463.     QueryCallback callback = new QueryCallback() {
  464.         @Override public boolean reportFixture (Fixture fixture) {
  465.             // if the hit point is inside the fixture of the body
  466.             // we report it
  467.             if (fixture.testPoint(testPoint)) {
  468.                 hitBody = fixture.getBody();
  469.                 return false;
  470.             } else
  471.                 return true;
  472.         }
  473.     };
  474.  
  475.     @Override public boolean touchDown (int x, int y, int pointer) {
  476.         // translate the mouse coordinates to world coordinates
  477.         camera.getScreenToWorld(x, y, testPoint);
  478.         // ask the world which bodies are within the given
  479.         // bounding box around the mouse pointer
  480.         hitBody = null;
  481.         world.QueryAABB(callback, testPoint.x - 0.0001f, testPoint.y - 0.0001f, testPoint.x + 0.0001f, testPoint.y + 0.0001f);
  482.  
  483.         if (hitBody == groundBody) hitBody = null;
  484.  
  485.         // ignore kinematic bodies, they don't work with the mouse joint
  486.         if (hitBody != null && hitBody.getType() == BodyType.KinematicBody) return false;
  487.  
  488.         // if we hit something we create a new mouse joint
  489.         // and attach it to the hit body.
  490.         if (hitBody != null) {
  491.             MouseJointDef def = new MouseJointDef();
  492.             def.bodyA = groundBody;
  493.             def.bodyB = hitBody;
  494.             def.collideConnected = true;
  495.             def.target.set(testPoint);
  496.             def.maxForce = 1000.0f * hitBody.getMass();
  497.  
  498.             mouseJoint = (MouseJoint)world.createJoint(def);
  499.             hitBody.setAwake(true);
  500.         }
  501.  
  502.         return false;
  503.     }
  504.  
  505.     /** another temporary vector **/
  506.     Vector2 target = new Vector2();
  507.  
  508.     @Override public boolean touchDragged (int x, int y, int pointer) {
  509.         // if a mouse joint exists we simply update
  510.         // the target of the joint based on the new
  511.         // mouse coordinates
  512.         if (mouseJoint != null) {
  513.             camera.getScreenToWorld(x, y, target);
  514.             mouseJoint.setTarget(target);
  515.         }
  516.         return false;
  517.     }
  518.  
  519.     @Override public boolean touchUp (int x, int y, int pointer) {
  520.         // if a mouse joint exists we simply destroy it
  521.         if (mouseJoint != null) {
  522.             world.destroyJoint(mouseJoint);
  523.             mouseJoint = null;
  524.         }
  525.         return false;
  526.     }
  527.    
  528.     public void pause() {
  529.        
  530.     }
  531.    
  532.     public void resume() {
  533.        
  534.     }
  535.    
  536.     public void resize(int width, int height) {
  537.        
  538.     }
  539. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement