Example usage for com.badlogic.gdx.physics.box2d World rayCast

List of usage examples for com.badlogic.gdx.physics.box2d World rayCast

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d World rayCast.

Prototype

public void rayCast(final RayCastCallback callback, Vector2 point1, Vector2 point2) 

Source Link

Usage

From source file:com.agateau.pixelwheels.utils.ClosestBodyFinder.java

License:Open Source License

public Body find(World world, Vector2 origin, float angle) {
    mFraction = 1;/*from  w w w  .  j av  a  2 s . c o m*/
    mBody = null;
    for (float a = angle - mArc / 2; a <= angle + mArc / 2; a += ANGLE_BETWEEN_RAYS) {
        mTmp.set(mDepth, 0).rotate(a).add(origin);
        world.rayCast(this, origin, mTmp);
    }
    return mBody;
}

From source file:com.tnf.ptm.entities.planet.PlanetObjectsBuilder.java

License:Apache License

private void addDeco0(PtmGame game, float groundHeight, Vector2 planetPos, Map<Vector2, List<Dra>> collector,
        DecoConfig dc) {//from ww  w . j  a  v a2  s .  c  o  m
    World w = game.getObjMan().getWorld();
    ConsumedAngles consumed = new ConsumedAngles();

    final Vector2 rayCasted = new Vector2();
    RayCastCallback rcc = new RayCastCallback() {
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            if (!(fixture.getBody().getUserData() instanceof TileObject)) {
                return -1;
            }
            rayCasted.set(point);
            return fraction;
        }
    };

    int decoCount = (int) (2 * PtmMath.PI * groundHeight * dc.density);
    for (int i = 0; i < decoCount; i++) {
        float decoSz = PtmMath.rnd(dc.szMin, dc.szMax);
        float angularHalfWidth = PtmMath.angularWidthOfSphere(decoSz / 2, groundHeight);

        float decoAngle = 0;
        for (int j = 0; j < 5; j++) {
            decoAngle = PtmMath.rnd(180);
            if (!consumed.isConsumed(decoAngle, angularHalfWidth)) {
                consumed.add(decoAngle, angularHalfWidth);
                break;
            }
        }

        PtmMath.fromAl(rayCasted, decoAngle, groundHeight, true);
        rayCasted.add(planetPos);
        w.rayCast(rcc, rayCasted, planetPos);
        float decoDist = rayCasted.dst(planetPos);

        float baseAngle = PtmMath.windowCenter(decoAngle, DECO_PACK_ANGULAR_WIDTH);
        float baseDist = PtmMath.windowCenter(decoDist, DECO_PACK_SZ);
        Vector2 basePos = PtmMath.fromAl(baseAngle, baseDist).add(planetPos);
        Vector2 decoRelPos = new Vector2(rayCasted).sub(basePos);
        PtmMath.rotate(decoRelPos, -baseAngle - 90, true);
        float decoRelAngle = decoAngle - baseAngle;

        TextureAtlas.AtlasRegion decoTex = PtmMath.elemRnd(dc.texs);
        if (dc.allowFlip && PtmMath.test(.5f)) {
            decoTex = game.getTexMan().getFlipped(decoTex);
        }

        RectSprite s = new RectSprite(decoTex, decoSz, dc.orig.x, dc.orig.y, decoRelPos, DraLevel.DECO,
                decoRelAngle, 0, PtmColor.WHITE, false);
        List<Dra> ss = collector.get(basePos);
        if (ss == null) {
            ss = new ArrayList<Dra>();
            collector.put(new Vector2(basePos), ss);
        }
        ss.add(s);
        PtmMath.free(basePos);
    }
}

From source file:com.tnf.ptm.handler.input.SmallObjAvoider.java

License:Apache License

public float avoid(PtmGame game, PtmShip ship, float toDestAngle, Planet np) {
    myShip = ship;// w  w w.  j a v  a 2 s.  c  o  m
    Vector2 shipPos = ship.getPosition();
    float shipSpdLen = ship.getSpd().len();
    float ttt = ship.calcTimeToTurn(toDestAngle + 45);
    float raycastLen = shipSpdLen * (ttt + MANEUVER_TIME);
    if (raycastLen < MIN_RAYCAST_LEN) {
        raycastLen = MIN_RAYCAST_LEN;
    }

    PtmMath.fromAl(myDest, toDestAngle, raycastLen);
    myDest.add(shipPos);
    myCollided = false;
    World w = game.getObjMan().getWorld();
    w.rayCast(myRayBack, shipPos, myDest);
    if (!myCollided) {
        return toDestAngle;
    }

    toDestAngle += 45;
    PtmMath.fromAl(myDest, toDestAngle, raycastLen);
    myDest.add(shipPos);
    myCollided = false;
    w.rayCast(myRayBack, shipPos, myDest);
    if (!myCollided) {
        return toDestAngle;
    }

    toDestAngle -= 90;
    PtmMath.fromAl(myDest, toDestAngle, raycastLen);
    myDest.add(shipPos);
    myCollided = false;
    w.rayCast(myRayBack, shipPos, myDest);
    if (!myCollided) {
        return toDestAngle;
    }

    if (np.getFullHeight() < np.getPos().dst(shipPos)) {
        return toDestAngle - 45;
    }
    return PtmMath.angle(np.getPos(), shipPos);
}

From source file:org.destinationsol.game.input.SmallObjAvoider.java

License:Apache License

public float avoid(SolGame game, SolShip ship, float toDestAngle, Planet np) {
    myShip = ship;/*  w ww.j  a v a2  s.c o  m*/
    Vector2 shipPos = ship.getPosition();
    float shipSpdLen = ship.getSpd().len();
    float ttt = ship.calcTimeToTurn(toDestAngle + 45);
    float raycastLen = shipSpdLen * (ttt + MANEUVER_TIME);
    if (raycastLen < MIN_RAYCAST_LEN)
        raycastLen = MIN_RAYCAST_LEN;

    SolMath.fromAl(myDest, toDestAngle, raycastLen);
    myDest.add(shipPos);
    myCollided = false;
    World w = game.getObjMan().getWorld();
    w.rayCast(myRayBack, shipPos, myDest);
    if (!myCollided)
        return toDestAngle;

    toDestAngle += 45;
    SolMath.fromAl(myDest, toDestAngle, raycastLen);
    myDest.add(shipPos);
    myCollided = false;
    w.rayCast(myRayBack, shipPos, myDest);
    if (!myCollided)
        return toDestAngle;

    toDestAngle -= 90;
    SolMath.fromAl(myDest, toDestAngle, raycastLen);
    myDest.add(shipPos);
    myCollided = false;
    w.rayCast(myRayBack, shipPos, myDest);
    if (!myCollided)
        return toDestAngle;

    if (np.getFullHeight() < np.getPos().dst(shipPos))
        return toDestAngle - 45;
    return SolMath.angle(np.getPos(), shipPos);
}

From source file:org.destinationsol.game.planet.PlanetObjectsBuilder.java

License:Apache License

private void addDeco0(SolGame game, float groundHeight, Vector2 planetPos, Map<Vector2, List<Dra>> collector,
        DecoConfig dc) {/*from  w ww . j a  v  a2s .  co m*/
    World w = game.getObjMan().getWorld();
    ConsumedAngles consumed = new ConsumedAngles();

    final Vector2 rayCasted = new Vector2();
    RayCastCallback rcc = new RayCastCallback() {
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            if (!(fixture.getBody().getUserData() instanceof TileObject)) {
                return -1;
            }
            rayCasted.set(point);
            return fraction;
        }
    };

    int decoCount = (int) (2 * SolMath.PI * groundHeight * dc.density);
    for (int i = 0; i < decoCount; i++) {
        float decoSz = SolMath.rnd(dc.szMin, dc.szMax);
        float angularHalfWidth = SolMath.angularWidthOfSphere(decoSz / 2, groundHeight);

        float decoAngle = 0;
        for (int j = 0; j < 5; j++) {
            decoAngle = SolMath.rnd(180);
            if (!consumed.isConsumed(decoAngle, angularHalfWidth)) {
                consumed.add(decoAngle, angularHalfWidth);
                break;
            }
        }

        SolMath.fromAl(rayCasted, decoAngle, groundHeight, true);
        rayCasted.add(planetPos);
        w.rayCast(rcc, rayCasted, planetPos);
        float decoDist = rayCasted.dst(planetPos);

        float baseAngle = SolMath.windowCenter(decoAngle, DECO_PACK_ANGULAR_WIDTH);
        float baseDist = SolMath.windowCenter(decoDist, DECO_PACK_SZ);
        Vector2 basePos = SolMath.fromAl(baseAngle, baseDist).add(planetPos);
        Vector2 decoRelPos = new Vector2(rayCasted).sub(basePos);
        SolMath.rotate(decoRelPos, -baseAngle - 90, true);
        float decoRelAngle = decoAngle - baseAngle;

        TextureAtlas.AtlasRegion decoTex = SolMath.elemRnd(dc.texs);
        if (dc.allowFlip && SolMath.test(.5f))
            decoTex = game.getTexMan().getFlipped(decoTex);

        RectSprite s = new RectSprite(decoTex, decoSz, dc.orig.x, dc.orig.y, decoRelPos, DraLevel.DECO,
                decoRelAngle, 0, SolColor.W, false);
        List<Dra> ss = collector.get(basePos);
        if (ss == null) {
            ss = new ArrayList<Dra>();
            collector.put(new Vector2(basePos), ss);
        }
        ss.add(s);
        SolMath.free(basePos);
    }
}