Search in sources :

Example 31 with PhysicsComponent

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

the class RevoluteJointSample method initGame.

@Override
protected void initGame() {
    getGameScene().setBackgroundColor(Color.LIGHTGRAY);
    entityBuilder().buildScreenBoundsAndAttach(50);
    // platform
    entityBuilder().at(400, 400).viewWithBBox(new Rectangle(500, 20, Color.BROWN)).with(new PhysicsComponent()).buildAndAttach();
    PhysicsComponent physics = new PhysicsComponent();
    physics.setFixtureDef(new FixtureDef().density(1.1f));
    physics.setBodyType(BodyType.DYNAMIC);
    Entity block = entityBuilder().at(600, 100).viewWithBBox(new Rectangle(80, 50)).with(physics).buildAndAttach();
    PhysicsComponent physics2 = new PhysicsComponent();
    physics2.setBodyType(BodyType.DYNAMIC);
    FixtureDef fd = new FixtureDef();
    fd.setDensity(1.0f);
    physics2.setFixtureDef(fd);
    Entity ball1 = entityBuilder().at(600, 360).bbox(new HitBox("main", BoundingShape.circle(15))).view(texture("ball.png", 30, 30)).with(physics2).buildAndAttach();
    PhysicsComponent physics3 = new PhysicsComponent();
    physics3.setBodyType(BodyType.DYNAMIC);
    physics3.setFixtureDef(fd);
    Entity ball2 = entityBuilder().at(700, 360).bbox(new HitBox("main", BoundingShape.circle(15))).view(texture("ball.png", 30, 30)).with(physics3).buildAndAttach();
    physics2.getBody().setAngularDamping(1f);
    physics3.getBody().setAngularDamping(1f);
    getPhysicsWorld().addRevoluteJoint(block, ball1, new Point2D(80, 50), new Point2D(15, 15));
    getPhysicsWorld().addRevoluteJoint(block, ball2, new Point2D(0, 50), new Point2D(15, 15));
}
Also used : Entity(com.almasb.fxgl.entity.Entity) HitBox(com.almasb.fxgl.physics.HitBox) PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

Example 32 with PhysicsComponent

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

the class RevoluteJointSample method createPhysicsEntity.

private Entity createPhysicsEntity() {
    PhysicsComponent physics = new PhysicsComponent();
    physics.setBodyType(BodyType.DYNAMIC);
    physics.setFixtureDef(new FixtureDef().density(0.1f).restitution(0.3f));
    return entityBuilder().at(getInput().getMousePositionWorld()).with(physics).build();
}
Also used : PhysicsComponent(com.almasb.fxgl.physics.PhysicsComponent) FixtureDef(com.almasb.fxgl.physics.box2d.dynamics.FixtureDef)

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