Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. package com.mygdx.game;
  2.  
  3. import com.badlogic.gdx.ApplicationListener;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input;
  6. import com.badlogic.gdx.InputAdapter;
  7. import com.badlogic.gdx.graphics.GL20;
  8. import com.badlogic.gdx.graphics.OrthographicCamera;
  9. import com.badlogic.gdx.graphics.Texture;
  10. import com.badlogic.gdx.graphics.g2d.Sprite;
  11. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  12. import com.badlogic.gdx.math.Matrix4;
  13. import com.badlogic.gdx.math.Vector2;
  14. import com.badlogic.gdx.physics.box2d.Body;
  15. import com.badlogic.gdx.physics.box2d.BodyDef;
  16. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  17. import com.badlogic.gdx.physics.box2d.FixtureDef;
  18. import com.badlogic.gdx.physics.box2d.PolygonShape;
  19. import com.badlogic.gdx.physics.box2d.World;
  20.  
  21. public class MyGdxGame extends InputAdapter implements ApplicationListener
  22. {
  23. private OrthographicCamera mCamera;
  24.  
  25. private SpriteBatch mBatch;
  26. private Texture mTexture;
  27. private Sprite mSprite;
  28.  
  29. private World mWorld;
  30.  
  31. private Body mBody;
  32.  
  33. private Box2DDebugRenderer mDebugRenderer;
  34. private Matrix4 mDebugMatrix;
  35.  
  36. private static float PIXELS_TO_METERS = 0.01f;
  37. private static float METERS_TO_PIXELS = 100f;
  38.  
  39. @Override
  40. public void create()
  41. {
  42. mCamera = new OrthographicCamera( Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
  43.  
  44. mTexture = new Texture( "badlogic.jpg" );
  45.  
  46. mSprite = new Sprite( mTexture );
  47. mSprite.setPosition( 250f, 200f );
  48.  
  49. // if we change the sprite position, we need to not change the origin in the body
  50.  
  51. mWorld = new World( new Vector2( 0f, -1f ), true );
  52.  
  53. BodyDef bodyDef = new BodyDef();
  54. bodyDef.type = BodyDef.BodyType.DynamicBody;
  55. bodyDef.position.set( mSprite.getX() * PIXELS_TO_METERS, mSprite.getY() * PIXELS_TO_METERS );
  56.  
  57. mBody = mWorld.createBody( bodyDef );
  58.  
  59. PolygonShape shape = new PolygonShape();
  60. shape.setAsBox( ( mSprite.getWidth() / 2f ) * PIXELS_TO_METERS, ( mSprite.getHeight() / 2f ) * PIXELS_TO_METERS );
  61.  
  62. FixtureDef fixtureDef = new FixtureDef();
  63. fixtureDef.shape = shape;
  64. fixtureDef.density = 1f;
  65. fixtureDef.restitution = 0.5f;
  66.  
  67. mBody.createFixture( fixtureDef );
  68.  
  69. BodyDef bodyDef2 = new BodyDef();
  70. bodyDef2.type = BodyDef.BodyType.StaticBody;
  71. bodyDef2.position.set( 0f, -Gdx.graphics.getHeight() / 2f * PIXELS_TO_METERS );
  72.  
  73. //bodyDef2.position.set( 0f, -2f );
  74.  
  75. Body groundBody = mWorld.createBody( bodyDef2 );
  76.  
  77. PolygonShape groundShape = new PolygonShape();
  78. groundShape.setAsBox( Gdx.graphics.getWidth() * PIXELS_TO_METERS / 2f, Gdx.graphics.getHeight() * 0.05f * PIXELS_TO_METERS );
  79.  
  80. FixtureDef fixtureDef2 = new FixtureDef();
  81. fixtureDef2.shape = groundShape;
  82. fixtureDef2.density = 1f;
  83. fixtureDef2.friction = 1f;
  84.  
  85. groundBody.createFixture( fixtureDef2 );
  86.  
  87. shape.dispose();
  88. groundShape.dispose();
  89.  
  90. mDebugMatrix = mCamera.combined.cpy().scale( METERS_TO_PIXELS, METERS_TO_PIXELS, 1f );
  91.  
  92. mDebugRenderer = new Box2DDebugRenderer();
  93.  
  94. mBatch = new SpriteBatch();
  95. mBatch.setProjectionMatrix( mCamera.combined );
  96. }
  97.  
  98. @Override
  99. public void render()
  100. {
  101. if( Gdx.input.isKeyPressed( Input.Keys.LEFT ) )
  102. {
  103. mBody.setLinearVelocity( -0.5f, 1f );
  104. }
  105. if( Gdx.input.isKeyPressed( Input.Keys.UP ) )
  106. mBody.setAngularVelocity( 0.1f );
  107.  
  108. mWorld.step( Gdx.graphics.getDeltaTime(), 6, 2 );
  109.  
  110. mSprite.setPosition( mBody.getPosition().x * METERS_TO_PIXELS, mBody.getPosition().y * METERS_TO_PIXELS );
  111. mSprite.translate( -mSprite.getWidth() / 2f, -mSprite.getHeight() / 2f );
  112. mSprite.setRotation( (float)Math.toDegrees( mBody.getAngle() ) );
  113.  
  114. Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
  115.  
  116. mBatch.begin();
  117. mBatch.draw( mSprite, mSprite.getX(), mSprite.getY(), mSprite.getOriginX(), mSprite.getOriginY(),
  118. mSprite.getWidth(), mSprite.getHeight(), mSprite.getScaleX(), mSprite.getScaleY(),
  119. mSprite.getRotation() );
  120.  
  121. mBatch.end();
  122.  
  123. mDebugRenderer.render( mWorld, mDebugMatrix );
  124. }
  125.  
  126. @Override
  127. public void dispose()
  128. {
  129. mBatch.dispose();
  130. mTexture.dispose();
  131.  
  132. mWorld.dispose();
  133. mDebugRenderer.dispose();
  134. }
  135.  
  136. @Override
  137. public void pause()
  138. {
  139.  
  140.  
  141. }
  142.  
  143. public void resume()
  144. {
  145.  
  146. }
  147.  
  148. public void resize( int width, int height )
  149. {
  150.  
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement