Example usage for com.badlogic.gdx.physics.box2d Body setBullet

List of usage examples for com.badlogic.gdx.physics.box2d Body setBullet

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d Body setBullet.

Prototype

public void setBullet(boolean flag) 

Source Link

Document

Should this body be treated like a bullet for continuous collision detection?

Usage

From source file:com.me.main.startgame.java

License:Apache License

private Body createPlayer() {
    BodyDef def = new BodyDef();
    def.type = BodyType.DynamicBody;/*from  w  w w  .  j  a  v  a 2  s. c  om*/

    PolygonShape poly = new PolygonShape();
    poly.setAsBox(0.2f, 0.3f);

    FixtureDef sqrdef = new FixtureDef();
    sqrdef.shape = poly;
    sqrdef.density = 1;

    CircleShape circle = new CircleShape();
    circle.setRadius(0.2f);
    circle.setPosition(new Vector2(0, -0.25f));

    FixtureDef circledef = new FixtureDef();
    circledef.shape = circle;
    circledef.density = 1;

    Body box = world.createBody(def);
    scene.setCustom(box, "tipo", TIPOPLAYER);
    box.setBullet(true);
    playerPhysicsFixture = box.createFixture(sqrdef);
    playerSensorFixture = box.createFixture(circledef);

    return box;
}

From source file:com.pilot51.cannon.GameField.java

License:Apache License

private void addBall() {
    final Scene scene = mEngine.getScene();
    final FixtureDef ballFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0, false);
    final Sprite ball = new Sprite((-tCircle.getWidth() / 2) + 1, (-tCircle.getHeight() / 2) - 1, tCircle);
    ball.setVelocity(velocity * speed / ratio * (float) Math.cos(Math.toRadians(angle)),
            -velocity * speed / ratio * (float) Math.sin(Math.toRadians(angle)));
    ball.setScaleCenter(ball.getWidth() / 2, ball.getHeight() / 2);
    ball.setScale(ballRadius / (ball.getWidth() / 2));
    ball.setColor(Color.red(colorProj) / 255f, Color.green(colorProj) / 255f, Color.blue(colorProj) / 255f,
            Color.alpha(colorProj) / 255f);
    ball.setUpdatePhysics(false);//from w w  w  .  j av  a 2 s  .  co  m
    runOnUpdateThread(new Runnable() {
        @Override
        public void run() {
            final Body body = PhysicsFactory.createCircleBody(mPhysicsWorld, ball, BodyType.DynamicBody,
                    ballFixtureDef);
            body.setBullet(true);
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("sprite", ball);
            map.put("hits", (long) 0);
            map.put("shot", nShots++);
            body.setUserData(map);
            mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, body, true, true, true, false));
        }
    });
    addEntity(ball, 2, null);
    if (mRandom) {
        score -= 1;
        String txt = getString(R.string.hud_score, score);
        sText.setPosition(cameraWidth - 10 - txt.length() * fontSize * 0.6f, 10);
        sText.setText(txt);
    }
    if (Common.getPrefs().getBoolean("trail", false)) {
        trail(ball);
    }
    if (fuze > 0) {
        TimerTask remove = new TimerTask() {
            @Override
            public void run() {
                removeSprite(ball, 2);
            }
        };
        new Timer().schedule(remove, (long) (fuze / speed));
    }
    scene.registerUpdateHandler(new IUpdateHandler() {
        @Override
        public void onUpdate(float pSecondsElapsed) {
            int ballX = (int) (ball.getX() + (ball.getWidth() / 2));
            int ballY = (int) (ball.getY() + (ball.getHeight() / 2));
            // Remove ball if it leaves screen and definitely won't come back
            if ((gravity >= 0 & ballY > 0) | (wind <= 0 & ballX < 0) | (gravity <= 0 & ballY < -cameraHeight)
                    | (wind >= 0 & ballX > cameraWidth)) {
                scene.unregisterUpdateHandler(this);
                removeSprite(ball, 2);
            }
        }

        @Override
        public void reset() {
        }
    });
}