Search code examples
javaandroidlibgdxbox2d

How to create movingPlatform?


I want to create platform that moving right/left with box2d, but I don't know how?

I create platform-body and gave him KinematicType, and trying to move it with setLinearVelocity, but it doesn't help

This is how I create platform "bucket":

package com.niceboy.game.Objects;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

import java.util.Random;

import static com.niceboy.game.Constants.PLATFORM_MARGIN;
import static com.niceboy.game.Constants.PPM;

public class Bucket {
  private Random rand = new Random();

  private World world;
  private Body body;

  public Bucket(World world){
      this.world = world;
  }

  public void createBucket(int y){
      BodyDef bdef = new BodyDef();    
      bdef.position.set(rand.nextInt(Gdx.graphics.getWidth())/PPM,PLATFORM_MARGIN/PPM*y);
      bdef.type = BodyDef.BodyType.KinematicBody;

      body = world.createBody(bdef);

      PolygonShape box = new PolygonShape();
      box.setAsBox(50/PPM,20/PPM);
      FixtureDef fdef = new FixtureDef();
      fdef.shape = box;

      body.createFixture(fdef).setUserData("bucket");
  }

  public void repos(int y){
    body.getPosition().set(rand.nextInt(Gdx.graphics.getWidth())/PPM,PLATFORM_MARGIN/PPM*y);
  }
}

But Idon't know how to move it


Solution

  • kinematicBody->setLinearVelocity(1,0); //move right 1 unit per second

    I've created KinematicBody body and give him PolygonShape and apply linear velocity than body start moving, when you want to change direction apply same velocity in negative magnitude so that it start moving in opposite direction.

    public class MainGame extends InputAdapter implements ApplicationListener {
    
        private SpriteBatch batch;
        private ExtendViewport extendViewport;
        private OrthographicCamera cam;
    
        private float w=20;
        private float h=22;
    
        private World world;
        private Box2DDebugRenderer debugRenderer;
    
        private Array<Body> array;
        private Vector3 vector3;
        private Body platform;
        Vector2 vector2;
        boolean isLeft;
    
        @Override
        public void create() {
    
            vector2=new Vector2();
            isLeft=true;
            cam=new OrthographicCamera();
            extendViewport=new ExtendViewport(w,h,cam);
    
            batch =new SpriteBatch();
            Gdx.input.setInputProcessor(this);
    
            world=new World(new Vector2(0,-9.8f),true);
            array=new Array<Body>();
            debugRenderer=new Box2DDebugRenderer();
            vector3=new Vector3();
    
            BodyDef bodyDef=new BodyDef();
            bodyDef.type= BodyDef.BodyType.KinematicBody;
            bodyDef.position.set(0,0);
            platform=world.createBody(bodyDef);
    
            PolygonShape polygonShape=new PolygonShape();
            polygonShape.setAsBox(3,1);
    
            FixtureDef fixtureDef=new FixtureDef();
            fixtureDef.shape=polygonShape;
            fixtureDef.restitution=.5f;
            platform.createFixture(fixtureDef);
            polygonShape.dispose();
            platfrom.setLinearVelocity(1,0);
        }
    
        @Override
        public void render() {
    
            Gdx.gl.glClearColor(0,1,1,1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
            world.step(1/60f,6,2);
            batch.setProjectionMatrix(cam.combined);
            batch.begin();
    
            world.getBodies(array);
            for (Body body:array){
                if(body.getUserData()!=null) {
                    Sprite sprite = (Sprite) body.getUserData();
                    sprite.setPosition(body.getPosition().x-sprite.getWidth()/2, body.getPosition().y-sprite.getHeight()/2);
                    sprite.setRotation(body.getAngle()*MathUtils.radDeg);
                    sprite.draw(batch);
                }
            }
            batch.end();
            debugRenderer.render(world,cam.combined);
    
    
            Vector2 pos=platfrom.getTransform().getPosition();
            if(pos.x>20-3) {
                platfrom.setLinearVelocity(-1,0);
            }
            if(pos.x<3) {
                platfrom.setLinearVelocity(1,0);
            }
    
        }
    
        @Override
        public void pause() {
    
        }
    
        @Override
        public void resume() {
    
        }
    
        @Override
        public void resize(int width, int height) {
    
            extendViewport.update(width,height);
            cam.position.x = w /2;
            cam.position.y = h/2;
            cam.update();
        }
    
        private void createPhysicsObject(float x,float y){
    
            float sizeX=0.5f,sizeY=0.5f;
            BodyDef bodyDef=new BodyDef();
            bodyDef.position.set(x,y);
            bodyDef.type= BodyDef.BodyType.DynamicBody;
    
            Body body=world.createBody(bodyDef);
    
            PolygonShape polygonShape=new PolygonShape();
            polygonShape.setAsBox(sizeX,sizeY);
            FixtureDef fixtureDef=new FixtureDef();
            fixtureDef.shape=polygonShape;
            fixtureDef.restitution=.2f;
            fixtureDef.density=2;
    
            body.createFixture(fixtureDef);
            body.setFixedRotation(false);
            polygonShape.dispose();
    
            Sprite sprite=new Sprite(new Texture("badlogic.jpg"));
            sprite.setSize(2*sizeX,2*sizeY);
            sprite.setPosition(x-sprite.getWidth()/2,y-sprite.getHeight()/2);
            sprite.setOrigin(sizeX,sizeY);
    
            body.setUserData(sprite);
        }
    
        @Override
        public void dispose() {
            batch.dispose();
            debugRenderer.dispose();
            world.dispose();
        }
    
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    
            vector3.set(screenX,screenY,0);
            Vector3 position=cam.unproject(vector3);
            createPhysicsObject(vector3.x,vector3.y);
    
            return false;
        }
    }