Advertisement
dermetfan

Xtend Box2D test awesomeness

Aug 20th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package net.dermetfan.someLibgdxTests.screens
  2.  
  3. import com.badlogic.gdx.Gdx
  4. import com.badlogic.gdx.Screen
  5. import com.badlogic.gdx.graphics.GL20
  6. import com.badlogic.gdx.graphics.OrthographicCamera
  7. import com.badlogic.gdx.graphics.g2d.SpriteBatch
  8. import com.badlogic.gdx.math.Vector2
  9. import com.badlogic.gdx.physics.box2d.BodyDef
  10. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType
  11. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer
  12. import com.badlogic.gdx.physics.box2d.EdgeShape
  13. import com.badlogic.gdx.physics.box2d.FixtureDef
  14. import com.badlogic.gdx.physics.box2d.World
  15. import net.dermetfan.libgdx.graphics.Box2DSprite
  16. import net.dermetfan.someLibgdxTests.entities.Car
  17.  
  18. class XtendTest implements Screen {
  19.  
  20.     private SpriteBatch batch
  21.     private Box2DDebugRenderer debugRenderer
  22.     private OrthographicCamera camera
  23.     private World world
  24.     private Car car
  25.  
  26.     override show() {
  27.         batch = new SpriteBatch
  28.         debugRenderer = new Box2DDebugRenderer
  29.         camera = new OrthographicCamera
  30.        
  31.         world = new World(new Vector2(0, -9.81f), true)
  32.        
  33.         val chassisFixDef = new FixtureDef
  34.         chassisFixDef.density = 5
  35.         chassisFixDef.friction = 0.4f
  36.         chassisFixDef.restitution = 0.2f
  37.        
  38.         val wheelFixDef = new FixtureDef
  39.         wheelFixDef.density = chassisFixDef.density * 2
  40.         wheelFixDef.friction = 50
  41.         wheelFixDef.restitution = 0.4f
  42.        
  43.         car = new Car(world, chassisFixDef, wheelFixDef, 0, 2, 3, 1.25f)
  44.         Gdx.input.inputProcessor = car
  45.  
  46.         val groundBodyDef = new BodyDef
  47.         groundBodyDef.type = BodyType.StaticBody
  48.        
  49.         val groundShape = new EdgeShape
  50.         groundShape.set(-500, 0, 500, 0)
  51.  
  52.         world.createBody(groundBodyDef).createFixture(groundShape, 0)
  53.     }
  54.  
  55.     override render(float delta) {
  56.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
  57.        
  58.         world.step(1 / 60f, 8, 3)
  59.        
  60.         camera.position.set(car.getChassis.position.x, car.getChassis.position.y, 0)
  61.         camera.update
  62.        
  63.         batch.projectionMatrix = camera.combined
  64.         batch.begin
  65.         Box2DSprite.draw(batch, world)
  66.         batch.end
  67.        
  68.         debugRenderer.render(world, camera.combined)
  69.     }
  70.  
  71.     override resize(int width, int height) {
  72.         camera.viewportWidth = width / 25
  73.         camera.viewportHeight = height / 25
  74.     }
  75.  
  76.     override hide() {
  77.         dispose
  78.     }
  79.  
  80.     override pause() {
  81.     }
  82.  
  83.     override resume() {
  84.     }
  85.  
  86.     override dispose() {
  87.         batch.dispose
  88.     }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement