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 / fastest way to calculate distance in 2d ?

Author
Message
haliop
User Banned
Posted: 26th Jul 2013 20:40
i think i rad somewhere that
sqrt((x1-x2) * (y1-y2) is kinda slow is there a faster way then that?

kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 26th Jul 2013 21:09
I don't think there is any other method than that.

Also, "kinda slow" - unless you're going to have lots of such checks, it doesn't really matter.

Follow me on twitter! @MotionStruct
Motion Struct blog
haliop
User Banned
Posted: 26th Jul 2013 21:21
i have about from 100 to 500 per cycle..

Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 26th Jul 2013 21:21 Edited at: 26th Jul 2013 21:21
If you are comparing two distances you can leave out the square root and the result will be equivalent.

If you already have the distance of one then square that one before you compare to the other.

Other than that you can look at some fast square root algorithms which lose accuracy but may be fine for your needs.

Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 26th Jul 2013 21:22
Quote: "sqrt((x1-x2) * (y1-y2)"

Don't you mean

sqrt( (x1-x2)^2 + (y1-y2)^2 ) ?

Anyway, it's not really a question of if there is a faster way, it's a question of do I need it ?

for movement between two points, you can often work with X and Y without needing to combine them - if you wanted a bullet to hit an enemy, you could just set the vx and vy of the bullet, based on the ratio of (x1-x2) to (y1-y2).

So if the enemy was +100, -500 distance, a speed of +1,-5 would be going the right way to hit it, without needing the extra maths.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 26th Jul 2013 21:33
what are you doing with this distances?
maybe there exists a other solution.
haliop
User Banned
Posted: 26th Jul 2013 22:35
ok got it .. this was kinda stupid never even had to used it , i use it for "line sprites" and i didnt even had to use it since i already have a var for Length... so .. that was just me not noticing and trying to overcome an obstacle which wasnt even there..

thank you for your awesome quick respones.

so now instead of using the calculation i just use a value.... idiotic me..

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 27th Jul 2013 11:31
I always do it this way as I was told that using powers is slower than multiplication:

Although I haven't actually checked it for speed I haven't had to use it more than a hundred times per loop...


this.mess = abs(sin(times#))

Login to post a reply

Server time is: 2024-11-24 18:44:44
Your offset time is: 2024-11-24 18:44:44