Guest User

libgdx box2dlights testing

a guest
Oct 3rd, 2014
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 KB | None | 0 0
  1. package com.mygdx.game;
  2.  
  3. import box2dLight.DirectionalLight;
  4. import box2dLight.PointLight;
  5. import box2dLight.RayHandler;
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.Input.Keys;
  8. import com.badlogic.gdx.Screen;
  9. import com.badlogic.gdx.graphics.Color;
  10. import com.badlogic.gdx.graphics.GL20;
  11. import com.badlogic.gdx.graphics.OrthographicCamera;
  12. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  13. import com.badlogic.gdx.maps.MapObjects;
  14. import com.badlogic.gdx.maps.tiled.TiledMap;
  15. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
  16. import com.badlogic.gdx.maps.tiled.TmxMapLoader;
  17. import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
  18. import com.badlogic.gdx.math.Vector2;
  19. import com.badlogic.gdx.physics.box2d.Body;
  20. import com.badlogic.gdx.physics.box2d.BodyDef;
  21. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  22. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  23. import com.badlogic.gdx.physics.box2d.FixtureDef;
  24. import com.badlogic.gdx.physics.box2d.PolygonShape;
  25. import com.badlogic.gdx.physics.box2d.World;
  26.  
  27. public class LightingTest implements Screen {
  28.     final Drop game;
  29.  
  30.     float screenW, screenH;
  31.  
  32.     OrthographicCamera cam;
  33.     OrthogonalTiledMapRenderer mapRenderer;
  34.     MapObjects mapObjects;
  35.  
  36.     SpriteBatch mapBatch;
  37.  
  38.     World world;
  39.     OrthographicCamera boxLightCamera;
  40.     RayHandler rayHandler;
  41.     Box2DDebugRenderer debugRenderer;
  42.  
  43.     int TILE_SIZE = 64;
  44.     float PIXEL_PER_METER = 1 / 64f;
  45.  
  46.     PointLight pl;
  47.     DirectionalLight dl;
  48.  
  49.     public LightingTest(final Drop gam) {
  50.         this.game = gam;
  51.  
  52.         screenW = Gdx.graphics.getWidth();
  53.         screenH = Gdx.graphics.getHeight();
  54.  
  55.         cam = new OrthographicCamera(30, 30 * (screenH / screenW));
  56.         cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);
  57.         cam.update();
  58.  
  59.         TiledMap map = new TmxMapLoader().load("assets/map01.tmx");
  60.  
  61.         mapBatch = new SpriteBatch();
  62.         mapRenderer = new OrthogonalTiledMapRenderer(map, PIXEL_PER_METER, mapBatch);
  63.         mapRenderer.setView(cam);
  64.  
  65.         mapObjects = map.getLayers().get("objects").getObjects();
  66.  
  67.         debugRenderer = new Box2DDebugRenderer();
  68.  
  69.         boxLightCamera = createBox2DCam();
  70.         world = new World(new Vector2(), true);
  71.         rayHandler = createRayHandler(boxLightCamera);
  72.  
  73.         createLights();
  74.         createWorldScenery(map);
  75.     }
  76.  
  77.     private RayHandler createRayHandler(OrthographicCamera boxLightCamera) {
  78.         RayHandler.useDiffuseLight(true);
  79.         RayHandler rayHandler = new RayHandler(world);
  80.         rayHandler.setCombinedMatrix(boxLightCamera.combined);
  81.         rayHandler.setShadows(true);
  82.  
  83.         //rayHandler.setAmbientLight(0.2f, 0.2f, 0.2f, 0.1f);
  84.        
  85.         return rayHandler;
  86.     }
  87.  
  88.     private void createLights() {
  89.         pl = new PointLight(rayHandler, 128);
  90.         pl.setDistance(500);
  91.         pl.setPosition(150, 150);
  92.         pl.setColor(new Color(0.4f, 0.4f, 0f, 1f));
  93.         pl.setSoftnessLength(0);
  94.         pl.setSoft(true);
  95.  
  96.         dl = new DirectionalLight(rayHandler, 128, new Color(0.3f, 0.3f, 0.3f, 1f), 45);
  97.         dl.setSoftnessLength(0);
  98.         dl.setSoft(true);
  99.     }
  100.  
  101.     private OrthographicCamera createBox2DCam() {
  102.         // light camera
  103.         OrthographicCamera boxLightCamera = new OrthographicCamera();
  104.         float boxLightViewportWidth = cam.viewportWidth / PIXEL_PER_METER;
  105.         float boxLightViewportHeight = cam.viewportHeight / PIXEL_PER_METER;
  106.         boxLightCamera.setToOrtho(true, boxLightViewportWidth, boxLightViewportHeight);
  107.         boxLightCamera.update(true);
  108.  
  109.         return boxLightCamera;
  110.     }
  111.  
  112.     protected void createWorldScenery(TiledMap tileMap) {
  113.         // build plan for wall bodies
  114.         float halfBody = TILE_SIZE / 64f / 2;
  115.  
  116.         PolygonShape tileShape = new PolygonShape();
  117.         tileShape.setAsBox(0.5f, 0.5f);
  118.  
  119.         BodyDef tileBodyDef = new BodyDef();
  120.         tileBodyDef.type = BodyType.StaticBody;
  121.  
  122.         FixtureDef fixtureDef = new FixtureDef();
  123.         fixtureDef.shape = tileShape;
  124.         fixtureDef.filter.groupIndex = 0;
  125.  
  126.         // create box2d bodies for all wall tiles
  127.         TiledMapTileLayer layer = (TiledMapTileLayer) tileMap.getLayers().get(0);
  128.         int cols = layer.getWidth();
  129.         int rows = layer.getHeight();
  130.  
  131.         for (int row = 0; row < rows; row++) {
  132.             for (int col = 0; col < cols; col++) {
  133.  
  134.                 if (col == 0 || col == cols - 1 || row == 0 || row == rows - 1) {
  135.                     continue;
  136.                 }
  137.  
  138.                 int tileId = layer.getCell(col, row).getTile().getId();
  139.  
  140.                 if (tileId == 1) {
  141.                     float bodyX = col + halfBody;
  142.                     float bodyY = row + halfBody;
  143.                     tileBodyDef.position.set(bodyX, bodyY);
  144.                     Body tileBody = world.createBody(tileBodyDef);
  145.                     tileBody.createFixture(fixtureDef);
  146.                 }
  147.  
  148.             }
  149.         }
  150.  
  151.         tileShape.dispose();
  152.     }
  153.  
  154.     @Override
  155.     public void render(float delta) {
  156.         Gdx.gl.glClearColor(0, 0, 0.2f, 1);
  157.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  158.  
  159.         if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
  160.             Gdx.app.exit();
  161.         }
  162.  
  163.         pl.setPosition(Gdx.input.getX(), Gdx.input.getY());
  164.  
  165.         mapRenderer.render();
  166.  
  167.         game.batch.begin();
  168.  
  169.         game.batch.end();
  170.  
  171.         boxLightCamera.update();
  172.  
  173.         rayHandler.setCombinedMatrix(boxLightCamera.combined,
  174.                 boxLightCamera.position.x, boxLightCamera.position.y,
  175.                 boxLightCamera.viewportWidth * boxLightCamera.zoom,
  176.                 boxLightCamera.viewportHeight * boxLightCamera.zoom);
  177.  
  178.         rayHandler.updateAndRender();
  179.  
  180.         debugRenderer.render(world, cam.combined);
  181.     }
  182.  
  183.     @Override
  184.     public void dispose() {
  185.         game.dispose();
  186.         mapRenderer.dispose();
  187.         mapBatch.dispose();
  188.         world.dispose();
  189.         rayHandler.dispose();
  190.         debugRenderer.dispose();
  191.     }
  192.  
  193.     @Override
  194.     public void resize(int width, int height) {
  195.     }
  196.  
  197.     @Override
  198.     public void hide() {
  199.     }
  200.  
  201.     @Override
  202.     public void pause() {
  203.     }
  204.  
  205.     @Override
  206.     public void resume() {
  207.     }
  208.  
  209.     @Override
  210.     public void show() {
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment