当前位置: 首页>>代码示例>>Java>>正文


Java Body类代码示例

本文整理汇总了Java中com.badlogic.gdx.physics.box2d.Body的典型用法代码示例。如果您正苦于以下问题:Java Body类的具体用法?Java Body怎么用?Java Body使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Body类属于com.badlogic.gdx.physics.box2d包,在下文中一共展示了Body类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createRevoluteJoint

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
/**
 * create Revolute Joint
 */
public RevoluteJoint createRevoluteJoint(Body bodyA, Body bodyB,
		Vector2 jointPositionA, Vector2 jointPositionB,
		boolean enabledMotor, int maxMotorTorque, int motorSpeed) {
	RevoluteJointDef rJoint = new RevoluteJointDef();
	rJoint.bodyA = bodyA;
	rJoint.bodyB = bodyB;
	rJoint.localAnchorA.set(jointPositionA.x * WORLD_TO_BOX,
			jointPositionA.y * WORLD_TO_BOX);
	rJoint.localAnchorB.set(jointPositionB.x * WORLD_TO_BOX,
			jointPositionB.y * WORLD_TO_BOX);

	rJoint.enableMotor = enabledMotor;
	rJoint.maxMotorTorque = maxMotorTorque;
	rJoint.motorSpeed = motorSpeed;

	RevoluteJoint joint = (RevoluteJoint) world.createJoint(rJoint);
	return joint;
}
 
开发者ID:game-libgdx-unity,项目名称:GDX-Engine,代码行数:22,代码来源:PhysicsTiledScene.java

示例2: createBody

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
private void createBody() {
	BodyDef bodyDef = new BodyDef();
	bodyDef.type = BodyType.DynamicBody;
	
	// Set our body's starting position in the world
	bodyDef.position.set(Gdx.input.getX() / 100f, camera.viewportHeight - Gdx.input.getY() / 100f);
	
	// Create our body in the world using our body definition
	Body body = world.createBody(bodyDef);

	// Create a circle shape and set its radius to 6
	PolygonShape square = new PolygonShape();
	square.setAsBox(0.3f, 0.3f);

	// Create a fixture definition to apply our shape to
	FixtureDef fixtureDef = new FixtureDef();
	fixtureDef.shape = square;
	fixtureDef.density = 0.5f;
	fixtureDef.friction = 0.5f;
	fixtureDef.restitution = 0.5f;

	// Create our fixture and attach it to the body
	body.createFixture(fixtureDef);

	square.dispose();
}
 
开发者ID:jocasolo,项目名称:water2d-libgdx,代码行数:27,代码来源:GameMain.java

示例3: handleCollision

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
@Override public void handleCollision(Ball ball, Body bodyHit, Field field) {
    if (retractWhenHit) {
        this.setRetracted(true);
    }

    if (killBall) {
        field.removeBall(ball);
    }
    else {
        Vector2 impulse = this.impulseForBall(ball);
        if (impulse!=null) {
            ball.applyLinearImpulse(impulse);
            flashForFrames(3);
        }
    }
}
 
开发者ID:StringMon,项目名称:homescreenarcade,代码行数:17,代码来源:WallElement.java

示例4: createCircle

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
/** 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;
}
 
开发者ID:StringMon,项目名称:homescreenarcade,代码行数:25,代码来源:Box2DFactory.java

示例5: createWall

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:StringMon,项目名称:homescreenarcade,代码行数:29,代码来源:Box2DFactory.java

示例6: createStaticAttackBody

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
/**
 * Erzeugt automatisch einen Kreis als statischen Trigger.
 *
 * @see AttackHandler#createAttackFixture(Body, Shape)
 *
 * @param position der Mittelpunkt des Kreises
 * @param radius der Radius des Kreises
 */
public void createStaticAttackBody(Vector2 position, float radius)
{
    CircleShape circle = new CircleShape();

    circle.setPosition(new Vector2(position.x, position.y).scl(Physics.MPP));
    circle.setRadius(radius * Physics.MPP);

    BodyDef triggerDef = new BodyDef();
    triggerDef.type = BodyDef.BodyType.StaticBody;

    Body trigger = manager.getPhysicalWorld().createBody(triggerDef);

    createAttackFixture(trigger, circle);

    circle.dispose();
}
 
开发者ID:Entwicklerpages,项目名称:school-game,代码行数:25,代码来源:AttackHandler.java

示例7: processCollision

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
@Override public void processCollision(Field field, FieldElement element, Body hitBody, Ball ball) {
    // When center red bumper is hit, start multiball if all center rollovers are lit,
    // otherwise retract left barrier.
    String elementID = element.getElementId();
    if ("CenterBumper1".equals(elementID)) {
        WallElement barrier = (WallElement)field.getFieldElementById("LeftTubeBarrier");
        RolloverGroupElement multiballRollovers =
                (RolloverGroupElement)field.getFieldElementById("ExtraBallRollovers");

        if (multiballRollovers.allRolloversActive()) {
            barrier.setRetracted(false);
            startMultiball(field);
            multiballRollovers.setAllRolloversActivated(false);
        }
        else {
            // don't retract during multiball
            if (field.getBalls().size()==1) {
                barrier.setRetracted(true);
            }
        }
    }
}
 
开发者ID:StringMon,项目名称:homescreenarcade,代码行数:23,代码来源:Field2Delegate.java

示例8: processCollision

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
@Override public void processCollision(Field field, FieldElement element, Body hitBody, Ball ball) {
    // Add bumper bonus if active.
    if (element instanceof BumperElement) {
        double extraEnergy = 0;
        if (bumperBonusActive) {
            double fractionRemaining = 1 - (((double) bumperBonusNanosElapsed) / bumperBonusDurationNanos);
            extraEnergy = fractionRemaining * bumperBonusMultiplier;
            // Round score to nearest multiple of 10.
            double bonusScore = element.getScore() * extraEnergy;
            field.addScore(10 * (Math.round(bonusScore / 10)));
        }
        bumperEnergy = Math.min(bumperEnergy + 1 + extraEnergy, maxBumperEnergy);
        double ratio = (bumperEnergy) / maxBumperEnergy;
        field.getFieldElementById("BumperIndicator").setNewColor(colorForTemperatureRatio(ratio));
        checkForEnableMultiball(field);
    }
}
 
开发者ID:StringMon,项目名称:homescreenarcade,代码行数:18,代码来源:Field3Delegate.java

示例9: makePlatform

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:AndreFCruz,项目名称:feup-lpoo-armadillo,代码行数:26,代码来源:B2DFactory.java

示例10: makePolygonGround

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:AndreFCruz,项目名称:feup-lpoo-armadillo,代码行数:32,代码来源:B2DFactory.java

示例11: makeRectGround

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:AndreFCruz,项目名称:feup-lpoo-armadillo,代码行数:28,代码来源:B2DFactory.java

示例12: jump

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
/**
 * Logic associated to the Ball.
 * Controls the Ball's current state, making it jump if on the ground, making it dunk if flying.
 */
public void jump() {
    Body body = getBody();
    switch (this.state) {
        case LANDED:
            body.applyLinearImpulse(new Vector2(0, jump_force), body.getWorldCenter(), true);
            this.state = State.FLYING;
            break;
        case FLYING:
            body.applyLinearImpulse(new Vector2(0, -1.5f * jump_force), body.getWorldCenter(), true);
            this.state = State.DUNKING;
            break;
        case DUNKING:
            // Do nothing
            break;
        default:
            System.err.println("Unhandled state in BallEntity");
    }
}
 
开发者ID:AndreFCruz,项目名称:feup-lpoo-armadillo,代码行数:23,代码来源:BallModel.java

示例13: createStaticCircleBody

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
/**
    * Create static body from a TiledObject Refer the box2d manual to
    * understand the static body
    * 
    * @param o
    *            TiledObject
    */
   public Body createStaticCircleBody(float x, float y, float radius) {
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.type = BodyType.StaticBody;
// transform into box2d
x = x * WORLD_TO_BOX;
// get position-y of object from map
y = Gdx.graphics.getHeight() - y;
// transform into box2d
y = y * WORLD_TO_BOX;

groundBodyDef.position.set(x, y);
Body groundBody = world.createBody(groundBodyDef);
CircleShape shape = new CircleShape();
((CircleShape) shape).setRadius(radius * WORLD_TO_BOX / 2);
groundBody.createFixture(shape, 0.0f);
groundBody.setUserData("static");
return groundBody;
   }
 
开发者ID:game-libgdx-unity,项目名称:GDX-Engine,代码行数:26,代码来源:PhysicsManager.java

示例14: applyForce

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
/**
    * Apply a force to physics object with a force apply position
    * 
    * @param data
    * @param force
    * @param applyPoint
    */
   protected void applyForce(IPhysicsObject data, Vector2 force,
    Vector2 applyPoint) {
Iterator<Body> bi = world.getBodies();
while (bi.hasNext()) {
    Body b = bi.next();
    IPhysicsObject e;
    try {
	e = (IPhysicsObject) b.getUserData(); // get the IPhysicsObject
    } catch (Exception ex) {
	e = null;
    }
    if (e != null) {
	if (e == data) {
	    if (applyPoint == null)
		b.applyForceToCenter(force);
	    else
		b.applyForce(force, applyPoint);
	}
    }
}
   }
 
开发者ID:game-libgdx-unity,项目名称:GDX-Engine,代码行数:29,代码来源:PhysicsManager.java

示例15: createTempBody

import com.badlogic.gdx.physics.box2d.Body; //导入依赖的package包/类
public Body createTempBody(float x, float y, FixtureDef fixtureDef) {
	// Dynamic Body
	BodyDef bodyDef = new BodyDef();
	if (box2dDebug)
		bodyDef.type = BodyType.StaticBody;
	else
		bodyDef.type = BodyType.DynamicBody;

	// transform into box2d
	x = x * WORLD_TO_BOX;
	y = y * WORLD_TO_BOX;

	bodyDef.position.set(x, y);

	Body body = world.createBody(bodyDef);

	Shape shape = new CircleShape();
	((CircleShape) shape).setRadius(1 * WORLD_TO_BOX);

	if (fixtureDef == null)
		throw new GdxRuntimeException("fixtureDef cannot be null!");
	fixtureDef.shape = shape;
	body.createFixture(fixtureDef);
	return body;
}
 
开发者ID:game-libgdx-unity,项目名称:GDX-Engine,代码行数:26,代码来源:PhysicsTiledScene.java


注:本文中的com.badlogic.gdx.physics.box2d.Body类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。