Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Get sprite closest to X,Y?

Author
Message
JLM
9
Years of Service
User Offline
Joined: 21st Jul 2015
Location:
Posted: 17th Oct 2015 20:54
Is there a way to return the spriteID of a sprite thats closest to a pre-set X,Y?
-Writing programs for years, new to AGK2
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 17th Oct 2015 21:27 Edited at: 17th Oct 2015 21:28
this?
GetSpriteInCircle ( iSprite, x1, y1, radius )
integer GetSpriteHit( x, y )
AGK (Steam) V2 Beta .. : Windows 10 Pro 64 Bit : AMD (15.7.1) Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
Jack
20
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 17th Oct 2015 21:44 Edited at: 17th Oct 2015 21:46
Hello
I have created some lines of code for you, if you mean something like that? The following functions can check a bunch of sprites for distance.
The new array commands will sort the array so it will be easy to get the nearest sprite to a given point.



[/url]
JLM
9
Years of Service
User Offline
Joined: 21st Jul 2015
Location:
Posted: 17th Oct 2015 22:34
Both methods work nicely, if the sprite ID is known Problem is, the sprite ID is random and could be anything.

Markus:
I tried this method before, but it requires that I know the closest spriteID already

Jack:
Thats an amazing bit of code, and in my opinion should be immortalized in the commands/examples area Again, though, it only knows/interacts with the sprites created in your array, and ignores sprites that it does not already know the ID for (Or am I missing something?
-Writing programs for years, new to AGK2
JLM
9
Years of Service
User Offline
Joined: 21st Jul 2015
Location:
Posted: 17th Oct 2015 22:50
To see what I mean, Jack, replace:



with:

-Writing programs for years, new to AGK2
Jack
20
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 17th Oct 2015 23:39
If you remove the array, you will need to define a legit start and end-point for the search function.
This is not effective as an array, if you use a lot of sprites.



[/url]
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Oct 2015 09:01
You can make this more efficient:

Distance# = Sqrt(Dist_X# * Dist_X# + Dist_Y# * Dist_Y#)

can simply be:

Distance# = (Dist_X# * Dist_X#) + (Dist_Y# * Dist_Y#)

because you are only sorting by distance, not actually calculating the distance.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 19th Oct 2015 10:57
@BatVink
just in mind, is this valid too then?
sort# = (x2#-x1#) + (y2#-y1#)
AGK (Steam) V2 Beta .. : Windows 10 Pro 64 Bit : AMD (15.7.1) Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Oct 2015 11:24 Edited at: 19th Oct 2015 11:29
[EDIT]
I don't know if that is valid, probably not, you still need to square the values.

52 + 52 = 25
5 + 5 = 10

But in your calculation you could also get 92 + 12 = 82
9 + 1 = 10

A triangle where one side is almost zero (9,1) is very different to a triangle where both sides are the same length (5,5).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Jack
20
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 19th Oct 2015 20:30
BatVink is right, to get your information you dont have to use the root. Its only useful, once you need the corret distance to each sprite.

[/url]

Login to post a reply

Server time is: 2024-11-16 22:06:00
Your offset time is: 2024-11-16 22:06:00