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;
How do I calculate distance to the closest player and show it via a scoreboard on a vanilla server?
minecraft minecraft-commands
|
show 4 more comments
How do I calculate distance to the closest player and show it via a scoreboard on a vanilla server?
minecraft minecraft-commands
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
|
show 4 more comments
How do I calculate distance to the closest player and show it via a scoreboard on a vanilla server?
minecraft minecraft-commands
How do I calculate distance to the closest player and show it via a scoreboard on a vanilla server?
minecraft minecraft-commands
minecraft minecraft-commands
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
|
show 4 more comments
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
|
show 4 more comments
2 Answers
2
active
oldest
votes
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.
I can't install bukkit on Realms ((
– OwnageIsMagic
May 29 '17 at 21:36
Foreach
player show distance tonearest
(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
|
show 1 more comment
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.
New contributor
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
I can't install bukkit on Realms ((
– OwnageIsMagic
May 29 '17 at 21:36
Foreach
player show distance tonearest
(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
|
show 1 more comment
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.
I can't install bukkit on Realms ((
– OwnageIsMagic
May 29 '17 at 21:36
Foreach
player show distance tonearest
(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
|
show 1 more comment
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.
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.
answered May 29 '17 at 21:34
RookieTEC9RookieTEC9
684319
684319
I can't install bukkit on Realms ((
– OwnageIsMagic
May 29 '17 at 21:36
Foreach
player show distance tonearest
(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
|
show 1 more comment
I can't install bukkit on Realms ((
– OwnageIsMagic
May 29 '17 at 21:36
Foreach
player show distance tonearest
(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
|
show 1 more comment
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.
New contributor
add a comment |
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.
New contributor
add a comment |
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.
New contributor
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.
New contributor
New contributor
answered 1 hour ago
YoshinoBHLYoshinoBHL
11
11
New contributor
New contributor
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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