private void checkBottomCollision() { float raySize = -(verticalSpeed+Gdx.graphics.getDeltaTime())*Gdx.graphics.getDeltaTime(); if(raySize < 5f) raySize = 5f; if(verticalSpeed > 0) return; final Vector2 rayFrom = new Vector2((item.getX() + item.getWidth() / 2) * PhysicsBodyLoader.SCALE, (item.getY() + item.getHeight()/2) * PhysicsBodyLoader.SCALE); final Vector2 rayTo = new Vector2((item.getX() + item.getWidth() / 2) * PhysicsBodyLoader.SCALE, (item.getY() - raySize) * PhysicsBodyLoader.SCALE); gameStage.getWorld().rayCast(new RayCastCallback(){ public float reportRayFixture(final Fixture fixture, final Vector2 point, final Vector2 normal, final float fraction){ verticalSpeed = 0; item.setY(point.y/PhysicsBodyLoader.SCALE+0.1f); return 0; } }, rayFrom, rayTo); }
private boolean checkMovable(Body body, Vector2 from, Vector2 to) { World b2dWorld = body.getWorld(); moveable = true; RayCastCallback rayCastCallback = new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { if (fixture.getFilterData().categoryBits == GameManager.INDESTRUCTIIBLE_BIT | fixture.getFilterData().categoryBits == GameManager.BREAKABLE_BIT | fixture.getFilterData().categoryBits == GameManager.BOMB_BIT | fixture.getFilterData().categoryBits == GameManager.ENEMY_BIT | fixture.getFilterData().categoryBits == GameManager.PLAYER_BIT) { moveable = false; return 0; } return 0; } }; b2dWorld.rayCast(rayCastCallback, from, to); return moveable; }
private boolean checkCanExplodeThrough(Vector2 fromV, Vector2 toV) { canExplodeThrough = true; RayCastCallback rayCastCallback = new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { if (fixture.getFilterData().categoryBits == GameManager.INDESTRUCTIIBLE_BIT) { canExplodeThrough = false; return 0; } if (fixture.getFilterData().categoryBits == GameManager.BREAKABLE_BIT) { canExplodeThrough = false; Entity e = (Entity) fixture.getBody().getUserData(); Breakable breakable = e.getComponent(Breakable.class); breakable.state = Breakable.State.EXPLODING; return 0; } return 0; } }; b2dWorld.rayCast(rayCastCallback, fromV, toV); return canExplodeThrough; }
public static List<RaycastHit> raycast(Vector2 start, Vector2 direction) { final List<RaycastHit> hits = new ArrayList<RaycastHit>(); Vector2 end = start.add(direction); //TODO move to static single object physic.physicWorld.rayCast(new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { hits.add(new RaycastHit(fixture, point, normal, fraction)); return 1; } }, start, end); return hits; }
private boolean checkMovable(Body body, MoveDir dir) { canMove = true; World world = body.getWorld(); RayCastCallback rayCastCallback = new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { if (fixture.getFilterData().categoryBits == GameManager.WALL_BIT || fixture.getFilterData().categoryBits == GameManager.GATE_BIT) { canMove = false; return 0; } return 0; } }; for (int i = 0; i < 2; i++) { tmpV1.set(body.getPosition()); switch (dir) { case UP: tmpV2.set(body.getPosition().x - (i - 0.5f) * 0.2f, body.getPosition().y + 0.6f); break; case DOWN: tmpV2.set(body.getPosition().x - (i - 0.5f) * 0.2f, body.getPosition().y - 0.6f); break; case LEFT: tmpV2.set(body.getPosition().x - 0.6f, body.getPosition().y - (i - 0.5f) * 0.2f); break; case RIGHT: tmpV2.set(body.getPosition().x + 0.6f, body.getPosition().y - (i - 0.5f) * 0.2f); break; default: break; } world.rayCast(rayCastCallback, tmpV1, tmpV2); } return canMove; }
protected boolean hitSomethingVertical(final Body body, Vector2 fromV, Vector2 toV) { World b2dWorld = body.getWorld(); hit = false; RayCastCallback rayCastCallback = new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { // if hit the player, ignore it if (fixture.getFilterData().categoryBits == GameManager.PLAYER_BIT || fixture.getFilterData().categoryBits == GameManager.POWERUP_BIT) { return 0; } if (fraction < 1.0f) { hit = true; } return 0; } }; for (int i = 0; i < 3; i++) { Vector2 tmpV = new Vector2(toV); b2dWorld.rayCast(rayCastCallback, fromV, tmpV.add((1 - i) * 0.4f, 0)); } return hit; }
protected boolean hitSomethingHorizontal(final Body body, Vector2 fromV, Vector2 toV) { World b2dWorld = body.getWorld(); hit = false; RayCastCallback rayCastCallback = new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { // if hit the player or power-up item, ignore it if (fixture.getFilterData().categoryBits == GameManager.PLAYER_BIT || fixture.getFilterData().categoryBits == GameManager.POWERUP_BIT) { return 0; } if (fraction < 1.0f) { hit = true; } return 0; } }; for (int i = 0; i < 3; i++) { Vector2 tmpV = new Vector2(toV); b2dWorld.rayCast(rayCastCallback, fromV, tmpV.add(0, (1 - i) * 0.4f)); } return hit; }
private void checkForCollision() { float rayGap = item.getHeight() / 2; float raySize = -(Life.verticalSpeed + Gdx.graphics.getDeltaTime()) * Gdx.graphics.getDeltaTime(); if (raySize < 5f) raySize = 5f; if (Life.verticalSpeed > 0) return; Vector2 rayFrom = new Vector2((item.getX() + item.getWidth() / 2) * PhysicsBodyLoader.SCALE, (item.getY() + rayGap) * PhysicsBodyLoader.SCALE); Vector2 rayTo = new Vector2((item.getX() + item.getWidth() / 2) * PhysicsBodyLoader.SCALE, (item.getY() - raySize) * PhysicsBodyLoader.SCALE); // cast the ray stage.getWorld().rayCast(new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { // stop the player Life.verticalSpeed = 0; // reposition player slightly upper the collision point item.setY(point.y / PhysicsBodyLoader.SCALE + 0.1f); // make sure it is grounded, to allow jumping again Life.grounded = true; // jumpCounter = 0; return 0; } }, rayFrom, rayTo); }
public void rayCast(final RayCastCallback pRayCastCallback, final Vector2 pPoint1, final Vector2 pPoint2) { this.mWorld.rayCast(pRayCastCallback, pPoint1, pPoint2); }
public List<MushroomedCrab> getCrubsOnSuffering(Player player) { final List<MushroomedCrab> crabs = level.getCrabs(); final List<MushroomedCrab> suffered = new ArrayList<MushroomedCrab>(); Vector2 ppos = player.getBody().getPosition(); Vector2 fireTo = null; if (player.getDirX() == DirectionX.RIGHT) { switch (player.getDirY()) { case CROSSHAIR_UP: fireTo = new Vector2(ppos.x + 8, ppos.y + 8); break; case CROSSHAIR_DOWN: fireTo = new Vector2(ppos.x + 8, ppos.y - 8); break; case CROSSHAIR_MIDDLE: fireTo = new Vector2(ppos.x + 8, ppos.y); break; } } else { switch (player.getDirY()) { case CROSSHAIR_UP: fireTo = new Vector2(ppos.x - 8, ppos.y + 8); break; case CROSSHAIR_DOWN: fireTo = new Vector2(ppos.x - 8, ppos.y - 8); break; case CROSSHAIR_MIDDLE: fireTo = new Vector2(ppos.x - 8, ppos.y); break; } } // find all concats with player shot level.getWorld().rayCast(new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { for (MushroomedCrab crab : crabs) { if (crab.getBody() == fixture.getBody()) { suffered.add(crab); return 0; } } return -1; } }, player.getBody().getPosition(), fireTo); return suffered; }
public void raycast(int worldId, RayCastCallback callback, Vector2 point1, Vector2 point2) { universe.get(worldId).rayCast(callback, point1, point2); }
@Override protected void process(final Entity e) { Particle p = (Particle) e.getComponent(Particle.class); // Move the particle Vector2 pos = p.getPosition().cpy(); Vector2 deltaX = new Vector2(p.getLinearVelocity().x * deltaSeconds(), p.getLinearVelocity().y * deltaSeconds()); // DO RAY CASTING FOR COLLIDABLE CHECK LogManager.debug("Physics", "Started raycasting for particle"); if (e.hasComponent(Collidable.class)) { if (p.getLinearVelocity().len() == 0) { throw new GdxRuntimeException("Cannot raycast for particle with speed of 0."); } World c = world.getBox2DWorld(); // Perform the raycast c.rayCast(new RayCastCallback() { @Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { // If collision occurs final Collidable col = (Collidable) e .getComponent(Collidable.class); // Get the victim if (fixture.isSensor()) return 0; final Entity victim = (Entity) fixture.getBody() .getUserData(); float continueCol = col.continueCollision(e, victim); if (continueCol == 0) // Call the on collide event for the entity and // terminate if appropriate. rayCastCollisions.addCallback(new Object(), new EventCallback() { @Override public void invoke(Entity es, Object... args) { col.onBeginContact(e, victim); } }); // Return whether or not the ray cast should be terminated. return col.continueCollision(e, victim); } }, pos, pos.cpy().add(deltaX)); } LogManager.debug("Physics", "Finished raycasting for particle"); // Move and set the final position of the entity. pos.add(deltaX); p.setPosition(pos); float angularVelocity = p.getAngularVelocity() * deltaSeconds(); p.setRotation(p.getRotation() + angularVelocity); }
public static boolean inLineOfSight(Vector2 a, Vector2 b){ RayCastCallback cb = new RayCastCallback() { @Override public float reportRayFixture(Fixture arg0, Vector2 arg1, Vector2 arg2, float arg3) { return 0; } }; MySidekick.getWorld().rayCast(cb, null, null); return true; }