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 / future command request

Author
Message
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 14th Oct 2013 23:06 Edited at: 14th Oct 2013 23:10
if it would speed things up with this distance calculation...
would it be possible to add it as a agk command for a quicker response?


right now it works fine....but I think if tgc made it as a command it would calc faster then like this.



any thoughts? anybody else want this? I just think it would be best if that was calculated as an agk command so all I need to do is enter the 6 real numbers to spit out a distance number

edit: I have this new game concept engine that needs to calc distance for every object on the screen every sync.... I think that if it was a agk command then it would perform faster then it currently does.

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 14th Oct 2013 23:19
To help speed up your snippet, don't squareroot it, just compare squared values.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 14th Oct 2013 23:20
You can make it slightly faster if you use multiply instead of power:


At least it's faster in DBPro... not tested in AppGameKit but I always just do it this way now!

I doubt it will be added but you could put in a feature request in the proper place

oct(31) = dec(25)
Fallout
22
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 14th Oct 2013 23:48 Edited at: 14th Oct 2013 23:52
The slowest bit is the actual sqrt, so I don't think it being an internal command would make a massive difference, as it'll still be performing the sqrt.

Also as Prof said a lot of the time you don't need to sqrt. For example, if you're say checking to see if two objects are within a range of each other, rather than doing the sqrt and comparing against a range value, don't do the sqrt and compare against range value squared.

if dist# > sqrt(xdist#*xdist# + ydist#*ydist# + zdist#*zdist#)

can be replaced with ...

if dist#*dist# > xdist#*xdist# + ydist#*ydist# + zdist#*zdist#

Also if you absolutely need the distance, but you're working within a particular range, you can make a look up table. If your objects are usually going to be within 0 and 10 units of each other, make a 1000 entry look up table with precalculated sqrt() distances of input values from 0^2+0^2+0^2 to 10^2+10^2+10^2.

You can create a function with logic like ...



I've done this before in a few games using a lot of sqrts and found the precision was perfectly adequate. You can also do the same for sin/cos/tan etc.

You probably know all this but worth mentioning anyway!
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 15th Oct 2013 00:00
hmm, some vector commands for all were nice.
also a vector type with x,y,z for the 3d part for parameters and return values.
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 15th Oct 2013 01:55
@Fallout:
I remember long ago using pre-calculated arrays for Sin and Cos values, but I'd never imagined using it for Square roots. Fantastic

SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 15th Oct 2013 03:27
Precalculating square root.... Never thought of that...genius!

I'm using it to calculate lighting so having a table lookup should be easy to setup.

I'll try that one first but these are all great suggestions I never thought of.

Thanks !

Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 15th Oct 2013 04:36
Quote: "I remember long ago using pre-calculated arrays for Sin and Cos values..."

I tried this in AppGameKit some time ago and was surprised by the results.

From the tests I did, it appeared that using Sin and Cos was actually faster than using an array look up of pre-calculated results.

Never tried with Sqrt though.
Fallout
22
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 15th Oct 2013 10:00
Quote: "From the tests I did, it appeared that using Sin and Cos was actually faster than using an array look up of pre-calculated results."


I found that too in DBP I think it was. Really quite disappointing! I suspect it's because of the undercover logic to search/index arrays etc. If I remember rightly, look-ups for Sqrts was faster, but not to the extent you'd think. It was something like twice as fast at best, perhaps less.
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 15th Oct 2013 10:10
I wonder if it would be faster to use a memblock, than an array I mean. Like, a memblock with 100x100 floats, a bit like an image memblock but made from floats, each one with the distance to the centre. Then work out the offsets and grab the value from the memblock. A sin pre-calc could be a memblock with 360 floats.

If it isn't, then maybe some sort of really fast memory access would be nice, maybe just make an array then get a pointer to it, and offset ourselves to bypass all the safety checks and stuff.

Most of the time, I don't need super accurate distance checks - if we could get something faster somehow. Maybe being able to declare the accuracy, like the number of decimal points when calling SQRT.

I am the one who knocks...
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 15th Oct 2013 10:48
Older ARM chips did not have much in the way of FPU hardware - so it that case a lookup may well be faster.

-- Jim - When is there going to be a release?
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 15th Oct 2013 10:51
Quote: "Also if you absolutely need the distance, but you're working within a particular range, you can make a look up table"


I've always used lookups for sin/cos/tsn where 1 degree of accuracy is sufficient. I'm a bit concerned that it's slower I use them all the time for object movements, there's usually half a dozen happening in any frame in some of my apps.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Oct 2013 11:12
I've been using the sqrt method I posted in AppGameKit and at times quite a lot per loop with little or no lowdown. Maybe it's time to do some simple tests...

oct(31) = dec(25)
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 15th Oct 2013 11:27
Yeah, let's do that Baxslash, I'm interested in seeing how different the methods are in Android - I mean any delays are probably negligible on a PC, but on a low end tablet that could easily be a performance issue that we can take steps to avoid.

I'll do some brute force tests tonight with my laptop and galaxy tab 2.

I am the one who knocks...
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Oct 2013 12:26 Edited at: 15th Oct 2013 12:27
I did a few quick tests. Sqrt runs 1 million times in 50ms for me... I'm not going to bother making a lookup table, I tried newton's method (and the Quake method using "0x5f3759df") and couldn't be bothered waiting for it to finish even at one iteration.

Using multiplication runs 1 million times in about 45ms, power runs about 85ms.

Honestly, if I run my function 1 million times per loop it shouldn't take more than 200ms for a 3D distance calc. That's roughly 0.0002ms per calculation. I'm not going to worry about it...

oct(31) = dec(25)
Fallout
22
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 15th Oct 2013 13:28
Sin and Cos can be the bane of your life. For example in Dreadcom I have maybe 100 ship pieces per ship, all needing to be positioned relative to the angle of the ship. That's a lot of trig.

To reduce that I use vectors after an initial calculation. So I work out a forward vector forwardx=sin(shipang) forwardz=cos(shipang). I work out a right vector, which is actually rightx=forwardz, rightz=forwardx *1

Ever ship component has an offset from the ship centre on the X,Z. I can then just multiply the X and Z offsets by these vector values to position hundreds of objects. I only need to do 2 trig calculations per ship. I've hardly used sqrt at all.

I think my point is, if you're doing thousands of sqrts or thousands of sins per loop, there's probably a faster way you've not seen just yet.
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 15th Oct 2013 13:42
Ohh Fallout dude... you should have a look at the local movement commands. Like, if you have a ship, and know the offset in XYZ, you can position a second object in the same place as the ship, rotate it the same as the ship, then MoveObjectLocalX Y and Z to the offset - and it'll move, rotate and position itself as if it was stuck to the ship. You could even use a 2D array map of a ship floor, and position characters with this method, allowing artificial gravity and collision but with exact collision and path finding control if you need it.

It's like my favorite thing about AppGameKit, I use it for attaching particle effects to objects, adding arrows to bows, that sort of stuff. It would be easy to make a function like Position_With_Object(obj,ox#,oy#,oz#), and avoid all the trig calculations. I have no idea if that method would be faster though... just thinking it might be a nicer way to work than raw, scary trig

I am the one who knocks...
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 15th Oct 2013 15:14
@ baxslash

are you saying that using powers is slower then using times? So if I try your distance calc it should run slightly quicker? Ill test it out.

My code currently runs on my new nexus but barely. And It doesn't run at all on my blackberry (too slow). As expected its fine on pc..

It does effect my app since its calling the sqrt function 576 times per frame. I realize that's crazy to do that....but what im trying to do requires it to be smooth.

SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 15th Oct 2013 15:23 Edited at: 15th Oct 2013 15:24
ok I cut it down to 324 per frame.

Here is the code that im currently doing so you can sort of see what im trying to do.


a,w,s,d to move
space jump
mouse r-click In to look around

when it gets to the edge of the map....ignore the bright tiles that get dropped of in the back....I just need to code a limiter to the tiles to fix that.

Attachments

Login to view attachments
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Oct 2013 15:34 Edited at: 15th Oct 2013 16:09
Quote: "are you saying that using powers is slower then using times? So if I try your distance calc it should run slightly quicker? Ill test it out."

Yes, it's nearly twice as fast to use multiply than to use power. Someone pointed that out to me ages ago in DBPro and it seems to be the same in AGK.

If you are comparing distances then you don't need sqrt but if you need sqrt the best method I have tried (although I haven't tried lookup tables yet) is the AppGameKit built in command.

I'll try to take a look at your code but I don't have much time at the moment, perhaps someone else could step in?

EDIT: I did some quick lookup tests using this general equation and 2 lookup tables. The resulting answer was accurate but it was over 4 times slower than sqrt:


oct(31) = dec(25)
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 16th Oct 2013 01:37
ah...I just figured out my slow down... its the tile instancing...

the sqr root is probably ok with how I had it. But I will try taking out the powers and replace them.

Login to post a reply

Server time is: 2024-11-24 22:48:41
Your offset time is: 2024-11-24 22:48:41