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 / Distance in an RTS

Author
Message
sprcoll
12
Years of Service
User Offline
Joined: 26th Aug 2012
Location:
Posted: 16th Feb 2022 17:53

Hello all together, i have a question and would be happy, if someone helps.
I am working on an rts in 2D and thinking of a what is the best method for me to shot if the units reach a certain distance.

One method is to work with vectors and calculate the root of (vectorx1#*vectorx1#)+(vectory1#*vectory1#).

A second method i have tried is with collision commands (getspriteincircle) and the third one with (getspritedistance).

I think if i have many units that the app has a lot to calculate (Everytime calculating the distance between the different units). I guess that the getspritedistance calculates similar to the root on the vectors.

Is the (getspriteincircle funktion) a faster method or is the code behind similar to the first method?

Thank you in advance.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 16th Feb 2022 21:59 Edited at: 16th Feb 2022 22:36
this doesn't answer your question directly (Distance vs GetSpriteInCircle - which you can test yourself) but a notion of culling which distances to calculate/compare, instead:


[LMB] = Add Allies/Enemies | [RMB] = Toggle Debug (view ranges)

then, only checking for enemies within a given sprite's range when they are ready to fire/attack will further "cull", minimizing Distance checks even more-so.

add, i guess i'm suggesting that you won't find a faster distance check for all allies vs all enemies than Box2D (sensors) followed by the sweeps above
Arch-Ok
AGK Developer
5
Years of Service
User Offline
Joined: 11th Jul 2019
Location: Bursa/TÜRKIYE
Posted: 17th Feb 2022 11:57 Edited at: 17th Feb 2022 12:26
Ok, I happen to see this problem twice for now and
since my first ever released game 'vpop' (listed in the agk showcase, would appreciate much if andorid user fella's lend a hand that lol)
did not downloaded as much as I expected and set me sad about game programming, I can spare time for codes.

At the topic 'inverse square root' I'd suggested to not to use sqrt.

So, if you only check if a unit is in range or not (bool), you definetly do not have to use sqrt.

You check the absoulte value of x differences, if it is below the ranged distance then you check the absolute value of y distances and if it is below the ranged distance then the unit is in range.

So, instead of;

Distance = Sqrt((Ax - Bx) * (Ax - Bx) + (Ay - By) * (Ay - By))
If Distance <= Range Then InRange = True

If Abs(Ax - Bx) <= Range
If Abs(Ay - By) <= Range Then InRange = True
EndIf


Then, another problem occurs, do we have to check all units against each other?
No!
We may subdivide the map into grids and group the units according to their current grids.
And then we check only the units which are in the grid_range.
Let's say;
gridsize = 100
range = 190
xpos = 543
ypos = 409

so, our unit is in the grid 5, 4 and, our grid_range is +-2 (range = 190) -> (1 + Floor(Range/GridSize))
we check the units which are on the grids, from (3, 2) To (7, 6).


So, every grid will hold the ID of the unit which are on it and the number of total units on it.

About the performance, I compared @Virtual Nomad's code in equalavent circumtances (unit count, resolution) and saw that, when the unit count on the map is low, my code is slower but when it goes like 1024, my code is faster, besides no sprite usages.

The grid size and unit's ranges directly affects the performance on my code. And there is a limit which a grid can hold maximum units.

And If I was making a 2D game with sprites, I'd choose @Virtual Nomad's code for sure.

Hope this helps...




Simply drag the mouse on the screen to set our hero's new position, it is fun to watch, like a spider is walking on it's web.

This code may not be the best solution and is avaible for optimization for better performance;






P.S. Also @Virtual Nomad's units are sorted due to their distances.

Attachments

Login to view attachments
Game_Code_here
4
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 17th Feb 2022 23:19
Use a shooting timer.

Just inc a timer then stop the bullet unless it hits collision.

Why?

Because it is more real life.

What I do is use a array for my timer for each bullet.
sprcoll
12
Years of Service
User Offline
Joined: 26th Aug 2012
Location:
Posted: 18th Feb 2022 17:33
Many thanks for the postings. I thought about a good solution for many days, without success. The codes look very complex, so i will try to read and work with them this weekend. Now i have new ideas.


Game_Code_here: Thanks, but this is well known. I am using arrays and timers for the shots. I only was searching for a good solution to check the distance.
Game_Code_here
4
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 18th Feb 2022 23:58
get_distance=GetSpriteDistance(sprite Id,Sprite id)

It is the best way.



Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 26th Feb 2022 14:16 Edited at: 26th Feb 2022 14:17
Quote: "get_distance=GetSpriteDistance(sprite Id,Sprite id)

It is the best way."


I disagree. If you're only looking for objects that are within a certain distance, then you don't need the actual distance. You can drop the square root from the calculation and compare it to the squared value of the range you wish to check and speed things up greatly.

Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Game_Code_here
4
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 26th Feb 2022 21:42
Phaelax

I love your way of getting distance.

I will try this for myself.

Login to post a reply

Server time is: 2024-11-21 12:47:48
Your offset time is: 2024-11-21 12:47:48