Advertisement
penco

penco libgdx/box2d velocity issue

May 15th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.50 KB | None | 0 0
  1. public abstract class LevelScreen implements Screen{
  2.  
  3.    
  4.     public LevelScreen(Outpost outpost) {
  5.         this.outpost = outpost;
  6.     }
  7.    
  8.    
  9.     @Override
  10.     public void show(){
  11.         //add input controls
  12.         Gdx.input.setInputProcessor(new PlayerInput(this));
  13.        
  14.         //create box2d world
  15.         world = new World(new Vector2(0, 0), true);
  16.         debugRenderer = new Box2DDebugRenderer();
  17.        
  18.         //run the setupLevel method in the child class
  19.         this.setupLevel();
  20.     };
  21.    
  22.     public void render(float delta) {
  23.        
  24.         //update player data
  25.         player.update();
  26.        
  27.         //update box2d
  28.         world.step(1/60f, 6, 2);
  29.        
  30.         //do graphical rendering in the WorldRenderer
  31.         renderer.render();
  32.     }
  33.    
  34.     public void hide() {
  35.         dispose();
  36.     }
  37.  
  38.     @Override
  39.     public void dispose() {
  40.         renderer.dispose();
  41.     }
  42.    
  43.    
  44.    
  45.    
  46.     public World getBox2DWorld(){
  47.         return world;
  48.     }
  49.    
  50.     public Box2DDebugRenderer getBox2DDebugRenderer(){
  51.         return debugRenderer;
  52.     }
  53.  
  54. }
  55.  
  56. public class Overworld extends LevelScreen{
  57.    
  58.     public Overworld(Outpost outpost){
  59.         super(outpost);            
  60.     }
  61.  
  62.     public void setupLevel() {
  63.        
  64.         //set background music and start playing
  65.         PlayAudio.playMusic(true);
  66.        
  67.         //create the player object
  68.         startLocation = new Vector2(0,0);
  69.         player = new Player(this, startLocation);
  70.        
  71.         //create map and renderer
  72.         map = new TmxMapLoader().load("data/graphics/overworld.tmx");
  73.         cameraLocation = new Vector3(player.getBody().getPosition().x, player.getBody().getPosition().y, 0);
  74.         renderer = new WorldRenderer(this, this.map, cameraLocation);
  75.        
  76.     }
  77.  
  78. }
  79.  
  80.  
  81. public class WorldRenderer {
  82.     public WorldRenderer(LevelScreen screen, TiledMap map, Vector3 cameraPosition){
  83.         this.screen = screen;
  84.        
  85.         screen.setRenderer(this);
  86.        
  87.         orthogonalTiledMapRenderer = new OrthogonalTiledMapRenderer(map,1f / 32f);
  88.        
  89.         width = Gdx.graphics.getWidth();
  90.         height = Gdx.graphics.getHeight();
  91.        
  92.         cam = new OrthographicCamera();
  93.         //make sure the two integers in setToOrtho are equal to make proportions match.
  94.         //Larger integer means smaller scale images (camera further away from plane).
  95.         cam.setToOrtho(false, width / height * 16, 16);
  96.         cam.position.set(cameraPosition);
  97.         cam.update();
  98.                
  99.         batch = new SpriteBatch();
  100.         batch.setProjectionMatrix(cam.combined);
  101.        
  102.     }
  103.    
  104.     public void render(){
  105.         Gdx.gl.glClearColor(0, 0, 0, 1);
  106.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  107.        
  108.         //get objects from screen
  109.         player = screen.getPlayer();
  110.  
  111.         cam.update();
  112.  
  113.         //update Box2D debug renderer
  114.         Box2DDebugRenderer debugRenderer = screen.getBox2DDebugRenderer();
  115.         Matrix4 cameraCopy = cam.combined.cpy().scale(LevelScreen.BOX_TO_WORLD, LevelScreen.BOX_TO_WORLD, 1f);
  116.         debugRenderer.render(screen.getBox2DWorld(), cameraCopy);
  117.        
  118.         //render the tile map
  119.         orthogonalTiledMapRenderer.setView(cam);
  120.         orthogonalTiledMapRenderer.render();
  121.        
  122.         //set batch matrix to camera matrix
  123.         batch.setProjectionMatrix(cam.combined);
  124.        
  125.         //begin rendering
  126.         batch.begin();
  127.        
  128.         //draw the ship
  129.         batch.draw(
  130.                 player.getTexture(), //texture region
  131.                 player.getBody().getPosition().x, player.getBody().getPosition().y, //position
  132.                 -1 * (player.getTexture().getRegionWidth() / 2) * LevelScreen.WORLD_TO_BOX, -1 * (player.getTexture().getRegionWidth() / 2) * LevelScreen.WORLD_TO_BOX, //origin offset
  133.                 player.getTexture().getRegionWidth(), player.getTexture().getRegionHeight(), //width/height
  134.                 LevelScreen.WORLD_TO_BOX, LevelScreen.WORLD_TO_BOX, //scale
  135.                 player.getBody().getTransform().getRotation()
  136.         );
  137.  
  138.        
  139.         //done rendering
  140.         batch.end();
  141.     }
  142.    
  143.    
  144.     public OrthographicCamera getCamera(){
  145.         return cam;
  146.     }
  147.    
  148.     public void dispose(){
  149.         batch.dispose();
  150.     }
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. public abstract class Entity {
  159.    
  160.     protected BodyDef bodyDef;
  161.     protected Body body;
  162.        
  163.     protected TextureAtlas atlas;
  164.     protected TextureRegion texture;
  165.    
  166.     public LevelScreen screen;
  167.    
  168.    
  169.     public Entity(LevelScreen screen){
  170.         this.screen = screen;  
  171.     }
  172.    
  173.    
  174.     /*
  175.      * Set up box2d body
  176.      */
  177.     protected void createBody(Vector2 position, float width, float height, float density, float friction, float restitution, boolean isMoveable){
  178.         bodyDef = new BodyDef();
  179.         bodyDef.position.set(position.scl(LevelScreen.WORLD_TO_BOX));
  180.         body = screen.getBox2DWorld().createBody(bodyDef);
  181.         body.setUserData(this);
  182.        
  183.         if(isMoveable){
  184.             bodyDef.type = BodyType.DynamicBody;
  185.         }else{
  186.             bodyDef.type = BodyType.StaticBody;
  187.         }
  188.        
  189.         PolygonShape shape = new PolygonShape();
  190.         shape.setAsBox(width * LevelScreen.WORLD_TO_BOX, height * LevelScreen.WORLD_TO_BOX);
  191.  
  192.         FixtureDef fixtureDef = new FixtureDef();
  193.         fixtureDef.shape = shape;
  194.         fixtureDef.density = density;
  195.         fixtureDef.friction = friction;
  196.         fixtureDef.restitution = restitution;
  197.         Fixture fixture = body.createFixture(fixtureDef);
  198.        
  199.         shape.dispose();   
  200.     }
  201.    
  202.    
  203.    
  204.     /**
  205.      * @return the box2d body
  206.      */
  207.     public Body getBody(){
  208.         return body;
  209.     }
  210.    
  211. }
  212.  
  213.  
  214.  
  215.  
  216. public abstract class ActiveEntity extends Entity {
  217.  
  218.    
  219.     protected Vector2 startPosition;
  220.    
  221.     protected boolean isMovingUp;
  222.     protected boolean isMovingDown;
  223.     protected boolean isMovingLeft;
  224.     protected boolean isMovingRight;
  225.  
  226.    
  227.     public ActiveEntity(LevelScreen screen, Vector2 startPosition){
  228.         super(screen);
  229.        
  230.         this.startPosition = startPosition;
  231.  
  232.         this.isMovingUp = false;
  233.         this.isMovingDown = false;
  234.         this.isMovingLeft = false;
  235.         this.isMovingRight = false;
  236.        
  237.         this.defineBody();
  238.         this.defineEntity();
  239.     }
  240.    
  241.    
  242.     public void update(){
  243.         this.applyIntendedMovement();
  244.         Gdx.app.log(Outpost.LOG, String.valueOf(this.body.getMass()));
  245.     }
  246.    
  247.     public void applyIntendedMovement(){
  248.         if(this.isMovingUp){
  249.             this.body.applyForceToCenter(new Vector2(0,100).scl(this.SPEED_FACTOR), true);
  250.         }
  251.     }
  252.    
  253.    
  254.    
  255.    
  256.     public void setMovingUp(boolean isMovingUp){
  257.         this.isMovingUp = isMovingUp;
  258.     }
  259.    
  260.     public void setMovingDown(boolean isMovingDown){
  261.         this.isMovingDown = isMovingDown;
  262.     }
  263.    
  264.     public void setMovingLeft(boolean isMovingLeft){
  265.         this.isMovingLeft = isMovingLeft;
  266.     }
  267.    
  268.     public void setMovingRight(boolean isMovingRight){
  269.         this.isMovingRight = isMovingRight;
  270.     }
  271.        
  272. }
  273.  
  274.  
  275.  
  276.  
  277. public class Player extends ActiveEntity{
  278.    
  279.     public Player(LevelScreen screen, Vector2 startPosition) {
  280.         super(screen, startPosition);
  281.     }
  282.    
  283.     protected void defineBody(){
  284.        
  285.         float width = 0.5f;
  286.         float height = 0.5f;
  287.        
  288.         float density = 10f;
  289.         float friction = 0f;
  290.         float restitution = 1f;
  291.        
  292.         boolean isMoveable = true;
  293.        
  294.         this.createBody(this.startPosition, width, height, density, friction, restitution, isMoveable);
  295.     }
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement