Incrementaly changing an object's color

About advanced scenes, and the Thyme scripting language used in Algodoo.

Incrementaly changing an object's color

Postby 10percenter » Mon Oct 14, 2013 8:28 pm

Is it possible to increment an object's color on collision?

Think of a ball bouncing, every time it hits the floor it progressively gets a brighter shade of red. Something like that.

Just thought I had...not critical but would add a nice effect to a scene I have been playing with.
10percenter
 
Posts: 7
Joined: Sat Aug 04, 2012 11:06 pm

Re: Incrementaly changing an object's color

Postby Xray » Fri Oct 18, 2013 7:07 am

Sure, very simple. There are two types of color properties in every geometry. They are "color" and "colorHSVA". You can change a color's hue, saturation, and opacity with either one, although in a slightly different manner. For the purpose of answering your question, I will use "color" for my example because that's the one that I am most familiar with and have most experience with. The color value is a small array of 4 elements which looks like this: color = [1.0,1.0,1.0,1.0]. Each element can have a value between 0 and 1.0. Reading from left to right, the elements are Red, Green, Blue, Opacity. In the example above, the color is pure white with a maximum opacity value of 1.0 which makes it opaque. The opacity value is a percentage divided by 100. So, for example, color = [1,0,0,0.5] is pure red with an opacity percentage of 50 percent. If you would like to incrementally increase the "brightness" of a color, you simply increment the color value(s) by some small number each time in a loop until you reach the max value of 1.0. You can do the same with the opacity if the scene requires it. There are many different ways you can do that, but I will show you one simple way here....

A sample script that gradually increases the Red from zero to max value is:

PostStep = (e)={color(0) < 1.0 ? {color = color + [0.01,0,0,0]}:{} }

With an increment value of 0.01 (with the sim frequency set to 60Hz), the color red will go from zero (no color) to full brightness in approximately 2 seconds. You can of course change the increment value to change the speed of the color change.

You can also make the color change up and down rhythmically by employing some simple math functions such as sin or cosin, but that goes beyond the scope of your question.

Hope this helps.

EDIT - I forgot about your question concerning collisions. You can do the same thing during a collision by employing the same technique of adding an incremental value to a color value each time an object collides. You of course do that in the "onCollide" property.

Example:
onCollide = (e)={color = color + [0.01,0,0,0]}}

If that makes the change too gradual, then simply increase the value to 0.05 or 0.10, etc.
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Incrementaly changing an object's color

Postby 10percenter » Mon Nov 04, 2013 3:42 pm

Awesome. I got it working the way I wanted pretty quickly. Now all I need to do is put in some trial and error to figure out how the colors work. Everything is too red right now!
10percenter
 
Posts: 7
Joined: Sat Aug 04, 2012 11:06 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 16 guests