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?

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

Can a 1st-level character have an ability score above 18?

Can a novice safely splice in wire to lengthen 5V charging cable?

Hiding Certain Lines on Table

The variadic template constructor of my class cannot modify my class members, why is that so?

How are presidential pardons supposed to be used?

Python - Fishing Simulator

What LEGO pieces have "real-world" functionality?

How to pronounce 1ターン?

Can withdrawing asylum be illegal?

Why does the Event Horizon Telescope (EHT) not include telescopes from Africa, Asia or Australia?

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

How to copy the contents of all files with a certain name into a new file?

Netflix Recommendations?

When did F become S in typeography, and why?

Arduino Pro Micro - switch off LEDs

Keeping a retro style to sci-fi spaceships?

Do working physicists consider Newtonian mechanics to be "falsified"?

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

Road tyres vs "Street" tyres for charity ride on MTB Tandem

How to test the equality of two Pearson correlation coefficients computed from the same sample?

Problems with Ubuntu mount /tmp

Can the prologue be the backstory of your main character?

Am I ethically obligated to go into work on an off day if the reason is sudden?



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 1 hour ago









        YoshinoBHLYoshinoBHL

        11




        11




        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

            ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

            Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

            19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу