SiriusHardware
Posts: 503
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Kaleidoscope

Wed Jun 26, 2013 7:08 pm

Something, don't know what, made me think of Kaleidoscopes (an optical pattern making toy, if you haven't come across them before) and I thought maybe this would be a good moderately complex graphical project to tackle in Python.

It appears that the term Kaleidoscope / Kaleidoscopic is more often applied to Kaleidoscope-like mirrored / reflected photo effects nowadays, but what I want is to make something that looks very like the effect produced by the bits of coloured glass / plastic used in the traditional toy.

I'm assuming Pygame would be where to start with this, but maybe I'm wrong: Is there a better way / better toolbox for this?

The basic approach will be to have a circular display area populated with a large number of simple 2D shapes. All of these objects initially need to be able to rotate around the centre of the circle as though glued to a circular cardboard base and disappear / reappear as they enter / exit the upper right quadrant. Once that's working, the other three quadrants of the display will be filled with appropriately reflected / mirrored copies of what is visible in the upper right quadrant.

That's the first phase:

Second phase would be to allow the pieces to push / move each other around on the circular 'base' as the Kaleidoscope is 'turned' to make an infinitely variable succession of patterns. They will ideally never overlap, but preferably loosely interlock side by side. This bit is where I think it is likely to get quite complicated.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Kaleidoscope

Thu Jun 27, 2013 7:38 pm

You'll need to use a physics engine like Box2D or PyMunk, but don't get your hopes up as this sounds like a big ask for the Pi using Python.
Maybe better looking at a C/C++ solution.

Dave.
Apple say... Monkey do !!

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Kaleidoscope

Fri Jun 28, 2013 9:31 am

I would use the graphics processor. It's not too tricky to fiddle with shaders via pi3d (but it's certainly not a snip either) @hesspet has ported some of the shadertoy shaders to pi3d compatible format, including the kaleidoscope one:
youtube here
The advantage of using a shader is that it's blindingly fast. The disadvantage is that you only have 'one pass' and it limits what you can do.

You could use pi3d to actually define the coloured 'shards' inside the tube, but as soon as you start to do anything beyond very simple physics (as they tumble) then python will start to creak. Having said that, demos/CollisionBalls.py will run with 30 balls bouncing off each other. To do the quadrant thing you could mask the shards with two 'almost transparent' planes and redraw them at different rotations/reflections. If you do this in the right order the gpu will discard pixels drawn behind other object even though they are effectively see through.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

PiGraham
Posts: 3939
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Kaleidoscope

Fri Jun 28, 2013 9:46 am

I should think the rough outline would be to paint the "glitter" to a texture image that is applied to a "pie segment" shaped flat mesh and translate/rotate/mirror the mesh to various positions around the circle.
Unless you want to animate the "glitter" you don't need a physics engine, and even then, very simple linear motion may be good enough.

Alternatively, define the mirror arrangement as a mesh with reflective material properties and render it against the "glitter" background plane. Rotate the mesh and you get the kaleidoscope image. This relies on the renderer handling multi-surface reflections, which it may not do, or may be slow.

I don't know much about it, but I would think PyGame could do the job.

SiriusHardware
Posts: 503
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Kaleidoscope

Sun Jun 30, 2013 11:05 pm

PiGraham wrote:I should think the rough outline would be to paint the "glitter" to a texture image that is applied to a "pie segment" shaped flat mesh and translate/rotate/mirror the mesh to various positions around the circle.
Unless you want to animate the "glitter" you don't need a physics engine, and even then, very simple linear motion may be good enough.
.
Thanks for the interesting responses:-

I would indeed need to be able to animate the individual shards - sort of. If you've ever played with a real Kaleidoscope you'll know that as you turn the rotating part of the toy, the shards tumble, fall and generally shift around, sometimes quite suddenly, forming often startling new images as they do so. In my opinion no Kaleidoscope simulation would be worthy without this effect, so yes, it would need to have a primitive physics engine of sorts.

Forget about the mirror / reflection aspect for the moment and imagine the following: a shallow glass circular tray seven-tenths full of thin, flat, transparent coloured shards of glass. The tray has a raised glass rim one shard high so the shards can't escape off the edges. The shard layer is only one shard deep: There is an overall glass lid fitted over the top so the shards can't escape from the shallow glass tray.

So the shards can move / slide anywhere they can, within one plane only. The glass rim prevents them from leaving the glass tray at the edges, and the presence of the other shards means that their overall ability to move is limited: They may move until they collide with other shards, and they may move those shards, but they can never overlap with other shards. (This last rule is not strictly true for real Kaleidoscopes - I have decided to make it a rule for this digital version).

Now, pick this hypothetical device up and hold it vertically. The shards will slide downwards as far as they can, and tumble and fall against each other to form a random pretty pattern. Now, still holding it vertically, rotate the whole thing either clockwise or anticlockwise. The shapes which are rotated to the top by this process will naturally try to fall downwards into the spaces between the other shards, probably shifting some of them as they do so. This is the essence of how the pieces in a Kaleidoscope should behave, and is therefore the effect that this project would have to try to reproduce.

Having achieved that (!!!) the mirror effect would then have to be added, making only the top one quarter or one sixth segment of the whole tray visible, and mirroring / reflecting the visible content of that segment to the other four or six segments.

PiGraham
Posts: 3939
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Kaleidoscope

Mon Jul 01, 2013 5:26 am

Have you looked at this?
http://www.petercollingridge.co.uk/pyga ... simulation

You would need to render the shards then use that image as texture on multiple segments. So, you need to render to texture. I don't know the details of that using Python.

This may help

http://comments.gmane.org/gmane.comp.python.pygame/6346

SiriusHardware
Posts: 503
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Kaleidoscope

Tue Jul 02, 2013 7:04 pm

PiGraham wrote:Have you looked at this?
http://www.petercollingridge.co.uk/pyga ... simulation

You would need to render the shards then use that image as texture on multiple segments. So, you need to render to texture. I don't know the details of that using Python.

This may help

http://comments.gmane.org/gmane.comp.python.pygame/6346
Wow, those are terrific leads, thank you - using filled circles like those in the example in the first link as the preliminary 'shards' would certainly simplify the initial version.

Return to “Python”