0
\$\begingroup\$

In my game i have a ball falling from above and a rectangle below. when my rectangle collide with ball,sprite position displaced from body automatically,i could not understand this. and i also tried moving Dynamic body using linear velocity it works fine but give problem after collision. here are picture before colliding and after collidingbefore collision

After collision

and my code is

  public class Ball extends ApplicationAdapter implements InputProcessor {
        SpriteBatch batch;
        Sprite sprite,sprite2;
        Texture img;
        Texture img1;
        World world;
        Body body,body2;
        Body ground;

        Matrix4 debugMatrix;
        OrthographicCamera camera;
        ExtendViewport viewport;
    Box2DDebugRenderer debugRenderer;

        float SCALE=0.05f;


        @Override
        public void create (){
            batch = new SpriteBatch();
            img = new Texture("ball.png");
            img1=new Texture("badlogic.jpg");
            camera=new OrthographicCamera();
            viewport= new ExtendViewport(50,50,camera);


            sprite = new Sprite(img);
            sprite2 = new Sprite(img1);
            sprite.setSize(sprite.getWidth()*SCALE,sprite.getHeight()*SCALE);
            sprite2.setSize(sprite2.getWidth()*SCALE,sprite2.getHeight()*SCALE);

            world = new World(new Vector2(0, -10f),true);
             createBall();
            createRect();
            debugRenderer = new Box2DDebugRenderer();
            Gdx.input.setInputProcessor(this);
        }
        @Override
        public void resize(int width, int height) {
            viewport.update(width, height, true);

        batch.setProjectionMatrix(camera.combined);
            createGround();
        }

        private void createBall(){
            BodyDef bodyDef = new BodyDef();
            bodyDef.type = BodyDef.BodyType.DynamicBody;
            bodyDef.position.set(MathUtils.random(15,30),50);
             CircleShape circleShape=new CircleShape();
            circleShape.setRadius(1.2f);
            body = world.createBody(bodyDef);
            FixtureDef fixtureDef = new FixtureDef();
            fixtureDef.shape = circleShape;
            fixtureDef.density = 0.1f;
            fixtureDef.restitution = 0.2f;
            fixtureDef.friction=0.2f;
            body.createFixture(fixtureDef);
            circleShape.dispose();
        }

        private void createRect(){
            BodyDef bodyDef2 = new BodyDef();
            bodyDef2.type = BodyDef.BodyType.DynamicBody;
            bodyDef2.position.set(25,10);
            body2 = world.createBody(bodyDef2);
            FixtureDef fixtureDef2 = new FixtureDef();
            PolygonShape shape = new PolygonShape();
            shape.setAsBox(sprite2.getWidth()/2, sprite2.getHeight()/2);
            fixtureDef2.shape = shape;
            fixtureDef2.density = 0.1f;
            fixtureDef2.restitution = 0.7f;
            fixtureDef2.friction=0.2f;
            body2.createFixture(fixtureDef2);
            shape.dispose();
        }


            private void createGround() {

                BodyDef bodyDef = new BodyDef();
                bodyDef.type = BodyDef.BodyType.StaticBody;
                FixtureDef fixtureDef = new FixtureDef();
                PolygonShape shape = new PolygonShape();
                shape.setAsBox(camera.viewportWidth, 1);
                fixtureDef.shape = shape;
                ground = world.createBody(bodyDef);
                ground.createFixture(fixtureDef);
                ground.setTransform(0, 0, 0);

                shape.dispose();


            }
        @Override
        public void dispose() {
            img.dispose();
            world.dispose();
        }




        @Override
        public void render () {
            Gdx.gl.glClearColor(1, 1, 1, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            world.step(1f/60f, 6, 2);
            sprite.setPosition(body.getPosition().x-sprite.getWidth()/2,
                    body.getPosition().y-sprite.getHeight()/2 );

            sprite.setRotation((float)Math.toDegrees(body2.getAngle()));
            sprite2.setPosition(body2.getPosition().x - sprite2.getWidth()/2,
                    body2.getPosition().y -sprite2.getHeight()/2 );
            sprite2.setRotation((float)Math.toDegrees(body.getAngle()));


            batch.begin();
            batch.draw(sprite, sprite.getX(), sprite.getY(),sprite.getOriginX(),
                    sprite.getOriginY(),
                    sprite.getWidth(),sprite.getHeight(),sprite.getScaleX(),sprite.
                            getScaleY(),sprite.getRotation());
            batch.draw(sprite2, sprite2.getX(), sprite2.getY(),sprite2.getOriginX(),
                    sprite2.getOriginY(),
                    sprite2.getWidth(),sprite2.getHeight(),sprite2.getScaleX(),sprite2.
                        getScaleY(),sprite2.getRotation());
            batch.end();
            debugRenderer.render(world, camera.combined);
        }


        @Override
        public boolean keyDown(int keycode) {
            if(keycode== Input.Keys.LEFT)        //get some linear velocity
                body2.setLinearVelocity(-10f,0);
            if(keycode==Input.Keys.RIGHT)
                body2.setLinearVelocity(10f,5);
            return true;
        }

        @Override
        public boolean keyUp(int keycode) {
            if(keycode== Input.Keys.LEFT)   
                body2.setLinearVelocity(0,0);
            if(keycode==Input.Keys.RIGHT)
                body2.setLinearVelocity(0,0);
            return true;
        }

        @Override
        public boolean keyTyped(char character) {
            return false;
        }

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            return false;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            return false;
        }

        @Override
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            return false;
        }

        @Override
        public boolean mouseMoved(int screenX, int screenY) {
            return false;
        }

        @Override
        public boolean scrolled(int amount) {
            return false;
        }
    }
\$\endgroup\$
-1
\$\begingroup\$

finally got this problem solved by just adding this line of code to the bodies

body.setFixedRotation=true;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.