Search in sources :

Example 1 with BoundingBoxComponent

use of com.almasb.fxgl.entity.component.BoundingBoxComponent in project FXGL by AlmasB.

the class PhysicsWorld method createBody.

/**
 * Create physics body and attach to physics world.
 *
 * @param e physics entity
 */
private void createBody(Entity e) {
    BoundingBoxComponent bbox = e.getBoundingBoxComponent();
    PhysicsComponent physics = e.getComponent(PhysicsComponent.class);
    double w = bbox.getWidth();
    double h = bbox.getHeight();
    // if position is 0, 0 then probably not set, so set ourselves
    if (physics.bodyDef.getPosition().x == 0 && physics.bodyDef.getPosition().y == 0) {
        physics.bodyDef.getPosition().set(toMetersF(bbox.getMinXWorld() + w / 2), toMetersF(appHeight - (bbox.getMinYWorld() + h / 2)));
    }
    if (physics.bodyDef.getAngle() == 0) {
        physics.bodyDef.setAngle((float) -Math.toRadians(e.getRotation()));
    }
    physics.body = jboxWorld.createBody(physics.bodyDef);
    createFixtures(e);
    if (physics.isGenerateGroundSensor()) {
        createGroundSensor(e);
    }
    physics.body.setUserData(e);
    physics.onInitPhysics();
    e.addControl(new PhysicsControl(this));
}
Also used : BoundingBoxComponent(com.almasb.fxgl.entity.component.BoundingBoxComponent)

Example 2 with BoundingBoxComponent

use of com.almasb.fxgl.entity.component.BoundingBoxComponent in project FXGL by AlmasB.

the class PhysicsWorld method createFixtures.

private void createFixtures(Entity e) {
    BoundingBoxComponent bbox = e.getBoundingBoxComponent();
    PhysicsComponent physics = e.getComponent(PhysicsComponent.class);
    PositionComponent position = e.getPositionComponent();
    Point2D entityCenter = bbox.getCenterWorld();
    for (HitBox box : bbox.hitBoxesProperty()) {
        Bounds bounds = box.translate(position.getX(), position.getY());
        // take world center bounds and subtract from entity center (all in pixels) to get local center
        Point2D boundsCenterWorld = new Point2D((bounds.getMinX() + bounds.getMaxX()) / 2, (bounds.getMinY() + bounds.getMaxY()) / 2);
        Point2D boundsCenterLocal = boundsCenterWorld.subtract(entityCenter);
        double w = bounds.getWidth();
        double h = bounds.getHeight();
        FixtureDef fd = physics.fixtureDef;
        Shape b2Shape;
        BoundingShape boundingShape = box.getShape();
        switch(boundingShape.type) {
            case CIRCLE:
                CircleShape circleShape = new CircleShape();
                circleShape.setRadius(toMetersF(w / 2));
                circleShape.m_p.set(toMetersF(boundsCenterLocal.getX()), -toMetersF(boundsCenterLocal.getY()));
                b2Shape = circleShape;
                break;
            case POLYGON:
                PolygonShape polygonShape = new PolygonShape();
                polygonShape.setAsBox(toMetersF(w / 2), toMetersF(h / 2), new Vec2(toMetersF(boundsCenterLocal.getX()), -toMetersF(boundsCenterLocal.getY())), 0);
                b2Shape = polygonShape;
                break;
            case CHAIN:
                if (physics.body.getType() != BodyType.STATIC) {
                    throw new IllegalArgumentException("BoundingShape.chain() can only be used with static objects");
                }
                Point2D[] points = (Point2D[]) boundingShape.data;
                Vec2[] vertices = new Vec2[points.length];
                for (int i = 0; i < vertices.length; i++) {
                    vertices[i] = toVector(points[i].subtract(boundsCenterLocal)).subLocal(toVector(bbox.getCenterLocal()));
                }
                ChainShape chainShape = new ChainShape();
                chainShape.createLoop(vertices, vertices.length);
                b2Shape = chainShape;
                break;
            case EDGE:
            default:
                log.warning("Unsupported hit box shape");
                throw new UnsupportedOperationException("Using unsupported shape: " + boundingShape.type);
        }
        // we use definitions from user, but override shape
        fd.setShape(b2Shape);
        Fixture fixture = physics.body.createFixture(fd);
        fixture.setUserData(box);
    }
}
Also used : PositionComponent(com.almasb.fxgl.entity.component.PositionComponent) Bounds(javafx.geometry.Bounds) BoundingBoxComponent(com.almasb.fxgl.entity.component.BoundingBoxComponent) Point2D(javafx.geometry.Point2D) Vec2(com.almasb.fxgl.core.math.Vec2)

Example 3 with BoundingBoxComponent

use of com.almasb.fxgl.entity.component.BoundingBoxComponent in project FXGL by AlmasB.

the class PhysicsWorld method createPhysicsParticles.

private void createPhysicsParticles(Entity e) {
    double x = e.getX();
    double y = e.getY();
    double width = e.getWidth();
    double height = e.getHeight();
    ParticleGroupDef def = e.getComponent(PhysicsParticleComponent.class).getDefinition();
    def.setPosition(toMetersF(x + width / 2), toMetersF(appHeight - (y + height / 2)));
    Shape shape = null;
    BoundingBoxComponent bbox = e.getBoundingBoxComponent();
    if (!bbox.hitBoxesProperty().isEmpty()) {
        if (bbox.hitBoxesProperty().get(0).getShape().type == ShapeType.POLYGON) {
            PolygonShape rectShape = new PolygonShape();
            rectShape.setAsBox(toMetersF(width / 2), toMetersF(height / 2));
            shape = rectShape;
        } else if (bbox.hitBoxesProperty().get(0).getShape().type == ShapeType.CIRCLE) {
            CircleShape circleShape = new CircleShape();
            circleShape.setRadius(toMetersF(width / 2));
            shape = circleShape;
        } else {
            log.warning("Unknown hit box shape: " + bbox.hitBoxesProperty().get(0).getShape().type);
            throw new UnsupportedOperationException();
        }
    }
    if (shape == null) {
        PolygonShape rectShape = new PolygonShape();
        rectShape.setAsBox(toMetersF(width / 2), toMetersF(height / 2));
        shape = rectShape;
    }
    def.setShape(shape);
    ParticleGroup particleGroup = jboxWorld.createParticleGroup(def);
    Color color = e.getComponent(PhysicsParticleComponent.class).getColor();
    e.addControl(new PhysicsParticleControl(particleGroup, color, this));
}
Also used : ParticleGroupDef(com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef) BoundingBoxComponent(com.almasb.fxgl.entity.component.BoundingBoxComponent) ParticleGroup(com.almasb.fxgl.physics.box2d.particle.ParticleGroup) Color(javafx.scene.paint.Color)

Aggregations

BoundingBoxComponent (com.almasb.fxgl.entity.component.BoundingBoxComponent)3 Vec2 (com.almasb.fxgl.core.math.Vec2)1 PositionComponent (com.almasb.fxgl.entity.component.PositionComponent)1 ParticleGroup (com.almasb.fxgl.physics.box2d.particle.ParticleGroup)1 ParticleGroupDef (com.almasb.fxgl.physics.box2d.particle.ParticleGroupDef)1 Bounds (javafx.geometry.Bounds)1 Point2D (javafx.geometry.Point2D)1 Color (javafx.scene.paint.Color)1