help with screen shake effect

Discussion in 'Spigot Plugin Development' started by koekeritis, May 6, 2015.

  1. for a earthquake disaster plugin I'm making a screen shake effect has to be implemented. Which I did. But I have 2 problems.
    1. Entity & more seem to freeze while the camera is shaking (They just freeze for the player's perspective)
    2. The player can't move (Quite understandable with the current code but really anoying)
    here is my code:
    Code (Text):
    for(int i = 0; i < 10000; i++) {
        Location shake1 = p.getLocation();
        shake1.setPitch(p.getLocation().getPitch() - 1);

        Location shake2 = p.getLocation();
        shake2.setPitch(p.getLocation().getPitch() + 1);

        Location shake3 = p.getLocation();
        shake3.setPitch(p.getLocation().getPitch() - 1);
        shake3.setYaw(p.getLocation().getYaw() + 1);

        Location shake4 = p.getLocation();
        shake4.setPitch(p.getLocation().getPitch());
        shake4.setYaw(p.getLocation().getYaw() - 1);

        Location shake5 = p.getLocation();
        shake5.setYaw(p.getLocation().getYaw() + 1);

        Location shake0 = p.getLocation();

        p.teleport(shake1);
        p.teleport(shake2);
        p.teleport(shake3);
        if (rnd.nextInt(2) == 0) p.teleport(shake4);
        else p.teleport(shake5);
        p.teleport(shake0);
        p.sendMessage(10000 - i + " shakes remaining!");
    }
    NOTE:
    - The for loop is temporary I just made this for testing purposes.
    - No, removing the shake0 doesn't help. It just does wath I wanted to prevent from happening by adding shake0 (duh) and nothing more (or less)
     
  2. Try to wait after every teleport.
    If you are in an async thread, you can quietly use TimeUnit.

    Rewrite your code with these steps:

    Code (Text):

    foreach loop with all players
      get the current player's location
      modify it
      teleport to it

    wait eg. 200ms

    //Do it again and again with another types of modifying.
     
     
    • Creative Creative x 1
  3. Ever heard of Thread safety? I doubt teleporting is thread-safe so it might be best not to do it async..
    Using the main thread and a scheduler can get you the same effect.

    OP: The freezing is most likely cause by the fact that the server is just doing this huge task for you.. This loop probably takes a second or a few to complete, and in that time it can't do other things (such as updating the entities). When you remove the for loop and replace it with shorter bursts, the server should have time to update the rest in between.
     
  4. konsolas

    Supporter

    You should schedule a repeating task, with a delay instead of trying to do 10000 shakes in 1 tick.

    When all the required shakes are completed, simply cancel the task.
     
    • Agree Agree x 1
  5. Yeah, waiting a bit is the best thing to do. I was a bit stupid but I thought that thread.sleep wasn't good to use (and it isn't) but I didn't think of alternative's. Thx everyone.
     
  6. The freezing isn't cuased by the server not being able to handle I don't think because if I log on to the server with a second acount his game doesn't freeze while the other acount is getting screen shaked.