Finding closest possible coordinatesHow to check if a possible path exist?How do I find out which DOM element has the focus?Where can I find documentation on formatting a date in JavaScript?Easy interview question got harder: given numbers 1..100, find the missing number(s)Getting the closest string matchFind an integer not among four billion given onesFind object by id in an array of JavaScript objectsHow to find time complexity of an algorithmIs it possible to apply CSS to half of a character?Detecting 3D collision between a triangle and a rotating sphereFinding closest x,y coordinates
How to not starve gigantic beasts
Checks user level and limit the data before saving it to mongoDB
Was there a Viking Exchange as well as a Columbian one?
Implications of cigar-shaped bodies having rings?
Size of electromagnet needed to replicate Earth's magnetic field
What are the characteristics of a typeless programming language?
What happened to Captain America in Endgame?
How do I check if a string is entirely made of the same substring?
Is it idiomatic to construct against `this`
Is this homebrew Wind Wave spell balanced?
Two field separators (colon and space) in awk
Which big number is bigger?
Elements other than carbon that can form many different compounds by bonding to themselves?
Dynamic SOQL query relationship with field visibility for Users
How come there are so many candidates for the 2020 Democratic party presidential nomination?
How to fry ground beef so it is well-browned
How to have a sharp product image?
What term is being referred to with "reflected-sound-of-underground-spirits"?
Providing evidence of Consent of Parents for Marriage by minor in England in early 1800s?
How does Captain America channel this power?
Multiple options vs single option UI
A Paper Record is What I Hamper
How could Tony Stark make this in Endgame?
555 timer FM transmitter
Finding closest possible coordinates
How to check if a possible path exist?How do I find out which DOM element has the focus?Where can I find documentation on formatting a date in JavaScript?Easy interview question got harder: given numbers 1..100, find the missing number(s)Getting the closest string matchFind an integer not among four billion given onesFind object by id in an array of JavaScript objectsHow to find time complexity of an algorithmIs it possible to apply CSS to half of a character?Detecting 3D collision between a triangle and a rotating sphereFinding closest x,y coordinates
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have x,y coordinates board like this:
the board is up to 100x100.
Where myPosition is gold, destinations is green and collisions is red. myPosition
is object destinations
and collisions
are array of objects:
let myPosition=x:0,y:0;
let destinations = [x: 0, y: 5, x: 2, y: 0, x: 2, y: 2];
let collisions = [x: 1, y: 0,x: 1, y: 1,x: 1, y: 2,x: 1, y: 3,x: 1, y: 4,x: 2, y: 1,x: 2, y: 0,x: 2, y: 1]
With this code (live demo) I'm able to find nearest destination but it doesn't know about collisions at all. I can not figure how to write algorithm which additionally would check for collision and give ouput 0,5
in above scenerio.
There is also assumption that we can not move diagonally.
I found this SO answer which seems to provide answer to my question but I can not get it to work with my input arrays.
javascript algorithm collision nearest-neighbor
add a comment |
I have x,y coordinates board like this:
the board is up to 100x100.
Where myPosition is gold, destinations is green and collisions is red. myPosition
is object destinations
and collisions
are array of objects:
let myPosition=x:0,y:0;
let destinations = [x: 0, y: 5, x: 2, y: 0, x: 2, y: 2];
let collisions = [x: 1, y: 0,x: 1, y: 1,x: 1, y: 2,x: 1, y: 3,x: 1, y: 4,x: 2, y: 1,x: 2, y: 0,x: 2, y: 1]
With this code (live demo) I'm able to find nearest destination but it doesn't know about collisions at all. I can not figure how to write algorithm which additionally would check for collision and give ouput 0,5
in above scenerio.
There is also assumption that we can not move diagonally.
I found this SO answer which seems to provide answer to my question but I can not get it to work with my input arrays.
javascript algorithm collision nearest-neighbor
This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced
– Steven Stark
Mar 22 at 17:44
even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs
– juvian
Mar 22 at 17:46
I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?
– BT101
Mar 22 at 18:46
add a comment |
I have x,y coordinates board like this:
the board is up to 100x100.
Where myPosition is gold, destinations is green and collisions is red. myPosition
is object destinations
and collisions
are array of objects:
let myPosition=x:0,y:0;
let destinations = [x: 0, y: 5, x: 2, y: 0, x: 2, y: 2];
let collisions = [x: 1, y: 0,x: 1, y: 1,x: 1, y: 2,x: 1, y: 3,x: 1, y: 4,x: 2, y: 1,x: 2, y: 0,x: 2, y: 1]
With this code (live demo) I'm able to find nearest destination but it doesn't know about collisions at all. I can not figure how to write algorithm which additionally would check for collision and give ouput 0,5
in above scenerio.
There is also assumption that we can not move diagonally.
I found this SO answer which seems to provide answer to my question but I can not get it to work with my input arrays.
javascript algorithm collision nearest-neighbor
I have x,y coordinates board like this:
the board is up to 100x100.
Where myPosition is gold, destinations is green and collisions is red. myPosition
is object destinations
and collisions
are array of objects:
let myPosition=x:0,y:0;
let destinations = [x: 0, y: 5, x: 2, y: 0, x: 2, y: 2];
let collisions = [x: 1, y: 0,x: 1, y: 1,x: 1, y: 2,x: 1, y: 3,x: 1, y: 4,x: 2, y: 1,x: 2, y: 0,x: 2, y: 1]
With this code (live demo) I'm able to find nearest destination but it doesn't know about collisions at all. I can not figure how to write algorithm which additionally would check for collision and give ouput 0,5
in above scenerio.
There is also assumption that we can not move diagonally.
I found this SO answer which seems to provide answer to my question but I can not get it to work with my input arrays.
javascript algorithm collision nearest-neighbor
javascript algorithm collision nearest-neighbor
asked Mar 22 at 17:40
BT101BT101
2092626
2092626
This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced
– Steven Stark
Mar 22 at 17:44
even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs
– juvian
Mar 22 at 17:46
I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?
– BT101
Mar 22 at 18:46
add a comment |
This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced
– Steven Stark
Mar 22 at 17:44
even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs
– juvian
Mar 22 at 17:46
I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?
– BT101
Mar 22 at 18:46
This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced
– Steven Stark
Mar 22 at 17:44
This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced
– Steven Stark
Mar 22 at 17:44
even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs
– juvian
Mar 22 at 17:46
even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs
– juvian
Mar 22 at 17:46
I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?
– BT101
Mar 22 at 18:46
I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?
– BT101
Mar 22 at 18:46
add a comment |
1 Answer
1
active
oldest
votes
I used pathFinding.js library and finding is very simple:
currentPath = finder.findPath(heroCoords.x,
heroCoords.y,
monstersCoords[i].x,
monstersCoords[i].y,
currentGrid);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
,
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%2fstackoverflow.com%2fquestions%2f55305115%2ffinding-closest-possible-coordinates%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I used pathFinding.js library and finding is very simple:
currentPath = finder.findPath(heroCoords.x,
heroCoords.y,
monstersCoords[i].x,
monstersCoords[i].y,
currentGrid);
add a comment |
I used pathFinding.js library and finding is very simple:
currentPath = finder.findPath(heroCoords.x,
heroCoords.y,
monstersCoords[i].x,
monstersCoords[i].y,
currentGrid);
add a comment |
I used pathFinding.js library and finding is very simple:
currentPath = finder.findPath(heroCoords.x,
heroCoords.y,
monstersCoords[i].x,
monstersCoords[i].y,
currentGrid);
I used pathFinding.js library and finding is very simple:
currentPath = finder.findPath(heroCoords.x,
heroCoords.y,
monstersCoords[i].x,
monstersCoords[i].y,
currentGrid);
answered Mar 24 at 13:30
BT101BT101
2092626
2092626
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55305115%2ffinding-closest-possible-coordinates%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
This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: buildnewgames.com/astar . a* is also mentioned in the SO answer you referenced
– Steven Stark
Mar 22 at 17:44
even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs
– juvian
Mar 22 at 17:46
I found A* library but in usage github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this?
– BT101
Mar 22 at 18:46