Randomness + AI

Greetings! Im wanting to here what advice the community has on systems (border line AI) for smartly placing objects in relation to other objects. For example, I have a game that has “gates”. Each gate is two points connected by a beam. If the ball crosses that beam the gate is destroyed and another gate is randomly made.

On my to do list is to make it so that you can see where the next gates will be placed. The next step is to have to gates position them selfs in air (there currently on the sides of the screen) smartly so that you can line up multiple shots with one go. This also includes rotation.

Although that is a current goal, I’m more generally interested in the broader topic of random level design and things of that nature.

Edit: Was it something I said?


@Goatboy76 - I wasn’t sure what you were asking. If it’s how to find an algorithm to place your gates, that depends entirely on your exact setup. What you have described is fairly vague, which makes it difficult to give advice.

@Goatboy76 There’s randomness, pseudo-randomness and then there’s what humans perceive as randomness. Depends on what you are looking for. :slight_smile:

For randomness, grab the milliseconds. For pseudo-randomness, I guess Codea’s Lua uses the regular C-library which is okay, but some day, I will do a Lua version of the Mersenne Twister (if there isn’t one already.)

But for perceived randomness, that is, an evenish distribution across your playing field, I’d keep a table of locations and mark them as each is chosen at random. Or, create a table of them and randomize them by iterating through linearly and swapping each element with a randomly chosen one (card-shuffling algorithm).

@Ignatz Sorry for the misconception. I’m wanting to hear what “people” have to say on what an algorithm could look like for intelligently placing objects based on their properties. It will be up to me to figure out my own problem. Algorithm is a far better way to explain what I was trying to say. I gave that example to help explain what i was searching for.

@Goatboy76 - I think you’re looking for something that is random but also has a pattern, eg to line up gates. The best way to do it may be to draw a number of different layouts, see if you can come up with different patterns that work well, and then choose randomly from those patterns.

@syntonica The different kinds of randomness are quite interesting. I used set positions to place the “gates”. I don’t know much down that road.

@Ignatz Sounds like a plan. Ill see what I can come up with.

@Goatboy76 I think once you nail down what you want to be random, then it will all fall in place. Ignatz has a good idea, too. A lot of things that look random in games are really just things carefully chosen.

PS. Your goat makes me laugh. :slight_smile:

The problem with randomness in games or VFX is that ‘truly’ random numbers are really not much use. What you really need is something that gives you ‘control’ to express what you want to achieve whilst giving the perception that something random is happening. Having ‘control’ is the real issue.

For this reason, I really like Perlin noise to generate pseudo-random numbers. Effectively its just a mathematically, non repeating function that outputs an infinite string of numbers. If you sample very small intervals the values change very slightly, if you sample large steps it ‘looks’ like your generating a set of unrelated random numbers.

There are ‘lots’ of applications of this in computer graphics and is hugely versatile (check out Ken Perlins page here for examples: http://mrl.nyu.edu/~perlin/) - i’m sure this can be applied to give you some control of random placement of objects.

Thinking out loud here - the other way to think about your random scattering problem is to subdivide your playing area into a grid of x,y cells. Each cell has a ‘weight’ attribute associated with it (just a normalised value 0-1) that gives a statistical likelyhood of whether that cell will be filled with an object. i.e. 0 = never, 1 = always, with other values between 0-1 that indicate some sort of likeyhood/threshold it will be filled.

You can then iterate through your x,y table of cell weights and compare with a random value against this threshold - if its less than the ‘weight’ for that cell it gets filled with an object. The beauty of this approach is that for each level all you have to do it set up some tables with a bunch of pre-defined x,y cell weights and let your ‘level builder’ do the rest. You’ll then get a predictable set of random looking placement of objects within the set of constrains you’ve defined.

I use this a lot in VFX when doing crowd shots or wanting to propagate trees on a landscape etc… - you dont want crowds to spill over into rivers or trees cropping up in the middle of roads, you can then simply paint a greyscale ‘weight map’ that controls the placement of objects and write a bit of code to do the placement.

Hope this helps… :wink:

@syntonica Thats probley (get it?) true. Thanks for the complement. Every time I see that picture it reminds of my true home among my goat heard…

@andymac3d That “seemingly random” method using noise sounds interesting. I love how much control you could have with that method by taking numbers at different increments. Cells sound promising. For my purposes, there’d have to be a great many cells so that the placement of the gates could be more free and not look so pix-elated
Weighting the cells just takes it another marking down the geeky scale for me ;;).

Alright, I’m back. I’ve made a grid class that quite simply creates a table of points that forum a grid based on 2 parameters. 1. Length and Width of the grid 2. Space between the points.

At least that was plan any way. But, infinite loops are getting in the way. Anyone have a better method to de-bug infinite loops?

You say you have an unwanted infinite loop?
If you have a variable n that controls the loop and should stop it, then insert in your loop

if p>pmax then break end

Then you can check how the values of n evolve and why the loop doesnt stop.
A loop wont stop usually because.
-1 you do not increment n.
-2 the stop condition is badly writen (typo) and then always false

@Jmv38 Im really confused on your example. Is there supposed to be an upper case P? and what is n? Shouldn’t n be p?

@Goatboy76 - what he’s saying, is to include a counter somehwere in the loop that’s not stopping, and break out of the loop if the counter gets too big. Then at least you don’t have to close and re-open codea!

I’ll right, I’ll try that. Thanks

@andymac3d has excellent advice. Perlin noise is a great way to get ‘controlled randomness’. You can also use cellular automata algorithms for growing structure which might be relevant to your gates problem, and CAs are fun to write and play with. Check out Conway’s Game Of Life if you have never seen it before. Similarly Fractal geometry can be useful if you are looking for structure with some symmetry, although the math is a little daunting. Good luck!

@Goatboy76 - there is a demo Noise program in Codea (way on the right) that shows how to use Perlin noise…

As well as the Codea example @Ignatz pointed out - a nice intro to Perlin Noise is here:


@andymac3d - nice link, thanks :)>-

Here is quite nice slideshow by Perlin himself. :slight_smile:


And here is an example of using perlin noise in the vertex shader: