Solved Checking for an obstructed vector (Project abandoned)

Discussion in 'Plugin Development' started by Demyxa, Apr 11, 2020.

Thread Status:
Not open for further replies.
  1. I have two positions for which I want to check if there are blocks inbetween those said positions. The point of this is to "muffle" chat messages if there are walls in the way. So far, I've made a Vector from Position 1 to Position 2 using Vector Subtraction, however I can't think of any way to check if that Vector is obstructed by Blocks other than firing an invisible projectile and checking for a hit, but that seems far too inconvenient (and inconsistent) to use.

    Code:
    public void onPlayerFChat(PlayerChatEvent farc) {
            Player p = farc.getPlayer();
            Location send = p.getEyeLocation();
            Set<Player> Players = farc.getRecipients();
            Players.remove(p);
                for (Player rec : Players) {
                    Location recp = rec.getEyeLocation();
                    Vector vec = recp.toVector().subtract(send.toVector());
    This is how I made the Vector, anyone have any ideas?
     
    Last edited: Apr 11, 2020
  2. Offline

    Zombie_Striker

    @Demyxa
    You don't need to make a physical projectile, but you can do small steps between the two locations to check if there is a block there. Here's how you would do it:
    1. Get the direction between both players as a vector
    2. Take that vector, normalize it, multiply it by 0.1. This is now a step that is 0.1 of a block (you can increase it if you don't want to worry about corners that much)
    3. Create a new location which is the checker location.
    4. Create a for loop, while some int (I) is less than the distance between the two players, add 0.1 (I+=0.1)
    5. Add the step to the checker. It has now moved one step.
    6. Check to see if the block is solid. If it is, you can break knowing that the chat should be muffled.
     
  3. @Zombie_Striker
    I grabbed aside my partner and we did just that together, however we've got a few amount of issues still.
    1) Whenever the distance is greater than 10 we make steps in 1.1 - 1.5 blocks, meaning we sometimes skip blocks which can make this inconsistent (the local chat distance is 15 blocks.)
    2) We don't want to muffle the chat if the person is merely behind a 1 Block wide obstacle like a tree or pillar, meaning we have to check for adjacent blocks which can take a long time if there are a lot of people nearby.

    Actually, never mind, I just found that he used a while-loop:

    Code:
    double r = 0.0;
            while (r <= 1) {
                
                    p.sendMessage("§dBeginne While-Schleife");
                    Vector vecz = vec.multiply(r);
                    p.sendMessage("Vektor 'z' bei: " + vecz);
                    Vector vec2 = vecz.add(send.toVector());
                    p.sendMessage("Vektor'2' bei: " + vecz);
                    if (!(vec2.toLocation(p.getWorld()).getBlock().getType().equals(Material.AIR))) {
                        p.sendMessage("Block an " + vec2.toLocation(p.getWorld()).getBlock().getLocation());
                        p.sendMessage("Block Art: " + vec2.toLocation(p.getWorld()).getBlock().getType());
                        break;
                    }
                    r += 0.01;
                }
     
    Last edited: Apr 12, 2020
Thread Status:
Not open for further replies.

Share This Page