Java 类com.badlogic.gdx.physics.box2d.World 实例源码

项目:homescreenarcade    文件:FieldElement.java   
/**
 * Extracts common values from the definition parameter map, and calls finishCreate to allow
 * subclasses to further initialize themselves. Subclasses should override finishCreate, and
 * should not override this method.
 */
public void initialize(Map<String, ?> params, FieldElementCollection collection, World world)
        throws DependencyNotAvailableException {
    this.parameters = params;
    this.box2dWorld = world;
    this.elementID = (String)params.get(ID_PROPERTY);

    @SuppressWarnings("unchecked")
    List<Number> colorList = (List<Number>)params.get(COLOR_PROPERTY);
    if (colorList!=null) {
        this.initialColor = Color.fromList(colorList);
    }

    if (params.containsKey(SCORE_PROPERTY)) {
        this.score = ((Number)params.get(SCORE_PROPERTY)).longValue();
    }

    this.finishCreateElement(params, collection);
    this.createBodies(world);
}
项目:homescreenarcade    文件:FlipperElement.java   
@Override public void createBodies(World world) {
    this.anchorBody = Box2DFactory.createCircle(world, this.cx, this.cy, 0.05f, true);
    // Joint angle is 0 when flipper is horizontal.
    // The flipper needs to be slightly extended past anchorBody to rotate correctly.
    float ext = (this.flipperLength > 0) ? -0.05f : +0.05f;
    // Width larger than 0.12 slows rotation?
    this.flipperBody = Box2DFactory.createWall(world, cx+ext, cy-0.12f, cx+flipperLength, cy+0.12f, 0f);
    flipperBody.setType(BodyDef.BodyType.DynamicBody);
    flipperBody.setBullet(true);
    flipperBody.getFixtureList().get(0).setDensity(5.0f);

    jointDef = new RevoluteJointDef();
    jointDef.initialize(anchorBody, flipperBody, new Vector2(this.cx, this.cy));
    jointDef.enableLimit = true;
    jointDef.enableMotor = true;
    // counterclockwise rotations are positive, so flip angles for flippers extending left
    jointDef.lowerAngle = (this.flipperLength>0) ? this.minangle : -this.maxangle;
    jointDef.upperAngle = (this.flipperLength>0) ? this.maxangle : -this.minangle;
    jointDef.maxMotorTorque = 1000f;

    this.joint = (RevoluteJoint)world.createJoint(jointDef);

    flipperBodySet = Collections.singletonList(flipperBody);
    this.setEffectiveMotorSpeed(-this.downspeed); // Force flipper to bottom when field is first created.
}
项目:homescreenarcade    文件:Box2DFactory.java   
/** Creates a circle object with the given position and radius. Resitution defaults to 0.6. */
public static Body createCircle(World world, float x, float y, float radius, boolean isStatic) {
    CircleShape sd = new CircleShape();
    sd.setRadius(radius);

    FixtureDef fdef = new FixtureDef();
    fdef.shape = sd;
    fdef.density = 1.0f;
    fdef.friction = 0.3f;
    fdef.restitution = 0.6f;

    BodyDef bd = new BodyDef();
    bd.allowSleep = true;
    bd.position.set(x, y);
    Body body = world.createBody(bd);
    body.createFixture(fdef);
    if (isStatic) {
        body.setType(BodyDef.BodyType.StaticBody);
    }
    else {
        body.setType(BodyDef.BodyType.DynamicBody);
    }
    return body;
}
项目:homescreenarcade    文件:Box2DFactory.java   
/**
 * Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax),
 * and rotating the box counterclockwise through the given angle, with specified restitution.
 */
public static Body createWall(World world, float xmin, float ymin, float xmax, float ymax,
        float angle, float restitution) {
    float cx = (xmin + xmax) / 2;
    float cy = (ymin + ymax) / 2;
    float hx = Math.abs((xmax - xmin) / 2);
    float hy = Math.abs((ymax - ymin) / 2);
    PolygonShape wallshape = new PolygonShape();
    // Don't set the angle here; instead call setTransform on the body below. This allows future
    // calls to setTransform to adjust the rotation as expected.
    wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), 0f);

    FixtureDef fdef = new FixtureDef();
    fdef.shape = wallshape;
    fdef.density = 1.0f;
    if (restitution>0) fdef.restitution = restitution;

    BodyDef bd = new BodyDef();
    bd.position.set(cx, cy);
    Body wall = world.createBody(bd);
    wall.createFixture(fdef);
    wall.setType(BodyDef.BodyType.StaticBody);
    wall.setTransform(cx, cy, angle);
    return wall;
}
项目:homescreenarcade    文件:FieldLayout.java   
void initFromLevel(Map<String, ?> layoutMap, World world) {
    this.width = asFloat(layoutMap.get(WIDTH_PROPERTY), 20.0f);
    this.height = asFloat(layoutMap.get(HEIGHT_PROPERTY), 30.0f);
    this.gravity = asFloat(layoutMap.get(GRAVITY_PROPERTY), 4.0f);
    this.targetTimeRatio = asFloat(layoutMap.get(TARGET_TIME_RATIO_PROPERTY));
    this.numberOfBalls = asInt(layoutMap.get(NUM_BALLS_PROPERTY), 3);
    this.ballRadius = asFloat(layoutMap.get(BALL_RADIUS_PROPERTY), 0.5f);
    this.ballColor = colorFromMap(layoutMap, BALL_COLOR_PROPERTY, DEFAULT_BALL_COLOR);
    this.secondaryBallColor = colorFromMap(
            layoutMap, SECONDARY_BALL_COLOR_PROPERTY, DEFAULT_SECONDARY_BALL_COLOR);
    this.launchPosition = asFloatList(listForKey(layoutMap, LAUNCH_POSITION_PROPERTY));
    this.launchVelocity = asFloatList(listForKey(layoutMap, LAUNCH_VELOCITY_PROPERTY));
    this.launchVelocityRandomDelta = asFloatList(
            listForKey(layoutMap, LAUNCH_RANDOM_VELOCITY_PROPERTY));
    this.launchDeadZoneRect = asFloatList(listForKey(layoutMap, LAUNCH_DEAD_ZONE_PROPERTY));

    this.allParameters = layoutMap;
    this.fieldElements = createFieldElements(layoutMap, world);
}
项目:feup-lpoo-armadillo    文件:B2DFactory.java   
/**
 * Creates a pivoted platform from the given object, at the given world.
 *
 * @param world  The world were the platform will be.
 * @param object The object used to initialize the platform.
 * @return The Platform Model of the created platform.
 */
static PlatformModel makePlatform(World world, RectangleMapObject object) {
    PlatformModel platform = new PlatformModel(world, object);

    Boolean pivoted = object.getProperties().get("pivoted", boolean.class);
    if (pivoted != null && pivoted == true) {
        Rectangle rect = object.getRectangle();
        RectangleMapObject anchorRect = new RectangleMapObject(
                rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2, 1, 1
        );
        Body anchor = makeRectGround(world, anchorRect);

        RevoluteJointDef jointDef = new RevoluteJointDef();
        jointDef.initialize(anchor, platform.getBody(), platform.getBody().getWorldCenter());
        world.createJoint(jointDef);
    }

    return platform;
}
项目:feup-lpoo-armadillo    文件:B2DFactory.java   
/**
 * Creates polygon ground from the given object, at the given world.
 *
 * @param world  The world were the ground will be.
 * @param object The object used to initialize the ground.
 * @return The body of the created ground.
 */
static Body makePolygonGround(World world, PolygonMapObject object) {
    Polygon polygon = object.getPolygon();

    // Body and Fixture variables
    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();

    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(PIXEL_TO_METER * polygon.getX(), PIXEL_TO_METER * polygon.getY());

    Body body = world.createBody(bdef);

    float[] new_vertices = polygon.getVertices().clone();
    for (int i = 0; i < new_vertices.length; i++)
        new_vertices[i] *= PIXEL_TO_METER;

    shape.set(new_vertices);
    fdef.shape = shape;
    fdef.filter.categoryBits = GROUND_BIT;
    body.createFixture(fdef);

    return body;
}
项目:feup-lpoo-armadillo    文件:B2DFactory.java   
/**
 * Creates rectangular ground from the given object, at the given world.
 *
 * @param world  The world were the ground will be.
 * @param object The object used to initialize the ground.
 * @return The body of the created ground.
 */
static Body makeRectGround(World world, RectangleMapObject object) {
    Rectangle rect = object.getRectangle();

    // Body and Fixture variables
    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();

    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(PIXEL_TO_METER * (rect.getX() + rect.getWidth() / 2), PIXEL_TO_METER * (rect.getY() + rect.getHeight() / 2));

    Body body = world.createBody(bdef);

    shape.setAsBox((rect.getWidth() / 2) * PIXEL_TO_METER, (rect.getHeight() / 2) * PIXEL_TO_METER);
    fdef.shape = shape;
    fdef.filter.categoryBits = GROUND_BIT;
    body.createFixture(fdef);

    return body;
}
项目:feup-lpoo-armadillo    文件:B2DFactory.java   
/**
 * Creates a PowerUp from the given object, at the given world.
 *
 * @param world  The world were the PowerUp will be.
 * @param object The object used to initialize the PowerUp.
 * @return The created PowerUp.
 */
static PowerUp makePowerUp(World world, RectangleMapObject object) {
    String classProperty = object.getProperties().get("class", String.class);
    if (classProperty == null) {
        System.err.println("PowerUp has no class set!");
        return null;
    }

    switch (classProperty) {
        case "gravity":
            return new GravityPowerUp(world, object);
        case "velocity":
            return new SpeedPowerUp(world, object);
        case "jump":
            return new JumpPowerUp(world, object);
        default:
            return new RandomPowerUp(world, object);
    }
}
项目:feup-lpoo-armadillo    文件:BoxModel.java   
/**
 * Box's Constructor.
 * Creates a Box in the given with world, from the given object.
 *
 * @param world  Physics world where the box will be in.
 * @param object Object used to create the box.
 */
public BoxModel(World world, RectangleMapObject object) {
    super(world, object.getRectangle().getCenter(new Vector2()).scl(PIXEL_TO_METER), ModelType.BOX, ANGULAR_DAMP, LINEAR_DAMP);

    // Fetch density from properties
    Float property = object.getProperties().get("density", float.class);
    if (property != null)
        density = property;

    // Create Fixture's Shape
    Shape shape = createPolygonShape(new float[]{
            0, 0, 64, 0, 64, 64, 0, 64
    }, new Vector2(64, 64));

    createFixture(new FixtureProperties(shape, density, FRICTION, RESTITUTION, GROUND_BIT, (short) (BALL_BIT | GROUND_BIT | FLUID_BIT)));
}
项目:feup-lpoo-armadillo    文件:WaterModel.java   
/**
 * Water Model's constructor.
 * Creates a water model from the given object, into the given world.
 *
 * @param world The world the water model will be in.
 * @param rect  The rectangle to create the water model with.
 */
public WaterModel(World world, Rectangle rect) {
    // Body and Fixture variables
    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();

    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(PIXEL_TO_METER * (rect.getX() + rect.getWidth() / 2), PIXEL_TO_METER * (rect.getY() + rect.getHeight() / 2));

    this.body = world.createBody(bdef);

    shape.setAsBox((rect.getWidth() / 2) * PIXEL_TO_METER, (rect.getHeight() / 2) * PIXEL_TO_METER);
    fdef.shape = shape;
    fdef.filter.categoryBits = FLUID_BIT;
    fdef.isSensor = true;
    fdef.density = 1f;
    fdef.friction = 0.1f;
    fdef.restitution = 0f;
    body.createFixture(fdef);

    body.setUserData(new BuoyancyController(world, body.getFixtureList().first()));

}
项目:GDX-Engine    文件:PhysicsManager.java   
public void loadStaticBodies(String bodylayer, TiledMap tiledMap) {

    world = new World(new Vector2(0, -5), true);
    // loop each object group.
    // a object group is all objects in a layer.
    for (int i = 0; i < tiledMap.objectGroups.size(); i++) {
        TiledObjectGroup group = tiledMap.objectGroups.get(i);

        // Find the object group of "static" layer
        if (bodylayer.equals(group.name)) {
        // foreach objects in this group
        for (int j = 0; j < group.objects.size(); j++) {
            TiledObject object = group.objects.get(j);
            // create static body from the object
            createStaticBody(object);
        }
        }
    }
    }
项目:GDX-Engine    文件:PhysicsTiledScene.java   
protected void loadStaticBodies(String bodylayer) {

        world = new World(new Vector2(0, -5), true);
        // loop each object group.
        // a object group is all objects in a layer.
        for (int i = 0; i < tiledMap.objectGroups.size(); i++) {
            TiledObjectGroup group = tiledMap.objectGroups.get(i);

            // Find the object group of "static" layer
            if (bodylayer.equals(group.name)) {
                // foreach objects in this group
                for (int j = 0; j < group.objects.size(); j++) {
                    TiledObject object = group.objects.get(j);
                    // create static body from the object
                    createStaticBody(object);
                }
            }
        }
    }
项目:summer17-android    文件:InteractiveTileObject.java   
public InteractiveTileObject(World world, TiledMap map, Rectangle bounds){
    this.world = world;
    this.map = map;
    this.bounds = bounds;

    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();

    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / NoObjectionGame.PPM, (bounds.getY() + bounds.getHeight() / 2 )/ NoObjectionGame.PPM);

    body = world.createBody(bdef);
    shape.setAsBox(bounds.getWidth() / 2 / NoObjectionGame.PPM, bounds.getHeight() / 2 / NoObjectionGame.PPM);
    fdef.shape = shape;
    fixture = body.createFixture(fdef);
}
项目:FlappySpinner    文件:WorldUtils.java   
public static Body createSpinner(World world) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(Constants.SPINNER_POSITIONS);
    Body body = world.createBody(bodyDef);
    CircleShape circle = new CircleShape();
    circle.setRadius(Constants.SPINNER_SIZE);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = Constants.SPENNER_DENSITY;
    fixtureDef.friction = 0f;
    fixtureDef.restitution = 0.45f;
    body.createFixture(fixtureDef);
    circle.dispose();
    return body;
}
项目:FlappySpinner    文件:WorldUtils.java   
public static Array<Body> createTopTube(World world, float posX) {

        float posY = generateTubePosition();

        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.KinematicBody;
        bodyDef.position.set(posX, posY);
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(Constants.TUBE_WIDTH / 2, Constants.TUBE_HEIGHT / 2);
        Body bodyTop = world.createBody(bodyDef);
        bodyTop.createFixture(shape, 0f);
        bodyTop.resetMassData();
        shape.dispose();
        bodyTop.setLinearVelocity(Constants.TUBE_SPEED, 0.0f);
        Array<Body> bodies = new Array<Body>();
        bodies.add(bodyTop);
        bodies.add(createBottomTube(world, posX, posY));
        return bodies;
    }
项目:FlappySpinner    文件:MenuScreen.java   
public MenuScreen(Game game) {
    Box2D.init();
    this.game = game;
    world = new World(new Vector2(0, 0), false);
    stage = new Stage(new StretchViewport(Constants.WIDTH, Constants.HEIGHT));

    preferences = new GamePreferences();
    audioManager = AudioManager.getInstance();
    Gdx.input.setInputProcessor(stage);

    FlappySpinner.gameManager.changeBackgroundColor("#4ec0ca");

    setUpBackground();
    setUpButtons();
    setUpLogo();
}
项目:libGdx-xiyou    文件:FireBallController.java   
public void update(World world) {

        for (int i = 0; i < balls.size; i++) {
            FireBall ball = balls.get(i);

            //检查火球速度异常情况
            if (ball.getBody().getLinearVelocity().x == 0.2f && ball.STATE == State.STATE_RIGHT_FIREBALL) {
                ball.getBody().setLinearVelocity(1.0f, 0);
            } else if (ball.getBody().getLinearVelocity().x == -0.2f && ball.STATE == State.STATE_RIGHT_FIREBALL) {
                ball.getBody().setLinearVelocity(1.0f, 0);
            } else if (ball.getBody().getLinearVelocity().x == 0.2f && ball.STATE == State.STATE_LEFT_FIREBALL) {
                ball.getBody().setLinearVelocity(-1.0f, 0);
            } else if (ball.getBody().getLinearVelocity().x == -0.2f && ball.STATE == State.STATE_LEFT_FIREBALL) {
                ball.getBody().setLinearVelocity(-1.0f, 0);
            }

            //检查是否Alive
            if (!checkAlive(ball)) {
                world.destroyBody(ball.getBody());
                balls.removeValue(ball, true);
            }
        }
    }
项目:QuackHack    文件:Box.java   
public Box(World world, MapObject object){
    super(Assets.getAtlas().findRegion("boxCrate_double"));
    Rectangle rect = ((RectangleMapObject) object).getRectangle();
    bdef.type = BodyDef.BodyType.DynamicBody;
    bdef.position.set((rect.getX()+rect.getWidth()/2)/ QuackHack.PPM, (rect.getY() + rect.getHeight()/2)/QuackHack.PPM); // I don't follow the math
    body = world.createBody(bdef);

    shape.setAsBox(rect.getWidth() / 2 / QuackHack.PPM, rect.getHeight() / 2 / QuackHack.PPM);
    fdef.shape = shape;
    fdef.friction = 0.4f;
    fdef.density = 0.1f;
    body.createFixture(fdef);

    boxRegion = new TextureRegion(getTexture(), 0, 0, 128, 128);
    setBounds(0,0, 128 / QuackHack.PPM, 128 / QuackHack.PPM);
    setRegion(boxRegion);
    setOrigin(getHeight()/2, getWidth()/2);
}
项目:QuackHack    文件:Player.java   
public Player (Integer id, World world, PlayScreen screen, PlayerType type){
    this.type = type;
    System.out.println("t: "+type.toString());
    setBounds(0, 0, 128 / QuackHack.PPM, 128 / QuackHack.PPM);
    setRegion(Assets.getAtlas().findRegion(type.toString()));
    setOrigin(getHeight() / 2, getWidth() / 2);

    this.id = id;
    this.world = world;
    touchingGround = false;
    isGoing = false;

    definePlayer();
    // Now we do the bounds for how large to render it
    defineLights();
}
项目:cgc-game    文件:ChainGame.java   
@Override
public void onEvent(int type, BaseTween<?> source)
{
    transitioning = false;
    CGCWorld.clearWorld();
    world = new World(new Vector2(0, 0), true);
    lh.createLayers();
    maps.clear();
    Array<Player> newPlayerArray = new Array<Player>();
    for (int i = 0; i < CGCWorld.numPlayers; i++)
    {
        CGCWorld.players.get(i).setInBossFight(true);
        newPlayerArray.add(CGCWorld.players.get(i));
    }
    myApp.setScreen(new BossFight(myApp, CGCWorld.getNumPlayers(), newPlayerArray, transition, tutorial));
}
项目:Undertailor    文件:WorldObject.java   
/**
 * Called once the parent room is ready to create a body
 * for this {@link WorldObject}.
 * 
 * @param world the World to create a body with
 */
void createBody(World world) {
    // hold the pixel-based positions
    float pxX = this.def.position.x;
    float pxY = this.def.position.y;

    // set to meters
    this.def.position.set(pxX * OverworldController.PIXELS_TO_METERS,
        pxY * OverworldController.PIXELS_TO_METERS);
    this.body = world.createBody(this.def);
    this.body.setUserData(this);

    if (this.boundingQueue != null) {
        this.boundingQueue.forEach(shape -> {
            this.body.createFixture(shape, 0F);
            shape.dispose();
        });

        this.boundingQueue = null;
    }

    // back to holding the pixel pos
    this.def.position.set(pxX, pxY);
}
项目:advio    文件:Food.java   
public Food(World world, float x, float y, int size, float toGive) {
    super(x, y);
    color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random(), 1);
    outline = Advio.instance.darker(color);
    BodyDef bd = new BodyDef();
    bd.type = BodyType.KinematicBody;
    bd.position.set(x, y);
    CircleShape shape = new CircleShape();
    shape.setRadius(size);
    FixtureDef fd = new FixtureDef();
    fd.density = 0.1f;
    fd.shape = shape;
    fd.friction = 0.1f;
    // fd.filter.groupIndex = Advio.GROUP_BEINGS;
    body = world.createBody(bd);
    body.createFixture(fd).setUserData(new UserData("food").add("toGive", toGive));
    shape.dispose();
}
项目:advio    文件:Block.java   
public Block(World world, float x, float f, BodyType type, boolean invisible) {
    BodyDef bd = new BodyDef();
    bd.type = type;
    bd.position.set(x, f);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(size / 2, size / 2);
    FixtureDef fd = new FixtureDef();
    fd.density = 0.1f;
    fd.shape = shape;
    fd.friction = 0.1f;
    fd.filter.groupIndex = Advio.GROUP_DONT_COLLIDE_WITH_PLAYER;
    // fd.filter.groupIndex = Advio.GROUP_SCENERY;
    body = world.createBody(bd);
    body.createFixture(fd).setUserData(new UserData("block"));
    this.invisible = invisible;
    shape.dispose();
}
项目:advio    文件:PressurePlate.java   
public PressurePlate(Level l, World world, float x, float y, float weight, Block[] blocks) {
    super(x, y);
    this.level = l;
    BodyDef bd = new BodyDef();
    bd.type = BodyType.KinematicBody;
    bd.position.set(x, y);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(Block.size / 2, Block.size / 2);
    FixtureDef fd = new FixtureDef();
    fd.density = 0.0f;
    fd.shape = shape;
    fd.friction = 0.1f;
    fd.isSensor = true;
    this.blocks = new ArrayList<Block>(Arrays.asList(blocks));
    body = world.createBody(bd);
    body.createFixture(fd).setUserData(new UserData("plate").add("pressed", false).add("weight", weight));
    shape.dispose();
    this.world = world;
}
项目:advio    文件:Player.java   
public Player(World world, float x, float y) {
    super(x, y);
    this.world = world;
    this.graphicsSize = 10;
    color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random(), 1);
    outline = Advio.instance.darker(color);
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DynamicBody;
    bd.position.set(x, y);
    CircleShape shape = new CircleShape();
    shape.setRadius(50);
    FixtureDef fd = new FixtureDef();
    fd.density = 0.0f;
    fd.shape = shape;
    fd.friction = 0.1f;
    body = world.createBody(bd);
    body.createFixture(fd).setUserData(new UserData("player"));
    shape.dispose();
    // size * 2.25f
}
项目:advio    文件:AgarLogic.java   
public AgarLogic(Level l, World world, float x, float y) {
    super(x, y);
    this.level = l;
    BodyDef bd = new BodyDef();
    bd.type = BodyType.KinematicBody;
    bd.position.set(x, y);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(Block.size / 2, Block.size / 2);
    FixtureDef fd = new FixtureDef();
    fd.density = 0.0f;
    fd.shape = shape;
    fd.friction = 0.1f;
    fd.isSensor = true;
    body = world.createBody(bd);
    body.createFixture(fd).setUserData(new UserData("agariologic"));
    shape.dispose();
    this.world = world;
}
项目:advio    文件:Enemy.java   
public Enemy(World world, float x, float y, int size) {
    super(x, y);
    this.world = world;
    this.graphicsSize = 0.1f;
    color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random(), 1);
    outline = Advio.instance.darker(color);
    BodyDef bd = new BodyDef();
    bd.type = BodyType.DynamicBody;
    bd.position.set(x, y);
    CircleShape shape = new CircleShape();
    shape.setRadius(size);
    FixtureDef fd = new FixtureDef();
    fd.density = 0.0f;
    fd.shape = shape;
    fd.friction = 0.1f;
    body = world.createBody(bd);
    body.createFixture(fd).setUserData(new UserData("enemy").add("body", body));
    shape.dispose();
}
项目:advio    文件:Goal.java   
public Goal(World world, float x, float y) {
    super(x, y);
    BodyDef bd = new BodyDef();
    bd.type = BodyType.KinematicBody;
    bd.position.set(x, y);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(Block.size / 2, Block.size / 2);
    FixtureDef fd = new FixtureDef();
    fd.density = 0.1f;
    fd.shape = shape;
    fd.friction = 0.1f;
    fd.isSensor = true;
    // fd.filter.groupIndex = Advio.GROUP_SCENERY;
    body = world.createBody(bd);
    body.createFixture(fd).setUserData(new UserData("goal"));
    shape.dispose();
}
项目:fabulae    文件:GameMap.java   
/**
 * Recreates all disposables.
 */
public void undispose() {
    if (!isDisposed) {
        return;
    }
    box2DDebugRenderer = new Box2DDebugRenderer(true, true, true, true, true, true);
    fogOfWarWorld = new World(new Vector2(0, 0), true);
    lightsWorld = new World(new Vector2(0, 0), true);
    createFogOfWarRayHandler();
    createLightsRayHandler();
    createViewConesRayHandler();

    isDisposed = false;

    for (int i = 0; i < gameObjects.size; ++i) {
        gameObjects.get(i).undispose();
    }

    for (Entry<TrapLocation, FilledPolygonRenderer> entry : traps.entries()) {
        traps.put(entry.key, entry.key.createRenderer());
    }
}
项目:libgdxjam    文件:PlayerSystem.java   
private void setStance(Entity entity, String stance) {
    logger.info("set stance: " + stance);

    PhysicsComponent physics = Mappers.physics.get(entity);
    PlayerComponent player = Mappers.player.get(entity);
    AssetManager assetManager = Env.getGame().getAssetManager();

    World world = physicsSystem.getWorld();

    if (world.getBodyCount() > 0 && 
            physics.body != null) {
        world.destroyBody(physics.body);
    }

    PhysicsData physicsData = assetManager.get(stance, PhysicsData.class);
    physics.body = physicsData.createBody(world, entity);

    Array<Fixture> fixtures = physics.body.getFixtureList();
    player.fixture = fixtures.get(physicsData.getFixtureIdx("main"));
    player.feetSensor = fixtures.get(physicsData.getFixtureIdx("feet"));

    NodeComponent node = Mappers.node.get(entity);
    NodeUtils.computeWorld(entity);

    physics.body.setTransform(node.position, node.angle);
}
项目:METEORIC    文件:WorldEdge.java   
public WorldEdge(World world, WorldEdgeType type){
     BodyDef edgeBody = new BodyDef();
     edgeBody.type = BodyDef.BodyType.StaticBody;

     if (type.equals(WorldEdgeType.Right)){
         edgeBody.position.set(GameMain.WIDTH / GameMain.PIXELS_TO_METERS, 0);
     }else{
         edgeBody.position.set(0, 0);
     }

     FixtureDef edgeDef = new FixtureDef();
     edgeDef.filter.categoryBits = GameMain.WORLD_ENTITY;
     edgeDef.filter.maskBits = GameMain.METEOR_ENTITY;

     EdgeShape edge = new EdgeShape();
     edge.set(0, 0, 0, GameMain.HEIGHT);
     edgeDef.shape = edge;

     body = world.createBody(edgeBody);
     body.createFixture(edgeDef);
     edge.dispose();
}
项目:braingdx    文件:GameContext2DImpl.java   
public GameContext2DImpl(ViewportFactory viewportFactory, ShaderConfig shaderConfig) {
    camera = new OrthographicCamera();
    world = new GameWorld(camera);
    behaviorManager = new BehaviorManager();
    batch = new SpriteBatch();
    input = new InputMultiplexer();
    boxWorld = new World(Vector2.Zero, false);
    lightingManager = new LightingManager(boxWorld, camera);
    renderManager = new GameObjectRenderManager(batch);
    gameCamera = new VectorGameCamera(camera);
    particleManager = new ParticleManager();
    stage = new Stage(viewportFactory.create(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
    Viewport worldStageViewport = viewportFactory.create(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    worldStageViewport.setCamera(camera);
    worldStage = new Stage(worldStageViewport);
    renderPipeline = new CombinedRenderPipelineFactory(shaderConfig, world, lightingManager, stage, worldStage, viewportFactory)
            .create();
    tiledMapManager = new TiledMapManagerImpl(behaviorManager, world, renderManager);
    wire();
}
项目:Bomberman_libGdx    文件:BombSystem.java   
private boolean checkMovable(Body body, Vector2 from, Vector2 to) {
    World b2dWorld = body.getWorld();
    moveable = true;

    RayCastCallback rayCastCallback = new RayCastCallback() {
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            if (fixture.getFilterData().categoryBits == GameManager.INDESTRUCTIIBLE_BIT
                    | fixture.getFilterData().categoryBits == GameManager.BREAKABLE_BIT
                    | fixture.getFilterData().categoryBits == GameManager.BOMB_BIT
                    | fixture.getFilterData().categoryBits == GameManager.ENEMY_BIT
                    | fixture.getFilterData().categoryBits == GameManager.PLAYER_BIT) {
                moveable = false;
                return 0;
            }
            return 0;
        }
    };

    b2dWorld.rayCast(rayCastCallback, from, to);
    return moveable;
}
项目:Entitas-Java    文件:RendererSystem.java   
public RendererSystem(Entitas entitas, Camera cam, Batch batch, World world) {
    this.physics = world;
    this.cam = cam;
    this.batch = batch;
    this.entitas = entitas;
    _groupTextureView = entitas.game.getGroup(GameMatcher.TextureView());
    this.debugShapeRenderer = new ShapeRenderer();
    this.debugRenderer = new Box2DDebugRenderer(DRAW_BOX2D_BODIES, DRAW_BOX2D_JOINTS, DRAW_BOX2D_ABBs,
            DRAW_BOX2D_INACTIVE_BODIES, DRAW_BOX2D_VELOCITIES, DRAW_BOX2D_CONTACTS);
    debugRenderer.setDrawAABBs(DRAW_BOX2D_ABBs);
    debugRenderer.setDrawBodies(DRAW_BOX2D_BODIES);
    debugRenderer.setDrawContacts(DRAW_BOX2D_CONTACTS);
    debugRenderer.setDrawInactiveBodies(DRAW_BOX2D_INACTIVE_BODIES);
    debugRenderer.setDrawJoints(DRAW_BOX2D_JOINTS);
    debugRenderer.setDrawVelocities(DRAW_BOX2D_VELOCITIES);
    this.inputs = entitas.input.getGroup(InputMatcher.Input());
}
项目:SupaBax    文件:BodyBuilder.java   
/**
 * 
 * @param world
 * @param ellipseObject
 * @param density
 * @param friction
 * @param restitution
 */
private void createEllipse(World world, EllipseMapObject ellipseObject, float density, float friction, float restitution){
    Ellipse circle = ellipseObject.getEllipse();
    CircleShape shape = new CircleShape();
    shape.setRadius(circle.width / 2f / SupaBox.PPM);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.StaticBody;
    bodyDef.position.set(new Vector2((circle.x + circle.width / 2f) / SupaBox.PPM, (circle.y + circle.width / 2f) / SupaBox.PPM));

    Body body = world.createBody(bodyDef);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = density;
    fixtureDef.friction = friction;
    fixtureDef.restitution = restitution;

    body.createFixture(fixtureDef);

    shape.dispose();
}
项目:Vector-Pinball-Editor    文件:FieldElement.java   
/**
 * Extracts common values from the definition parameter map, and calls finishCreate to allow
 * subclasses to further initialize themselves. Subclasses should override finishCreate, and
 * should not override this method.
 */
public void initialize(Map<String, ?> params, FieldElementCollection collection, World world)
        throws DependencyNotAvailableException {
    this.parameters = params;
    this.box2dWorld = world;
    this.elementID = (String)params.get(ID_PROPERTY);

    @SuppressWarnings("unchecked")
    List<Number> colorList = (List<Number>)params.get(COLOR_PROPERTY);
    if (colorList!=null) {
        this.initialColor = Color.fromList(colorList);
    }

    if (params.containsKey(SCORE_PROPERTY)) {
        this.score = ((Number)params.get(SCORE_PROPERTY)).longValue();
    }

    this.finishCreateElement(params, collection);
    this.createBodies(world);
}
项目:Vector-Pinball-Editor    文件:Box2DFactory.java   
/** Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax), and rotating the box counterclockwise
* through the given angle, with specified restitution.
*/
  public static Body createWall(World world, float xmin, float ymin, float xmax, float ymax, float angle, float restitution) {
    float cx = (xmin + xmax) / 2;
    float cy = (ymin + ymax) / 2;
      float hx = Math.abs((xmax - xmin) / 2);
      float hy = Math.abs((ymax - ymin) / 2);
    PolygonShape wallshape = new PolygonShape();
      // Don't set the angle here; instead call setTransform on the body below. This allows future
      // calls to setTransform to adjust the rotation as expected.
      wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), 0f);

FixtureDef fdef = new FixtureDef();
fdef.shape = wallshape;
fdef.density = 1.0f;
if (restitution>0) fdef.restitution = restitution;

    BodyDef bd = new BodyDef();
    bd.position.set(cx, cy);
    Body wall = world.createBody(bd);
    wall.createFixture(fdef);
    wall.setType(BodyDef.BodyType.StaticBody);
      wall.setTransform(cx, cy, angle);
    return wall;
  }
项目:SupaBax    文件:BodyBuilder.java   
/**
 * 
 * @param world
 * @param rectangleObject
 * @param density
 * @param friction
 * @param restitution
 */
private void createRectangle(World world, RectangleMapObject rectangleObject, float density, float friction, float restitution){
    Rectangle rect = rectangleObject.getRectangle();
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(rect.width / SupaBox.PPM / 2f, rect.height / SupaBox.PPM / 2f);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.StaticBody;
    bodyDef.position.set(new Vector2((rect.x + rect.width / 2f) / SupaBox.PPM, (rect.y + rect.height / 2f) / SupaBox.PPM));

    Body body = world.createBody(bodyDef);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = density;
    fixtureDef.friction = friction;
    fixtureDef.restitution = restitution;

    body.createFixture(fixtureDef);

    shape.dispose();
}
项目:Vector-Pinball-Editor    文件:FieldLayout.java   
void initFromLevel(Map<String, Object> layoutMap, World world) {
    this.width = asFloat(layoutMap.get(WIDTH_PROPERTY), 20.0f);
    this.height = asFloat(layoutMap.get(HEIGHT_PROPERTY), 30.0f);
    this.gravity = asFloat(layoutMap.get(GRAVITY_PROPERTY), 4.0f);
    this.targetTimeRatio = asFloat(layoutMap.get(TARGET_TIME_RATIO_PROPERTY));
    this.numberOfBalls = asInt(layoutMap.get(NUM_BALLS_PROPERTY), 3);
    this.ballRadius = asFloat(layoutMap.get(BALL_RADIUS_PROPERTY), 0.5f);
    this.ballColor = colorFromMap(layoutMap, BALL_COLOR_PROPERTY, DEFAULT_BALL_COLOR);
    this.secondaryBallColor = colorFromMap(
            layoutMap, SECONDARY_BALL_COLOR_PROPERTY, DEFAULT_SECONDARY_BALL_COLOR);
    this.launchPosition = asFloatList(listForKey(layoutMap, LAUNCH_POSITION_PROPERTY));
    this.launchVelocity = asFloatList(listForKey(layoutMap, LAUNCH_VELOCITY_PROPERTY));
    this.launchVelocityRandomDelta = asFloatList(listForKey(layoutMap, LAUNCH_RANDOM_VELOCITY_PROPERTY));
    this.launchDeadZoneRect = asFloatList(listForKey(layoutMap, LAUNCH_DEAD_ZONE_PROPERTY));

    this.allParameters = layoutMap;
    this.fieldElements = createFieldElements(layoutMap, world);
}