Search in sources :

Example 1 with FixtureDef

use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef in project FXGL by AlmasB.

the class ScifiFactory method newPlayer.

@Spawns("player")
public Entity newPlayer(SpawnData data) {
    PhysicsComponent physics = new PhysicsComponent();
    physics.setFixtureDef(new FixtureDef().friction(0).density(0.25f));
    BodyDef bd = new BodyDef();
    bd.setFixedRotation(true);
    physics.setBodyDef(bd);
    physics.setBodyType(BodyType.DYNAMIC);
    return Entities.builder().from(data).type(ScifiType.PLAYER).bbox(new HitBox("main", BoundingShape.circle(15))).bbox(new HitBox("lower", new Point2D(15 - 5, 30), BoundingShape.box(10, 10))).with(physics, new CollidableComponent(true)).with(new PlayerControl()).build();
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) BodyDef(com.almasb.fxgl.physics.box2d.dynamics.BodyDef) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 2 with FixtureDef

use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef in project FXGL by AlmasB.

the class MarioFactory method newPlayer.

@Spawns("player")
public Entity newPlayer(SpawnData data) {
    PhysicsComponent physics = new PhysicsComponent();
    physics.setFixtureDef(new FixtureDef().friction(0).density(0.25f));
    physics.setGenerateGroundSensor(true);
    BodyDef bd = new BodyDef();
    bd.setFixedRotation(true);
    physics.setBodyDef(bd);
    physics.setBodyType(BodyType.DYNAMIC);
    return Entities.builder().from(data).type(MarioType.PLAYER).bbox(new HitBox("main", BoundingShape.circle(15))).bbox(new HitBox("lower", new Point2D(15 - 5, 30), BoundingShape.box(10, 10))).with(physics, new CollidableComponent(true)).with(new PlayerControl()).build();
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) CollidableComponent(com.almasb.fxgl.entity.component.CollidableComponent) BodyDef(com.almasb.fxgl.physics.box2d.dynamics.BodyDef) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 3 with FixtureDef

use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef in project FXGL by AlmasB.

the class RealPhysicsSample method createPhysicsEntity.

private Entity createPhysicsEntity() {
    // 1. create and configure physics component
    PhysicsComponent physics = new PhysicsComponent();
    physics.setBodyType(BodyType.DYNAMIC);
    physics.setFixtureDef(new FixtureDef().density(0.7f).restitution(0.3f));
    return Entities.builder().at(getInput().getMousePositionWorld()).with(physics).build();
}
Also used : PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 4 with FixtureDef

use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef in project FXGL by AlmasB.

the class FlyingKeysSample method createPhysicsEntity.

private Entity createPhysicsEntity() {
    // 1. create and configure physics component
    PhysicsComponent physics = new PhysicsComponent();
    physics.setBodyType(BodyType.DYNAMIC);
    FixtureDef fd = new FixtureDef();
    fd.setDensity(0.7f);
    fd.setRestitution(0.3f);
    physics.setFixtureDef(fd);
    return Entities.builder().at(getWidth() / 2, getHeight() / 2).with(physics).build();
}
Also used : PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 5 with FixtureDef

use of com.almasb.fxgl.physics.box2d.dynamics.FixtureDef in project FXGL by AlmasB.

the class JointSample method initGame.

@Override
protected void initGame() {
    getGameWorld().addEntity(Entities.makeScreenBounds(50));
    PhysicsComponent physics = new PhysicsComponent();
    Entity block = Entities.builder().at(200, 200).viewFromNodeWithBBox(new Rectangle(50, 50)).with(physics).buildAndAttach(getGameWorld());
    PhysicsComponent physics2 = new PhysicsComponent();
    physics2.setBodyType(BodyType.DYNAMIC);
    FixtureDef fd = new FixtureDef();
    fd.setDensity(1.0f);
    physics2.setFixtureDef(fd);
    Entity ball = Entities.builder().at(200, 300).bbox(new HitBox("main", BoundingShape.circle(15))).viewFromNode(getAssetLoader().loadTexture("ball.png", 30, 30)).with(physics2).buildAndAttach(getGameWorld());
    Line line = new Line();
    line.setStartX(block.getCenter().getX());
    line.setStartY(block.getCenter().getY());
    // line.startXProperty().bind(block.getPositionComponent().xProperty());
    // line.startYProperty().bind(block.getPositionComponent().yProperty());
    line.endXProperty().bind(ball.getPositionComponent().xProperty().add(15));
    line.endYProperty().bind(ball.getPositionComponent().yProperty().add(15));
    getGameScene().addGameView(new EntityView(line));
    RevoluteJointDef rDef = new RevoluteJointDef();
    rDef.bodyA = physics.getBody();
    rDef.bodyB = physics2.getBody();
    rDef.collideConnected = false;
    rDef.localAnchorA = new Vec2(0, 0);
    rDef.localAnchorB = new Vec2(0, 5);
    rDef.enableLimit = true;
    rDef.lowerAngle = (float) (FXGLMath.toRadians(-180.0f));
    rDef.upperAngle = (float) (FXGLMath.toRadians(180.0f));
    rDef.enableMotor = true;
    rDef.motorSpeed = (float) (FXGLMath.toRadians(30));
    rDef.maxMotorTorque = 15.0f;
    joint = (RevoluteJoint) getPhysicsWorld().getJBox2DWorld().createJoint(rDef);
}
Also used : Line(javafx.scene.shape.Line) Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) EntityView(com.almasb.fxgl.entity.view.EntityView) Vec2(com.almasb.fxgl.core.math.Vec2) Rectangle(javafx.scene.shape.Rectangle) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef) RevoluteJointDef(com.almasb.fxgl.physics.box2d.dynamics.joints.RevoluteJointDef)

Aggregations

PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)7 FixtureDef (com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)7 HitBox (com.almasb.fxgl.physics.HitBox)4 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)3 BodyDef (com.almasb.fxgl.physics.box2d.dynamics.BodyDef)3 Point2D (javafx.geometry.Point2D)3 Vec2 (com.almasb.fxgl.core.math.Vec2)1 Entity (com.almasb.fxgl.entity.Entity)1 EntityView (com.almasb.fxgl.entity.view.EntityView)1 RevoluteJointDef (com.almasb.fxgl.physics.box2d.dynamics.joints.RevoluteJointDef)1 Line (javafx.scene.shape.Line)1 Rectangle (javafx.scene.shape.Rectangle)1