Adding blocks x amount from the center blocks in diffrent angles

Discussion in 'Spigot Plugin Development' started by AdamAdam, Jul 14, 2018.

  1. Basiaclly I have a block in the very middle, like this:


    Then I have another block I want to spawn x blocks away from this center block but in a random angle from it.
    Any ideas how you could accomplish this?
  2. Should be pretty easy.
    1. Take the middle block location
    2. Get a random number between 0 and 2 * PI, let's call this 'angle'
    3. Calculate direction vector with x = sin(angle), y = 0, z = cos(angle);
    4. Multiply direction vector by the desired radius
    5. Add direction vector to middle block location
    6. Set the block at the new location
  3. Thank you, only one thing I don't understand is "get a random number between 0 and 2 * PI" why 0 and 2. I dont see how I can get a 360 anglw out of that. Or is it just place holders?
  4. That's because the angle is in radians. You could also get a number from 0 to 360 and use Math.toRadians() to get the same, but we have to use radians since Math.sin() and Math.cos() take radians instead of degrees.

    Here's an excellent explanation from our local math god Finnbon
  5. Oh, makes sense. Thank you!