How can I calculate the distance to the closest player? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Can I retrieve the position of an offline player in SMP?Detect which player ignites the TNT using flint and steelHow do I do a /setblock for under a player?Convert player kill count in money [Vanilla]How do I teleport a player that has two specific scores in MinecraftHow to detect if a player out of one team diesHow to prevent the closest player from breaking blocks?How to detect player deaths in vanillaHow to test for the entity closest to the player?How do you find a player with a score in 1.13.1?

I could not break this equation. Please help me

Working through the single responsibility principle (SRP) in Python when calls are expensive

Single author papers against my advisor's will?

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time

Can undead you have reanimated wait inside a portable hole?

Why did all the guest students take carriages to the Yule Ball?

Sort a list of pairs representing an acyclic, partial automorphism

Can withdrawing asylum be illegal?

What's the point in a preamp?

Did the new image of black hole confirm the general theory of relativity?

Can smartphones with the same camera sensor have different image quality?

Why can't wing-mounted spoilers be used to steepen approaches?

How many people can fit inside Mordenkainen's Magnificent Mansion?

Didn't get enough time to take a Coding Test - what to do now?

Problems with Ubuntu mount /tmp

Windows 10: How to Lock (not sleep) laptop on lid close?

Semisimplicity of the category of coherent sheaves?

Segmentation fault output is suppressed when piping stdin into a function. Why?

Who or what is the being for whom Being is a question for Heidegger?

Would an alien lifeform be able to achieve space travel if lacking in vision?

Does Parliament need to approve the new Brexit delay to 31 October 2019?

He got a vote 80% that of Emmanuel Macron’s

Finding the path in a graph from A to B then back to A with a minimum of shared edges

Make it rain characters



How can I calculate the distance to the closest player?



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Can I retrieve the position of an offline player in SMP?Detect which player ignites the TNT using flint and steelHow do I do a /setblock for under a player?Convert player kill count in money [Vanilla]How do I teleport a player that has two specific scores in MinecraftHow to detect if a player out of one team diesHow to prevent the closest player from breaking blocks?How to detect player deaths in vanillaHow to test for the entity closest to the player?How do you find a player with a score in 1.13.1?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-2















How do I calculate distance to the closest player and show it via a scoreboard on a vanilla server?










share|improve this question



















  • 1





    Have you made any attempt to solve this yourself? Arqade works better when askers show effort to solve their own problems; we see that you have a problem you've worked on, and answerers respond to that. You also get a more specific answer that's tailored exactly to the part you're stuck, and Arqade gets a very specific question. Everybody wins!

    – Frank
    May 29 '17 at 21:21











  • @Frank yes, but there are many small problems (like how to get absolute value when I subtract coordinates, etc) seems you a bot btw

    – OwnageIsMagic
    May 29 '17 at 21:30











  • Do you need to do this with a vanilla server? It can be done easily on Bukkit.

    – RookieTEC9
    May 29 '17 at 21:32











  • I'd recommend adding that effort to your question; you'll get a better response.

    – Frank
    May 29 '17 at 21:34











  • @RookieTEC9 Yes. As I have restricted environment (Realms)

    – OwnageIsMagic
    May 29 '17 at 21:35

















-2















How do I calculate distance to the closest player and show it via a scoreboard on a vanilla server?










share|improve this question



















  • 1





    Have you made any attempt to solve this yourself? Arqade works better when askers show effort to solve their own problems; we see that you have a problem you've worked on, and answerers respond to that. You also get a more specific answer that's tailored exactly to the part you're stuck, and Arqade gets a very specific question. Everybody wins!

    – Frank
    May 29 '17 at 21:21











  • @Frank yes, but there are many small problems (like how to get absolute value when I subtract coordinates, etc) seems you a bot btw

    – OwnageIsMagic
    May 29 '17 at 21:30











  • Do you need to do this with a vanilla server? It can be done easily on Bukkit.

    – RookieTEC9
    May 29 '17 at 21:32











  • I'd recommend adding that effort to your question; you'll get a better response.

    – Frank
    May 29 '17 at 21:34











  • @RookieTEC9 Yes. As I have restricted environment (Realms)

    – OwnageIsMagic
    May 29 '17 at 21:35













-2












-2








-2


1






How do I calculate distance to the closest player and show it via a scoreboard on a vanilla server?










share|improve this question
















How do I calculate distance to the closest player and show it via a scoreboard on a vanilla server?







minecraft minecraft-commands






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 29 '17 at 21:03









RookieTEC9

684319




684319










asked May 29 '17 at 20:04









OwnageIsMagicOwnageIsMagic

1042




1042







  • 1





    Have you made any attempt to solve this yourself? Arqade works better when askers show effort to solve their own problems; we see that you have a problem you've worked on, and answerers respond to that. You also get a more specific answer that's tailored exactly to the part you're stuck, and Arqade gets a very specific question. Everybody wins!

    – Frank
    May 29 '17 at 21:21











  • @Frank yes, but there are many small problems (like how to get absolute value when I subtract coordinates, etc) seems you a bot btw

    – OwnageIsMagic
    May 29 '17 at 21:30











  • Do you need to do this with a vanilla server? It can be done easily on Bukkit.

    – RookieTEC9
    May 29 '17 at 21:32











  • I'd recommend adding that effort to your question; you'll get a better response.

    – Frank
    May 29 '17 at 21:34











  • @RookieTEC9 Yes. As I have restricted environment (Realms)

    – OwnageIsMagic
    May 29 '17 at 21:35












  • 1





    Have you made any attempt to solve this yourself? Arqade works better when askers show effort to solve their own problems; we see that you have a problem you've worked on, and answerers respond to that. You also get a more specific answer that's tailored exactly to the part you're stuck, and Arqade gets a very specific question. Everybody wins!

    – Frank
    May 29 '17 at 21:21











  • @Frank yes, but there are many small problems (like how to get absolute value when I subtract coordinates, etc) seems you a bot btw

    – OwnageIsMagic
    May 29 '17 at 21:30











  • Do you need to do this with a vanilla server? It can be done easily on Bukkit.

    – RookieTEC9
    May 29 '17 at 21:32











  • I'd recommend adding that effort to your question; you'll get a better response.

    – Frank
    May 29 '17 at 21:34











  • @RookieTEC9 Yes. As I have restricted environment (Realms)

    – OwnageIsMagic
    May 29 '17 at 21:35







1




1





Have you made any attempt to solve this yourself? Arqade works better when askers show effort to solve their own problems; we see that you have a problem you've worked on, and answerers respond to that. You also get a more specific answer that's tailored exactly to the part you're stuck, and Arqade gets a very specific question. Everybody wins!

– Frank
May 29 '17 at 21:21





Have you made any attempt to solve this yourself? Arqade works better when askers show effort to solve their own problems; we see that you have a problem you've worked on, and answerers respond to that. You also get a more specific answer that's tailored exactly to the part you're stuck, and Arqade gets a very specific question. Everybody wins!

– Frank
May 29 '17 at 21:21













@Frank yes, but there are many small problems (like how to get absolute value when I subtract coordinates, etc) seems you a bot btw

– OwnageIsMagic
May 29 '17 at 21:30





@Frank yes, but there are many small problems (like how to get absolute value when I subtract coordinates, etc) seems you a bot btw

– OwnageIsMagic
May 29 '17 at 21:30













Do you need to do this with a vanilla server? It can be done easily on Bukkit.

– RookieTEC9
May 29 '17 at 21:32





Do you need to do this with a vanilla server? It can be done easily on Bukkit.

– RookieTEC9
May 29 '17 at 21:32













I'd recommend adding that effort to your question; you'll get a better response.

– Frank
May 29 '17 at 21:34





I'd recommend adding that effort to your question; you'll get a better response.

– Frank
May 29 '17 at 21:34













@RookieTEC9 Yes. As I have restricted environment (Realms)

– OwnageIsMagic
May 29 '17 at 21:35





@RookieTEC9 Yes. As I have restricted environment (Realms)

– OwnageIsMagic
May 29 '17 at 21:35










2 Answers
2






active

oldest

votes


















0














You cannot show the distance closest to the player and show it via scoreboard due to the fact vanilla scoreboards are used for every player. This can be easily done with some bukkit code which will be both more efficient and cleaner to the eye.






share|improve this answer























  • I can't install bukkit on Realms ((

    – OwnageIsMagic
    May 29 '17 at 21:36











  • For each player show distance to nearest (nothing impossible)

    – OwnageIsMagic
    May 29 '17 at 21:39












  • It is impossible! The scoreboards are not per player! Look this up yourself before you downvote the correct answer to your question.

    – RookieTEC9
    May 30 '17 at 0:01











  • @OwnageIsMagic I think the post says this is impossible because you can't show a player their number and nobody else's. It doesn't seem to address what commands should be used.

    – ash4fun
    May 30 '17 at 18:09











  • @ash4fun Ok. I missed something, but its still not impossible (youtube.com/watch?v=cThnjDTxUUQ) PS I didn't press any vote button on this stack yet

    – OwnageIsMagic
    May 31 '17 at 20:49


















0














Just in case anyone tried to look up how this problem could be accomplished, because this is the first result from google search. And I happened to work it out.


You can do this in 1.13+ now, with the /execute store


The steps are as follows: store player coordinates > calculate the differences > square the differences > sum up > approximate the sqrt values. I'm using euclidean distance as the metric.


1. Store Player Coordinates:

You'd need to define objectives to store the values, I defined them as "X", "Y", and "Z"


execute as @a run execute store result score @s X run data get entity @s Pos[0]

execute as @a run execute store result score @s Y run data get entity @s Pos[1]

execute as @a run execute store result score @s Z run data get entity @s Pos[2]


2. Calculate the Differences:

I defined another set of objectives to store the results "X_difference", "Y_difference", "Z_difference", but you don't have to


execute as @a run scoreboard players operation @s X_difference = @s X

execute at @a run scoreboard players operation @s X_difference -= @p[sort=nearest] X

execute as @a run scoreboard players operation @s Y_difference = @s Y

execute at @a run scoreboard players operation @s Y_difference -= @p[sort=nearest] Y

execute as @a run scoreboard players operation @s Z_difference = @s Z

execute at @a run scoreboard players operation @s Z_difference -= @p[sort=nearest] Z


3. Square the Differences:


execute as @a run scoreboard players operation @s X_difference *= @s X_difference

execute as @a run scoreboard players operation @s Y_difference *= @s Y_difference

execute as @a run scoreboard players operation @s Z_difference *= @s Z_difference


4. Sum Up:

I defined another objective "distance" to store the sum of the squared values from 3.


execute as @a run scoreboard players operation @s distance = @s X_difference

execute as @a run scoreboard players operation @s distance += @s Y_difference

execute as @a run scoreboard players operation @s distance += @s Z_difference


5. Approximate the Square Root Values:

I defined another objective "dist_int" to store the approximations

Here you can expand the list as long as you want, here are a few examples:

execute as @a if score @s distance matches 0.. run scoreboard players set @s dist_int 0

execute as @a if score @s distance matches 1.. run scoreboard players set @s dist_int 1

execute as @a if score @s distance matches 4.. run scoreboard players set @s dist_int 2

execute as @a if score @s distance matches 9.. run scoreboard players set @s dist_int 3

execute as @a if score @s distance matches 16.. run scoreboard players set @s dist_int 4


Generally it's

execute as @a if score @s distance matches n * n.. run scoreboard players set @s dist_int n



The "dist_int" objective contains the answers to your problem.






share|improve this answer








New contributor




YoshinoBHL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "41"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgaming.stackexchange.com%2fquestions%2f310012%2fhow-can-i-calculate-the-distance-to-the-closest-player%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You cannot show the distance closest to the player and show it via scoreboard due to the fact vanilla scoreboards are used for every player. This can be easily done with some bukkit code which will be both more efficient and cleaner to the eye.






    share|improve this answer























    • I can't install bukkit on Realms ((

      – OwnageIsMagic
      May 29 '17 at 21:36











    • For each player show distance to nearest (nothing impossible)

      – OwnageIsMagic
      May 29 '17 at 21:39












    • It is impossible! The scoreboards are not per player! Look this up yourself before you downvote the correct answer to your question.

      – RookieTEC9
      May 30 '17 at 0:01











    • @OwnageIsMagic I think the post says this is impossible because you can't show a player their number and nobody else's. It doesn't seem to address what commands should be used.

      – ash4fun
      May 30 '17 at 18:09











    • @ash4fun Ok. I missed something, but its still not impossible (youtube.com/watch?v=cThnjDTxUUQ) PS I didn't press any vote button on this stack yet

      – OwnageIsMagic
      May 31 '17 at 20:49















    0














    You cannot show the distance closest to the player and show it via scoreboard due to the fact vanilla scoreboards are used for every player. This can be easily done with some bukkit code which will be both more efficient and cleaner to the eye.






    share|improve this answer























    • I can't install bukkit on Realms ((

      – OwnageIsMagic
      May 29 '17 at 21:36











    • For each player show distance to nearest (nothing impossible)

      – OwnageIsMagic
      May 29 '17 at 21:39












    • It is impossible! The scoreboards are not per player! Look this up yourself before you downvote the correct answer to your question.

      – RookieTEC9
      May 30 '17 at 0:01











    • @OwnageIsMagic I think the post says this is impossible because you can't show a player their number and nobody else's. It doesn't seem to address what commands should be used.

      – ash4fun
      May 30 '17 at 18:09











    • @ash4fun Ok. I missed something, but its still not impossible (youtube.com/watch?v=cThnjDTxUUQ) PS I didn't press any vote button on this stack yet

      – OwnageIsMagic
      May 31 '17 at 20:49













    0












    0








    0







    You cannot show the distance closest to the player and show it via scoreboard due to the fact vanilla scoreboards are used for every player. This can be easily done with some bukkit code which will be both more efficient and cleaner to the eye.






    share|improve this answer













    You cannot show the distance closest to the player and show it via scoreboard due to the fact vanilla scoreboards are used for every player. This can be easily done with some bukkit code which will be both more efficient and cleaner to the eye.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered May 29 '17 at 21:34









    RookieTEC9RookieTEC9

    684319




    684319












    • I can't install bukkit on Realms ((

      – OwnageIsMagic
      May 29 '17 at 21:36











    • For each player show distance to nearest (nothing impossible)

      – OwnageIsMagic
      May 29 '17 at 21:39












    • It is impossible! The scoreboards are not per player! Look this up yourself before you downvote the correct answer to your question.

      – RookieTEC9
      May 30 '17 at 0:01











    • @OwnageIsMagic I think the post says this is impossible because you can't show a player their number and nobody else's. It doesn't seem to address what commands should be used.

      – ash4fun
      May 30 '17 at 18:09











    • @ash4fun Ok. I missed something, but its still not impossible (youtube.com/watch?v=cThnjDTxUUQ) PS I didn't press any vote button on this stack yet

      – OwnageIsMagic
      May 31 '17 at 20:49

















    • I can't install bukkit on Realms ((

      – OwnageIsMagic
      May 29 '17 at 21:36











    • For each player show distance to nearest (nothing impossible)

      – OwnageIsMagic
      May 29 '17 at 21:39












    • It is impossible! The scoreboards are not per player! Look this up yourself before you downvote the correct answer to your question.

      – RookieTEC9
      May 30 '17 at 0:01











    • @OwnageIsMagic I think the post says this is impossible because you can't show a player their number and nobody else's. It doesn't seem to address what commands should be used.

      – ash4fun
      May 30 '17 at 18:09











    • @ash4fun Ok. I missed something, but its still not impossible (youtube.com/watch?v=cThnjDTxUUQ) PS I didn't press any vote button on this stack yet

      – OwnageIsMagic
      May 31 '17 at 20:49
















    I can't install bukkit on Realms ((

    – OwnageIsMagic
    May 29 '17 at 21:36





    I can't install bukkit on Realms ((

    – OwnageIsMagic
    May 29 '17 at 21:36













    For each player show distance to nearest (nothing impossible)

    – OwnageIsMagic
    May 29 '17 at 21:39






    For each player show distance to nearest (nothing impossible)

    – OwnageIsMagic
    May 29 '17 at 21:39














    It is impossible! The scoreboards are not per player! Look this up yourself before you downvote the correct answer to your question.

    – RookieTEC9
    May 30 '17 at 0:01





    It is impossible! The scoreboards are not per player! Look this up yourself before you downvote the correct answer to your question.

    – RookieTEC9
    May 30 '17 at 0:01













    @OwnageIsMagic I think the post says this is impossible because you can't show a player their number and nobody else's. It doesn't seem to address what commands should be used.

    – ash4fun
    May 30 '17 at 18:09





    @OwnageIsMagic I think the post says this is impossible because you can't show a player their number and nobody else's. It doesn't seem to address what commands should be used.

    – ash4fun
    May 30 '17 at 18:09













    @ash4fun Ok. I missed something, but its still not impossible (youtube.com/watch?v=cThnjDTxUUQ) PS I didn't press any vote button on this stack yet

    – OwnageIsMagic
    May 31 '17 at 20:49





    @ash4fun Ok. I missed something, but its still not impossible (youtube.com/watch?v=cThnjDTxUUQ) PS I didn't press any vote button on this stack yet

    – OwnageIsMagic
    May 31 '17 at 20:49













    0














    Just in case anyone tried to look up how this problem could be accomplished, because this is the first result from google search. And I happened to work it out.


    You can do this in 1.13+ now, with the /execute store


    The steps are as follows: store player coordinates > calculate the differences > square the differences > sum up > approximate the sqrt values. I'm using euclidean distance as the metric.


    1. Store Player Coordinates:

    You'd need to define objectives to store the values, I defined them as "X", "Y", and "Z"


    execute as @a run execute store result score @s X run data get entity @s Pos[0]

    execute as @a run execute store result score @s Y run data get entity @s Pos[1]

    execute as @a run execute store result score @s Z run data get entity @s Pos[2]


    2. Calculate the Differences:

    I defined another set of objectives to store the results "X_difference", "Y_difference", "Z_difference", but you don't have to


    execute as @a run scoreboard players operation @s X_difference = @s X

    execute at @a run scoreboard players operation @s X_difference -= @p[sort=nearest] X

    execute as @a run scoreboard players operation @s Y_difference = @s Y

    execute at @a run scoreboard players operation @s Y_difference -= @p[sort=nearest] Y

    execute as @a run scoreboard players operation @s Z_difference = @s Z

    execute at @a run scoreboard players operation @s Z_difference -= @p[sort=nearest] Z


    3. Square the Differences:


    execute as @a run scoreboard players operation @s X_difference *= @s X_difference

    execute as @a run scoreboard players operation @s Y_difference *= @s Y_difference

    execute as @a run scoreboard players operation @s Z_difference *= @s Z_difference


    4. Sum Up:

    I defined another objective "distance" to store the sum of the squared values from 3.


    execute as @a run scoreboard players operation @s distance = @s X_difference

    execute as @a run scoreboard players operation @s distance += @s Y_difference

    execute as @a run scoreboard players operation @s distance += @s Z_difference


    5. Approximate the Square Root Values:

    I defined another objective "dist_int" to store the approximations

    Here you can expand the list as long as you want, here are a few examples:

    execute as @a if score @s distance matches 0.. run scoreboard players set @s dist_int 0

    execute as @a if score @s distance matches 1.. run scoreboard players set @s dist_int 1

    execute as @a if score @s distance matches 4.. run scoreboard players set @s dist_int 2

    execute as @a if score @s distance matches 9.. run scoreboard players set @s dist_int 3

    execute as @a if score @s distance matches 16.. run scoreboard players set @s dist_int 4


    Generally it's

    execute as @a if score @s distance matches n * n.. run scoreboard players set @s dist_int n



    The "dist_int" objective contains the answers to your problem.






    share|improve this answer








    New contributor




    YoshinoBHL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.
























      0














      Just in case anyone tried to look up how this problem could be accomplished, because this is the first result from google search. And I happened to work it out.


      You can do this in 1.13+ now, with the /execute store


      The steps are as follows: store player coordinates > calculate the differences > square the differences > sum up > approximate the sqrt values. I'm using euclidean distance as the metric.


      1. Store Player Coordinates:

      You'd need to define objectives to store the values, I defined them as "X", "Y", and "Z"


      execute as @a run execute store result score @s X run data get entity @s Pos[0]

      execute as @a run execute store result score @s Y run data get entity @s Pos[1]

      execute as @a run execute store result score @s Z run data get entity @s Pos[2]


      2. Calculate the Differences:

      I defined another set of objectives to store the results "X_difference", "Y_difference", "Z_difference", but you don't have to


      execute as @a run scoreboard players operation @s X_difference = @s X

      execute at @a run scoreboard players operation @s X_difference -= @p[sort=nearest] X

      execute as @a run scoreboard players operation @s Y_difference = @s Y

      execute at @a run scoreboard players operation @s Y_difference -= @p[sort=nearest] Y

      execute as @a run scoreboard players operation @s Z_difference = @s Z

      execute at @a run scoreboard players operation @s Z_difference -= @p[sort=nearest] Z


      3. Square the Differences:


      execute as @a run scoreboard players operation @s X_difference *= @s X_difference

      execute as @a run scoreboard players operation @s Y_difference *= @s Y_difference

      execute as @a run scoreboard players operation @s Z_difference *= @s Z_difference


      4. Sum Up:

      I defined another objective "distance" to store the sum of the squared values from 3.


      execute as @a run scoreboard players operation @s distance = @s X_difference

      execute as @a run scoreboard players operation @s distance += @s Y_difference

      execute as @a run scoreboard players operation @s distance += @s Z_difference


      5. Approximate the Square Root Values:

      I defined another objective "dist_int" to store the approximations

      Here you can expand the list as long as you want, here are a few examples:

      execute as @a if score @s distance matches 0.. run scoreboard players set @s dist_int 0

      execute as @a if score @s distance matches 1.. run scoreboard players set @s dist_int 1

      execute as @a if score @s distance matches 4.. run scoreboard players set @s dist_int 2

      execute as @a if score @s distance matches 9.. run scoreboard players set @s dist_int 3

      execute as @a if score @s distance matches 16.. run scoreboard players set @s dist_int 4


      Generally it's

      execute as @a if score @s distance matches n * n.. run scoreboard players set @s dist_int n



      The "dist_int" objective contains the answers to your problem.






      share|improve this answer








      New contributor




      YoshinoBHL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















        0












        0








        0







        Just in case anyone tried to look up how this problem could be accomplished, because this is the first result from google search. And I happened to work it out.


        You can do this in 1.13+ now, with the /execute store


        The steps are as follows: store player coordinates > calculate the differences > square the differences > sum up > approximate the sqrt values. I'm using euclidean distance as the metric.


        1. Store Player Coordinates:

        You'd need to define objectives to store the values, I defined them as "X", "Y", and "Z"


        execute as @a run execute store result score @s X run data get entity @s Pos[0]

        execute as @a run execute store result score @s Y run data get entity @s Pos[1]

        execute as @a run execute store result score @s Z run data get entity @s Pos[2]


        2. Calculate the Differences:

        I defined another set of objectives to store the results "X_difference", "Y_difference", "Z_difference", but you don't have to


        execute as @a run scoreboard players operation @s X_difference = @s X

        execute at @a run scoreboard players operation @s X_difference -= @p[sort=nearest] X

        execute as @a run scoreboard players operation @s Y_difference = @s Y

        execute at @a run scoreboard players operation @s Y_difference -= @p[sort=nearest] Y

        execute as @a run scoreboard players operation @s Z_difference = @s Z

        execute at @a run scoreboard players operation @s Z_difference -= @p[sort=nearest] Z


        3. Square the Differences:


        execute as @a run scoreboard players operation @s X_difference *= @s X_difference

        execute as @a run scoreboard players operation @s Y_difference *= @s Y_difference

        execute as @a run scoreboard players operation @s Z_difference *= @s Z_difference


        4. Sum Up:

        I defined another objective "distance" to store the sum of the squared values from 3.


        execute as @a run scoreboard players operation @s distance = @s X_difference

        execute as @a run scoreboard players operation @s distance += @s Y_difference

        execute as @a run scoreboard players operation @s distance += @s Z_difference


        5. Approximate the Square Root Values:

        I defined another objective "dist_int" to store the approximations

        Here you can expand the list as long as you want, here are a few examples:

        execute as @a if score @s distance matches 0.. run scoreboard players set @s dist_int 0

        execute as @a if score @s distance matches 1.. run scoreboard players set @s dist_int 1

        execute as @a if score @s distance matches 4.. run scoreboard players set @s dist_int 2

        execute as @a if score @s distance matches 9.. run scoreboard players set @s dist_int 3

        execute as @a if score @s distance matches 16.. run scoreboard players set @s dist_int 4


        Generally it's

        execute as @a if score @s distance matches n * n.. run scoreboard players set @s dist_int n



        The "dist_int" objective contains the answers to your problem.






        share|improve this answer








        New contributor




        YoshinoBHL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        Just in case anyone tried to look up how this problem could be accomplished, because this is the first result from google search. And I happened to work it out.


        You can do this in 1.13+ now, with the /execute store


        The steps are as follows: store player coordinates > calculate the differences > square the differences > sum up > approximate the sqrt values. I'm using euclidean distance as the metric.


        1. Store Player Coordinates:

        You'd need to define objectives to store the values, I defined them as "X", "Y", and "Z"


        execute as @a run execute store result score @s X run data get entity @s Pos[0]

        execute as @a run execute store result score @s Y run data get entity @s Pos[1]

        execute as @a run execute store result score @s Z run data get entity @s Pos[2]


        2. Calculate the Differences:

        I defined another set of objectives to store the results "X_difference", "Y_difference", "Z_difference", but you don't have to


        execute as @a run scoreboard players operation @s X_difference = @s X

        execute at @a run scoreboard players operation @s X_difference -= @p[sort=nearest] X

        execute as @a run scoreboard players operation @s Y_difference = @s Y

        execute at @a run scoreboard players operation @s Y_difference -= @p[sort=nearest] Y

        execute as @a run scoreboard players operation @s Z_difference = @s Z

        execute at @a run scoreboard players operation @s Z_difference -= @p[sort=nearest] Z


        3. Square the Differences:


        execute as @a run scoreboard players operation @s X_difference *= @s X_difference

        execute as @a run scoreboard players operation @s Y_difference *= @s Y_difference

        execute as @a run scoreboard players operation @s Z_difference *= @s Z_difference


        4. Sum Up:

        I defined another objective "distance" to store the sum of the squared values from 3.


        execute as @a run scoreboard players operation @s distance = @s X_difference

        execute as @a run scoreboard players operation @s distance += @s Y_difference

        execute as @a run scoreboard players operation @s distance += @s Z_difference


        5. Approximate the Square Root Values:

        I defined another objective "dist_int" to store the approximations

        Here you can expand the list as long as you want, here are a few examples:

        execute as @a if score @s distance matches 0.. run scoreboard players set @s dist_int 0

        execute as @a if score @s distance matches 1.. run scoreboard players set @s dist_int 1

        execute as @a if score @s distance matches 4.. run scoreboard players set @s dist_int 2

        execute as @a if score @s distance matches 9.. run scoreboard players set @s dist_int 3

        execute as @a if score @s distance matches 16.. run scoreboard players set @s dist_int 4


        Generally it's

        execute as @a if score @s distance matches n * n.. run scoreboard players set @s dist_int n



        The "dist_int" objective contains the answers to your problem.







        share|improve this answer








        New contributor




        YoshinoBHL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        YoshinoBHL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 13 mins ago









        YoshinoBHLYoshinoBHL

        1




        1




        New contributor




        YoshinoBHL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        YoshinoBHL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        YoshinoBHL is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Arqade!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgaming.stackexchange.com%2fquestions%2f310012%2fhow-can-i-calculate-the-distance-to-the-closest-player%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Isurus Índice Especies | Notas | Véxase tamén | Menú de navegación"A compendium of fossil marine animal genera (Chondrichthyes entry)"o orixinal"A review of the Tertiary fossil Cetacea (Mammalia) localities in wales port taf Museum Victoria"o orixinalThe Vertebrate Fauna of the Selma Formation of Alabama. Part VII. Part VIII. The Mosasaurs The Fishes50419737IDsh85068767Isurus2548834613242066569678159923NHMSYS00210535017845105743

            Король Коль Исторические данные | Стихотворение | Примечания | Навигацияверсии1 правкаверсии1 правкаA New interpretation of the 'Artognou' stone, TintagelTintagel IslandАрхивировано

            Roughly how much would it cost to hire a team of dwarves to build a home in the mountainside? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)How much does a house cost?How long does it take to mine rock?How much does a house cost?How much gold would the construction of a forge cost?How much does a door cost?How much would it cost to make this magic item?How much would a glue bomb cost?How much does mandrake root cost?How much does a slave cost?How much does equipment cost?How much do sheep cost?How much would firearms cost?