Advertisement
recastrodiaz

Box2dDebugRenderer GLES2 port

Jan 31st, 2012
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1. package org.extension.physics.box2d.util;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.LinkedList;
  7. import java.util.Queue;
  8.  
  9. import org.andengine.entity.Entity;
  10. import org.andengine.entity.primitive.Ellipse;
  11. import org.andengine.entity.primitive.PolyLine;
  12. import org.andengine.entity.shape.Shape;
  13. import org.andengine.extension.physics.box2d.PhysicsConnector;
  14. import org.andengine.extension.physics.box2d.PhysicsWorld;
  15. import org.andengine.opengl.vbo.VertexBufferObjectManager;
  16.  
  17. import android.graphics.Color;
  18.  
  19. import com.badlogic.gdx.math.Vector2;
  20. import com.badlogic.gdx.physics.box2d.Body;
  21. import com.badlogic.gdx.physics.box2d.CircleShape;
  22. import com.badlogic.gdx.physics.box2d.Fixture;
  23. import com.badlogic.gdx.physics.box2d.PolygonShape;
  24.  
  25. /**
  26.  * Code Licence : GNU Lesser GPL
  27.  * @author TODO
  28.  * @author skyuzo : TODO
  29.  * @author Rodrigo Castro : GLES2 port.
  30.  * Needs PolyLine and Ellipse classes not currently present in the official git branch:
  31.  * https://github.com/recastrodiaz/AndEngine/
  32.  */
  33. public class Box2dDebugRenderer extends Entity {
  34.  
  35.     private PhysicsWorld world;
  36.    
  37.     // TODO do saving this manager has any bad consequences ?
  38.     private VertexBufferObjectManager mVertexBufferObjectManager;
  39.    
  40.     private Queue<Body> toRemove = new LinkedList<Body>();
  41.     private HashMap<Body, ArrayList<Shape>> bodies = new HashMap<Body, ArrayList<Shape>>();
  42.     private HashMap<Body, Boolean> isBodyActiveMap = new HashMap<Body, Boolean>();
  43.     private HashMap<Shape, Boolean> isSensorMap = new HashMap<Shape, Boolean>();
  44.    
  45.     private float colorAlpha = 0.35f;
  46.     private int activeBodyColor = Color.rgb(255, 0, 0);
  47.     private int sensorBodyColor = Color.rgb(55, 55, 255);
  48.     private int inactiveBodyColor = Color.rgb(180, 180, 180);
  49.    
  50.     public Box2dDebugRenderer(PhysicsWorld world, VertexBufferObjectManager pVertexBufferObjectManager ) {
  51.         this.world = world;
  52.         this.mVertexBufferObjectManager = pVertexBufferObjectManager;
  53.     }
  54.    
  55.     @Override
  56.     public void onManagedUpdate(float pSecondsElapsed) {
  57.         float ptm = PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT;
  58.        
  59.         for (Body body : bodies.keySet()) {
  60.             isBodyActiveMap.put(body, false);
  61.         }
  62.        
  63.         Iterator<Body> iterator = world.getBodies();
  64.         while(iterator.hasNext()) {
  65.             Body body = iterator.next();
  66.             if(!bodies.containsKey(body)) addBodyToShapeCollection(body);
  67.             isBodyActiveMap.put(body, true);
  68.            
  69.             // update shapes
  70.             ArrayList<Shape> shapes = bodies.get(body);
  71.             int shapesSize = shapes.size();
  72.             for(int i = 0; i < shapesSize; i++) {
  73.                 Shape shape = shapes.get(i);
  74.                
  75.                 shape.setAlpha(colorAlpha);
  76.                 if(body.isAwake()) shape.setColor(Color.red(activeBodyColor), Color.green(activeBodyColor), Color.blue(activeBodyColor));
  77.                 if(isSensorMap.containsKey(shape)) shape.setColor(Color.red(sensorBodyColor), Color.green(sensorBodyColor), Color.blue(sensorBodyColor));
  78.                 else shape.setColor(Color.red(inactiveBodyColor), Color.green(inactiveBodyColor), Color.blue(inactiveBodyColor));
  79.                
  80.                 shape.setRotationCenter(body.getMassData().center.x * ptm, body.getMassData().center.y * ptm);
  81.                 shape.setRotation((float) (body.getAngle() * (180 / Math.PI)));
  82.                 shape.setPosition(body.getPosition().x * ptm, body.getPosition().y * ptm);
  83.             }
  84.         }
  85.  
  86.         // remove all shapes of bodies that are no longer in the world
  87.         for (Body body : bodies.keySet()) {
  88.             if(isBodyActiveMap.get(body) == null || isBodyActiveMap.get(body) == false) {
  89.                 ArrayList<Shape> shapes = bodies.get(body);
  90.                 for(Shape shape : shapes) {
  91.                     this.detachChild(shape);
  92.                 }
  93.                 toRemove.add(body);
  94.             }
  95.         }
  96.        
  97.         // remove all bodies that are no longer in the world
  98.         for(Body body : toRemove) {
  99.             bodies.remove(body);
  100.         }
  101.         toRemove.clear();
  102.     }
  103.    
  104.     private void addBodyToShapeCollection(Body body) {
  105.         bodies.put(body, new ArrayList<Shape>());
  106.         isBodyActiveMap.put(body, false);
  107.        
  108.         for(Fixture fixture : body.getFixtureList()) {
  109.             Shape debugShape = null;
  110.             if(fixture.getShape() instanceof PolygonShape) {
  111.                 debugShape = createPolygonShape(fixture);
  112.             } else if(fixture.getShape() instanceof CircleShape){
  113.                 debugShape = createCircleShape(fixture);
  114.             }
  115.            
  116.             if(debugShape != null) {
  117.                 bodies.get(body).add(debugShape);
  118.                 if(fixture.isSensor()) isSensorMap.put(debugShape, true);
  119.                 this.attachChild(debugShape);
  120.             }
  121.         }
  122.     }
  123.  
  124.     private Shape createCircleShape(Fixture fixture) {
  125.         CircleShape fixtureShape = (CircleShape) fixture.getShape();
  126.         Vector2 position = fixtureShape.getPosition();
  127.         float radius = fixtureShape.getRadius()*PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT;
  128.         return new Ellipse(position.x, position.y, radius, radius, mVertexBufferObjectManager);
  129.     }
  130.    
  131.     private Shape createPolygonShape(Fixture fixture) {
  132.         PolygonShape fixtureShape = (PolygonShape) fixture.getShape();
  133.         if(fixtureShape == null)
  134.             return null;
  135.         int vSize = fixtureShape.getVertexCount();
  136.         float[] xPoints = new float[vSize];
  137.         float[] yPoints = new float[vSize];
  138.         Vector2 vertex = new Vector2();
  139.        
  140.         for(int i = 0; i < fixtureShape.getVertexCount(); i++) {
  141.             fixtureShape.getVertex(i, vertex);
  142.             xPoints[i] = vertex.x * PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT;
  143.             yPoints[i] = vertex.y * PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT;
  144.         }
  145.        
  146.         return new PolyLine(0, 0, xPoints, yPoints, mVertexBufferObjectManager);
  147.     }
  148.  
  149.     public void setActiveBodyColor(int red, int green, int blue) {
  150.         activeBodyColor = Color.rgb(red, green, blue);
  151.     }
  152.     public void setSensorBodyColor(int red, int green, int blue) {
  153.         sensorBodyColor = Color.rgb(red, green, blue);
  154.     }
  155.     public void setInactiveBodyColor(int red, int green, int blue) {
  156.         inactiveBodyColor = Color.rgb(red, green, blue);
  157.     }
  158.  
  159.     public int getActiveBodyColor() {
  160.         return activeBodyColor;
  161.     }
  162.  
  163.     public int getSensorBodyColor() {
  164.         return sensorBodyColor;
  165.     }
  166.  
  167.     public int getInactiveBodyColor() {
  168.         return inactiveBodyColor;
  169.     }
  170.    
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement