Search in sources :

Example 16 with PhysicsComponent

use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.

the class PlatformerSample method createPlatforms.

private void createPlatforms() {
    Entities.builder().at(0, 500).viewFromNode(new Rectangle(120, 100, Color.GRAY)).bbox(new HitBox("Main", BoundingShape.chain(new Point2D(0, 0), new Point2D(120, 0), new Point2D(120, 100), new Point2D(0, 100)))).with(new PhysicsComponent()).buildAndAttach(getGameWorld());
    Entities.builder().at(180, 500).viewFromNode(new Rectangle(400, 100, Color.GRAY)).bbox(new HitBox("Main", BoundingShape.chain(new Point2D(0, 0), new Point2D(400, 0), new Point2D(400, 100), new Point2D(0, 100)))).with(new PhysicsComponent()).buildAndAttach(getGameWorld());
}
Also used : HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle)

Example 17 with PhysicsComponent

use of com.almasb.fxgl.physics.PhysicsComponent 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 18 with PhysicsComponent

use of com.almasb.fxgl.physics.PhysicsComponent 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 19 with PhysicsComponent

use of com.almasb.fxgl.physics.PhysicsComponent 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)

Example 20 with PhysicsComponent

use of com.almasb.fxgl.physics.PhysicsComponent in project FXGL by AlmasB.

the class PhysicsParticlesSample method initGround.

private void initGround() {
    Entity ground = new Entity();
    ground.getPositionComponent().setValue(100, 500);
    ground.getViewComponent().setView(new EntityView(new Rectangle(800, 100)), true);
    ground.addComponent(new PhysicsComponent());
    getGameWorld().addEntity(ground);
}
Also used : Entity(com.almasb.fxgl.entity.Entity) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) EntityView(com.almasb.fxgl.entity.view.EntityView) Rectangle(javafx.scene.shape.Rectangle)

Aggregations

PhysicsComponent (com.almasb.fxgl.physics.PhysicsComponent)32 FixtureDef (com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)19 HitBox (com.almasb.fxgl.physics.HitBox)18 Point2D (javafx.geometry.Point2D)13 Rectangle (javafx.scene.shape.Rectangle)12 CollidableComponent (com.almasb.fxgl.entity.component.CollidableComponent)5 BodyDef (com.almasb.fxgl.physics.box2d.dynamics.BodyDef)5 Entity (com.almasb.fxgl.entity.Entity)4 ExpireCleanComponent (com.almasb.fxgl.dsl.components.ExpireCleanComponent)3 EntityView (com.almasb.fxgl.entity.view.EntityView)2 Input (com.almasb.fxgl.input.Input)2 UserAction (com.almasb.fxgl.input.UserAction)2 Circle (javafx.scene.shape.Circle)2 Polygon (javafx.scene.shape.Polygon)2 Vec2 (com.almasb.fxgl.core.math.Vec2)1 OffscreenCleanComponent (com.almasb.fxgl.dsl.components.OffscreenCleanComponent)1 ProjectileComponent (com.almasb.fxgl.dsl.components.ProjectileComponent)1 Spawns (com.almasb.fxgl.entity.Spawns)1 CollidableComponent (com.almasb.fxgl.entity.components.CollidableComponent)1 EffectControl (com.almasb.fxgl.entity.control.EffectControl)1