Search in sources :

Example 1 with PositionComponent

use of com.almasb.fxgl.entity.component.PositionComponent 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)

Aggregations

Vec2 (com.almasb.fxgl.core.math.Vec2)1 BoundingBoxComponent (com.almasb.fxgl.entity.component.BoundingBoxComponent)1 PositionComponent (com.almasb.fxgl.entity.component.PositionComponent)1 Bounds (javafx.geometry.Bounds)1 Point2D (javafx.geometry.Point2D)1