Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mygdx.game;
- import com.badlogic.gdx.ApplicationListener;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.InputAdapter;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Matrix4;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.physics.box2d.Body;
- import com.badlogic.gdx.physics.box2d.BodyDef;
- import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.World;
- public class MyGdxGame extends InputAdapter implements ApplicationListener
- {
- private OrthographicCamera mCamera;
- private SpriteBatch mBatch;
- private Texture mTexture;
- private Sprite mSprite;
- private World mWorld;
- private Body mBody;
- private Box2DDebugRenderer mDebugRenderer;
- private Matrix4 mDebugMatrix;
- private static float PIXELS_TO_METERS = 0.01f;
- private static float METERS_TO_PIXELS = 100f;
- @Override
- public void create()
- {
- mCamera = new OrthographicCamera( Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
- mTexture = new Texture( "badlogic.jpg" );
- mSprite = new Sprite( mTexture );
- mSprite.setPosition( 250f, 200f );
- // if we change the sprite position, we need to not change the origin in the body
- mWorld = new World( new Vector2( 0f, -1f ), true );
- BodyDef bodyDef = new BodyDef();
- bodyDef.type = BodyDef.BodyType.DynamicBody;
- bodyDef.position.set( mSprite.getX() * PIXELS_TO_METERS, mSprite.getY() * PIXELS_TO_METERS );
- mBody = mWorld.createBody( bodyDef );
- PolygonShape shape = new PolygonShape();
- shape.setAsBox( ( mSprite.getWidth() / 2f ) * PIXELS_TO_METERS, ( mSprite.getHeight() / 2f ) * PIXELS_TO_METERS );
- FixtureDef fixtureDef = new FixtureDef();
- fixtureDef.shape = shape;
- fixtureDef.density = 1f;
- fixtureDef.restitution = 0.5f;
- mBody.createFixture( fixtureDef );
- BodyDef bodyDef2 = new BodyDef();
- bodyDef2.type = BodyDef.BodyType.StaticBody;
- bodyDef2.position.set( 0f, -Gdx.graphics.getHeight() / 2f * PIXELS_TO_METERS );
- //bodyDef2.position.set( 0f, -2f );
- Body groundBody = mWorld.createBody( bodyDef2 );
- PolygonShape groundShape = new PolygonShape();
- groundShape.setAsBox( Gdx.graphics.getWidth() * PIXELS_TO_METERS / 2f, Gdx.graphics.getHeight() * 0.05f * PIXELS_TO_METERS );
- FixtureDef fixtureDef2 = new FixtureDef();
- fixtureDef2.shape = groundShape;
- fixtureDef2.density = 1f;
- fixtureDef2.friction = 1f;
- groundBody.createFixture( fixtureDef2 );
- shape.dispose();
- groundShape.dispose();
- mDebugMatrix = mCamera.combined.cpy().scale( METERS_TO_PIXELS, METERS_TO_PIXELS, 1f );
- mDebugRenderer = new Box2DDebugRenderer();
- mBatch = new SpriteBatch();
- mBatch.setProjectionMatrix( mCamera.combined );
- }
- @Override
- public void render()
- {
- if( Gdx.input.isKeyPressed( Input.Keys.LEFT ) )
- {
- mBody.setLinearVelocity( -0.5f, 1f );
- }
- if( Gdx.input.isKeyPressed( Input.Keys.UP ) )
- mBody.setAngularVelocity( 0.1f );
- mWorld.step( Gdx.graphics.getDeltaTime(), 6, 2 );
- mSprite.setPosition( mBody.getPosition().x * METERS_TO_PIXELS, mBody.getPosition().y * METERS_TO_PIXELS );
- mSprite.translate( -mSprite.getWidth() / 2f, -mSprite.getHeight() / 2f );
- mSprite.setRotation( (float)Math.toDegrees( mBody.getAngle() ) );
- Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
- mBatch.begin();
- mBatch.draw( mSprite, mSprite.getX(), mSprite.getY(), mSprite.getOriginX(), mSprite.getOriginY(),
- mSprite.getWidth(), mSprite.getHeight(), mSprite.getScaleX(), mSprite.getScaleY(),
- mSprite.getRotation() );
- mBatch.end();
- mDebugRenderer.render( mWorld, mDebugMatrix );
- }
- @Override
- public void dispose()
- {
- mBatch.dispose();
- mTexture.dispose();
- mWorld.dispose();
- mDebugRenderer.dispose();
- }
- @Override
- public void pause()
- {
- }
- public void resume()
- {
- }
- public void resize( int width, int height )
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement