ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Color changes on velocity

FAQ   •   VIEW ALL
 Color changes on velocity on May 27, 2012 at 12:26:38 am

I have a test i'm doing that has an object moving around, the faster it travels the color changes. Here's the example - https://www.dropbox.com/s/r98grbtnd1uawt8/V_test5.mp4

Expression for velocity.

factor =5;
Math.abs(thisComp.layer("G").position.velocity[0])/factor;

I have two problems with the current animation.

1. The object that samples the colors to the left over shoots in height sampling black color. This object moves higher in height the faster the dot moves. How can i clamp the vales so that the object doesn't go past a certain height value?

2. Notice how the colors on the band of particles do not fade. Instead it's solid from one color to the next. This is the result of the object moving way too quickly from fast to slow. Is there a way to control the speed in which the object moves? For example, how can the values be "smoothed" so that as the object samples the color it's slow enough to gradient correctly on the band of particles.

I hope i explained what i'm asking for. Any questions or suggestions is much appreciated.

Thanks!

Alain

 Re: Color changes on velocityon May 28, 2012 at 11:20:21 pm

[Alain Lores] "
factor =5;
Math.abs(thisComp.layer("G").position.velocity[0])/factor;"

This is only sampling the X component of the velocity of your layer. Is that what you're after? You probably noticed that velocity has 2 or 3 components, these correspond to the rate of change on the X and Y (and Z on a 3D layer) axes. If you want to work with a single value, and you don't want to split the velocity into separate axes, then you probably want to use the "speed" property:

``` factor = 5; thisComp.layer("G").position.speed/factor; ```

The Math.abs() function is not necessary for the position speed.

[Alain Lores] "1. The object that samples the colors to the left over shoots in height sampling black color. This object moves higher in height the faster the dot moves. How can i clamp the vales so that the object doesn't go past a certain height value?"

There are a couple of ways to accomplish this. One would be to use the Math.min() function:

``` max = 500; factor = 5; s = thisComp.layer("G").position.speed/factor; Math.min(max, s); ```

Math.min() will return the smaller of the two values passed to it, so whenever "s" is greater than "max" it will return "max" effectively clamping the highest value returned to "max."

[Alain Lores] "2. Notice how the colors on the band of particles do not fade. Instead it's solid from one color to the next. This is the result of the object moving way too quickly from fast to slow. Is there a way to control the speed in which the object moves? For example, how can the values be "smoothed" so that as the object samples the color it's slow enough to gradient correctly on the band of particles."

There is a smooth() function you could use that may help in this case. You'll want to smooth() the position property of the sampler, but it may be more complicated than that depending on how you've set up the expressions.

Darby Edelen

 Re: Color changes on velocityon May 30, 2012 at 1:36:00 pm

Darby thank you for your help.

The Speed property worked better! it looks like the values are less jumpy. It helped make the colors blend better too.

https://www.dropbox.com/s/sx3uzdwuqlvo756/Test7.mp4

I'm not the best expresioneer i normally edit and link expressions together. I'm having a hard time figuring out how to link your expression to the arrow. The arrow already contains an linked expression to the Y that determines its height based on speed. This may seem like a dumb question but how can i use both of these expressions in the Color_sample layer?

 Re: Color changes on velocityon Jun 1, 2012 at 9:49:06 pm

[Alain Lores] "This may seem like a dumb question but how can i use both of these expressions in the Color_sample layer?"

Combining them should be fairly simple. You just need to store the result of the first code in a variable and then pass it to the second part of the expression:

``` factor = 5; s = thisComp.layer("G").position.speed/factor; [10, 720 - s]; ```

I can't view your video at work, so I've probably got your last line wrong, but hopefully the above helps you make better sense of it.

Also be aware that the sampleImage() function is incompatible with the "Render Multiple Frames Simultaneously" feature. If you find that this feature speeds your rendering then you should bake any expressions that include the sampleImage() function into keyframes before your final render.

To do this, select the property with the sampleImage() expression and use the Animation > Keyframe Assistant > Convert Expression To Keyframes menu option. This will disable the expression and create a keyframe at every frame. If you need to make changes that will affect the sampleImage() expression then you'll need to re-enable the expression. You may also need to delete the keyframes.

Darby Edelen