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.

Newcomers AppGameKit Corner / Wrapping arrays

Author
Message
Plotinus
15
Years of Service
User Offline
Joined: 28th Mar 2009
Location:
Posted: 26th Jul 2017 19:58
Hi there,

I had a lot of fun with DBPro back in the day and am looking to get into AGK.

One thing I'd like to do with AppGameKit is to generate terrain, probably by using an array as a heightmap and fiddling about with it in the array before painting it out onto the screen and turning it into an image.

Because I want the terrain to wrap around, like it covers a whole planet, this would be very much easier if it were possible to "wrap" around the array. E.g. suppose I have an array of [10,10] and I have something like this (just pseudo-code to illustrate what I mean). It's intended to look at each point on the map in turn and see what the average height is of the nine-point square around that point:



Clearly if the heightmap array is 10*10 this will result in illegal operations, because it's going to be trying to look at data in locations like 0-1 and 10+1 and so on. What I want is for it automatically to wrap around, so that a call to (say) heightmap [5,10] will be interpreted as heightmap [5,0]. Or (say) heightmap [-1,1] will be interpreted as heightmap [9,1]. And so on.

So the question is: is this possible?
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 26th Jul 2017 20:21
you could try a simple clamp function, I just ran the below code and it did not overflow on the array, not sure if this would meet your needs.

Plotinus
15
Years of Service
User Offline
Joined: 28th Mar 2009
Location:
Posted: 26th Jul 2017 20:31
Thank you, this is straightforward and makes sense. It will stop the overflow but it won't wrap around. But now you've given me the idea of doing it in this sort of way I'm sure this function can be modified into a wrapping one. Thanks!
jhanson
6
Years of Service
User Offline
Joined: 19th Jun 2017
Location: USA
Posted: 26th Jul 2017 21:14
Here's a thought. If each node on the map stored the index of its previous and next node in each direction (x and y) , you could wrap from any point on the map by following those stored indexes (kind of like a doubly linked list).
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 26th Jul 2017 21:15 Edited at: 26th Jul 2017 21:20
switching the max/min variables would make it wrap

Coilwinder
11
Years of Service
User Offline
Joined: 28th Feb 2013
Location: Norway
Posted: 26th Jul 2017 21:56
Wouldn't Mod(value, max) do the trick? (the Modulo operation) I'm not sure how it will work with negative values with regard to the array indexing, some trial and error might be needed.

This seems to do work. Assuming the "min" value is 0, so omitted it. Also zero-based, so biggest result is max-1.


Apologies for any typos and strange grammar.
Plotinus
15
Years of Service
User Offline
Joined: 28th Mar 2009
Location:
Posted: 2nd Aug 2017 20:53
Thanks again for all the help here - much appreciated!
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 5th Aug 2017 15:07
Would not something like this work too?



Probably no need for it if what PartTimeCoder suggested works fine, but you might find this technique useful sometime

My Games - Latest WIP - My Website: Immortal.Digital - FB - Twitter
130,000 installs with AppGameKit and counting
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 5th Aug 2017 16:31 Edited at: 5th Aug 2017 16:38
Quote: " E.g. suppose I have an array of [10,10] and I have something like this (just pseudo-code to illustrate what I mean). It's intended to look at each point on the map in turn and see what the average height is of the nine-point square around that point:

For i = 0 to 9
For j=0 to 9
averageheight=0
For k=i-1 to i+1
For l=k-1 to k+1
Add averageheight, heightmap [k,l]
Next l
Next k
averageheight=averageheight/9
*do something clever with the averageheight info here*
Next j
Next i

"


I understand this is mock up code and the easy solution is to wrap the cords inside nested loops, but it's worth being aware there's a potentially mine field of overhead when that map gets bigger. Even at a tiny 10*10 field and 9 samples per point with a function on each axis, that's like 1800 function calls. You can get rid of the overhead, just depends on how big the map is going to be.

Even if you used some wrap function and got rid of the K loop and compressed the I loop into one would be a small start.

ie



could be ie


so a little more messing around, but we're doing to 3 function calls per sampling.. You can get rid of them all though, but the routine gets fatter..

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code

Login to post a reply

Server time is: 2024-04-24 16:01:29
Your offset time is: 2024-04-24 16:01:29