Roulette expression

Moderators: Disciple, zlovatt

blockrocker
Posts: 16
Joined: March 25th, 2009, 4:01 pm

Hi!

I'm trying to create a virtual roulette in AE that would first spin 4 seconds and then after that ease down and stop at a random moment in time (producing a random result just like a real roulette).

I'm a bit stuck with the expression and can't figure out how to make it ease out and stop at a random point in time.

Any help will be appreciated!

Thanks!
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am

Try this expression (on the rotation property):

Code: Select all

t0 = 4; //[sec]
Nrounds = 4;
angleVelocity = Nrounds*360/t0; // [deg/s]
seedRandom(index, true);
t1 = t0 + random(t0/4,t0/2);
y0 = angleVelocity * t0;
t = time - inPoint;
a = angleVelocity/(t0-t1)/2;
c = y0 - a*Math.pow(t0-t1, 2);

if (t<t0)
	ang = angleVelocity * t;
if (t>=t0 && t<t1)
	ang = a*Math.pow(t-t1,2) + c;
if (t>=t1)
	ang = c;

ang
It will make 4 full rounds (as you wanted, but you can change that with the variable Nrounds) in the interval of 0..t0
and then will ease to a stop in the interval of t0..(t0+t1), where t1 is random. I've given t1 a random number value between (t0/4) and (t0/2), you can change that in the code above if you want slowers stops or faster stops.

Let me know if that's what you wanted.

Koby.
blockrocker
Posts: 16
Joined: March 25th, 2009, 4:01 pm

Hi!

Thank you very much for your help, and sorry -I had no idea that it would get that complicated!

The expression works very well otherwise, but when I preview the animation, it results in the exact same position every time. I would really need to stop it in a random position after the initial animation, so that every time I play the animation, the results are different. You could think of it as a internet casino virtual roulette in which the result is different on every spin. I'm not sure anymore if that is even possible to achieve in AE?

The reason I'm asking this question is that I'm trying to build sort of a custom lot game for live situations with real people, in which I could randomly select people to respond based on the result of the AE animation. But it really requires that result of the animation is random and different every time.

If you can help, I really appreciate it a great deal!

Thank you!
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am

It is actually random, but since it always uses the same seed, this is always the "same random" value.
Change the seed number (on line 4 from "index" to a different number) and you will get different random stop angles.
You could make as many animations as you want, just change the seed each time to a different value (0,1,2,3,....) and you will get each time a different stop value.
(That's why the expression is so complicated... If I wanted it to stop each time at the same location it would have been much simpler).

Koby.
blockrocker
Posts: 16
Joined: March 25th, 2009, 4:01 pm

Thanks for helping out!
If I understand you correctly, is it simply not possible in ae to get different results for exactly the same comp on different renders, instead to have a different results a parameter needs to be manually changed every time?

I know this can be done in Flash since the web is full of flash based gambling games which produce different random results on each click (without the need to change any parameters between the mouse clicks). Is there something in the way ae works that makes this kind of functionality simply impossible?
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am

Flash is conceptually different, because it includes a software (ActionScript) that runs all the time in the background of the flash animation, thus it can change the random SEED each time a game begins. However in AE you don't have that software control above the renders. You might however use a script to help in that process... You will have to run the script each time you want a new animation, and it will give you a different seed.

However, I'm not totaly understanding how you want to achieve your Casino using AE... If I understood, I could try to find a way for you a achieve this... Are you planning to do a render each time the user wants to play the roulette ? Or you are making all kind of random animations ahead, and then choose one of them when you need one ?

Koby.
blockrocker
Posts: 16
Joined: March 25th, 2009, 4:01 pm

Thanks again!

What I'm trying to do is this:

I have a bunch of students in my animation class. I want to be able to question them about their homework/other assignments so that it is not I who chooses the one who has to answer but random luck instead. I think it could be exciting for the students if I spinned a virtual wheel to see who has to answer next. It would really force everyone to pay attention at all times, since you could never know if you are the next one who has to answer a question.

The problem is that if I need to change the random seed manually each time that I spin the wheel, it won't feel random to the students. And indeed, even I myself would probably learn pretty fast what seed produces a certain student, which would be kind of unfair and not really random. :)

So basically I would like this thing to behave in the following way:

I hit spacebar (a realtime ram preview is not necessary really) and the wheel starts spinning.. The excitement builds as the wheels spins.. then it starts to slow down and finally it stops at a random picture (one of the students). And that student has to answer the question I presented for them before the spin. :)
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am

I understand now.
Great idea ! :)
In order to achieve what you want, you could write a simle script that would write a random number for the seed (or for the ending time: t1). And each time, before you start the animation, you would run the script to give you a different random number. That should be a very simple script... If I have some time later I will try to write you that script.

Koby.
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am

Ok, I had a little time, so here it is:

1. Create a NULL in your composition, and place it as the first layer.
2. Add to that NULL a slider, and rename it to "myrand"
3. Here is the script you need to run in order to create a random value for "myrand":

Code: Select all

{
	// create an undo group 
	app.beginUndoGroup("Randomize Roulette");

	var curItem = app.project.activeItem; 
	var T = curItem.frameDuration;

	// check if comp is selected 
	if (curItem == null || !(curItem instanceof CompItem)){ 

		// if no comp selected, display an alert
		alert("Please establish a comp as the active item and run the script again"); 

	} else { 

		now = new Date();
		seed = now.getSeconds();
		var random_number = Math.random(seed);
		curItem.layer("Null 1").effect("myrand")("Slider").setValue(random_number);

		// close the undo group
		app.endUndoGroup(); 
	}
}
4. And that's the modified Expression, to take into account the random value from the slider "myrand". Write that instead of the previous one I gave you:

Code: Select all

rnd = thisComp.layer("Null 1").effect("myrand")("Slider");
t0 = 4; //[sec]
Nrounds = 4;
angleVelocity = Nrounds*360/t0; // [deg/s]
t1 = t0 + t0/4 + rnd*t0/2;
y0 = angleVelocity * t0;
t = time - inPoint;
a = angleVelocity/(t0-t1)/2;
c = y0 - a*Math.pow(t0-t1, 2);

if (t<t0)
	ang = angleVelocity * t;
if (t>=t0 && t<t1)
	ang = a*Math.pow(t-t1,2) + c;
if (t>=t1)
	ang = c;

ang
Each time you want a different animation, run the script and then you will have a different stop position.

Koby.
blockrocker
Posts: 16
Joined: March 25th, 2009, 4:01 pm

Cool, thank you very much for your help! :)
Yenaphe
Posts: 84
Joined: February 3rd, 2009, 6:30 pm
Location: Paris - France
Contact:

I'm wondering if the use of seedRandom() on top of the expression couldn't help with randomising the random seed.
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am

Sébastien,
That will not provide a solution to the problem... If you run seedRandom (without any arguments), it will indeed randomize the seed each frame, but that would make the animation totaly jumpy ! It will not settle down on a specific value, it will keep generating random stop positions each frame...

Koby.
blockrocker
Posts: 16
Joined: March 25th, 2009, 4:01 pm

I have now tested the script in a real world situation and it works great, I'm having alot of fun! :D So thank you very much for helping out with this kobyg, you are a genious!

On a side note, would it be difficult to make a button in the script that would perform the script, so that I could have it docked as a UI panel in AE? At the moment I'm loading the script every time from file-->scripts-->load script. If that is possible, then maybe it would be possible to even link the script so that it also automatically starts the render? That way I would simply hit a single button and let the magic happen! :D

I'm not expecting you to sacrifice your time to this kobyg, I can imagine how busy are and you helped me already a great deal! But if anyone here knows how it could be done, I'd appreciate any help or tips greatly!
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am

Hi,
I took the time to make the script a dockable panel,
with a button for spinning the roulette:
It will randomize the parameter for stopping the roulette at a random point (just like the previous script)
and will start automatically a RAM Preview (I could not find a way to do a normal PLAY preview.
If someone knows how to do that, I would love to know...).

As any other dockable panel script, you will have to copy the script to the "...\Scripts\ScriptUI Panels" folder
and run it (only for the first time) from the Windows menu. Then you will able to place/dock the panel wherever you want.

Enjoy,
Koby.

p.s.: The GUI (in order to make it a dockable panel) was based on the "Quote of the Day" script by Relu Pene.
viewtopic.php?f=3&t=1410&hilit=Quote
Thanks Relu (glad you were bored that day :wink: ) !
Attachments
Roulette.zip
(1.39 KiB) Downloaded 935 times
nab
Posts: 203
Joined: November 29th, 2005, 3:00 am
Location: Royan
Contact:

As an alternative, you can give a try to this 30-lines script. It doesn't require Null nor expression.
When the start button is clicked, the comp time is set to the wheel layer in point (typically 0), and two rotation keyframes are created (with ease interpolation). The second keyframe value is picked randomly within a specified range. When you want to select one of your students, you could just click Start and then press the space bar to start the wheel and wait until the lucky guy is known. (You may also want to disable View>>Show Layer Controls before starting the preview)

Code: Select all

var spinDuration = 8; // seconds 
var minRev = 5; // minimum number of revolution
var maxRev = 10; // maximum
var easeOutStart = new KeyframeEase(0,50); // easing out first key
var easeInEnd = new KeyframeEase(0,75); // easing in second key         

var title = "Spinning Wheel";
var startBtnName = "Start";
var pal = this instanceof Panel ? this : new Window("palette", title);
pal.startBtn = pal.add("button", undefined, startBtnName);
pal.startBtn.onClick = function ()
{
	var comp = app.project.activeItem;
	if (comp instanceof CompItem && (comp.selectedLayers[0] instanceof AVLayer || comp.selectedLayers[0] instanceof TextLayer))
	{
        var wheelLayer = comp.selectedLayers[0];
        var rotStartVal = wheelLayer.rotation.valueAtTime(wheelLayer.inPoint,false);
        var rotEndVal = (minRev + Math.random() * (maxRev-minRev)) * 360;
        var noEase = new KeyframeEase(0,0.1);
        
        app.beginUndoGroup(title);
        comp.time = wheelLayer.inPoint;
        wheelLayer.rotation.setValuesAtTimes([0,spinDuration],[rotStartVal,rotEndVal]);
        wheelLayer.rotation.setTemporalEaseAtKey(1,[noEase],[easeOutStart]);
        wheelLayer.rotation.setTemporalEaseAtKey(2,[easeInEnd],[noEase]);
        app.endUndoGroup();
    }
    else Window.alert("Select the wheel layer.",title);
};
pal instanceof Window ? pal.show() : pal.layout.layout(true);
Post Reply