Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Random rotation

Explorer ,
Dec 30, 2009 Dec 30, 2009

I have an object that Im currently rotating (360) degrees, I would like to give it a random rotation.

To move it in one direction, and then the other. I'm not sure the best way to do it.

Any advice/direction would be helpful

(below is the working code I have...I was experimenting with the TIMER to get it to rotate

every second based on a positive or negative random number.....I took out the timer, so you can see the code that works)

var spinTimer:Timer = new Timer(500, 1); // 1 second
var sumnum:Number
compass_mc.addEventListener(Event.ENTER_FRAME, compass);
spinTimer.addEventListener(TimerEvent.TIMER,randomizer);
function randomizer ()
{
sumnum=(Math.floor(Math.random() * (30 - (-30) + 1)) + (-30));
trace(sumnum)
}
function compass(e:Event)
{
compass_mc.rotation +=.5;
}

TOPICS
ActionScript
943
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 30, 2009 Dec 30, 2009

try:


var sumnum:Number;
compass_mc.addEventListener(Event.ENTER_FRAME, compass);
randomizer()
function randomizer() {
    sumnum=(Math.floor(Math.random() * (30 - (-30) + 1)) + (-30));
    trace(sumnum);
}
function compass(e:Event) {
    compass_mc.rotation = .5*(compass_mc.rotation+sumnum);
    if(Math.abs(compass_mc.rotation-sumnum)<1){
        randomizer();
    }
}

Translate
Community Expert ,
Dec 30, 2009 Dec 30, 2009

try:


var sumnum:Number;
compass_mc.addEventListener(Event.ENTER_FRAME, compass);
randomizer()
function randomizer() {
    sumnum=(Math.floor(Math.random() * (30 - (-30) + 1)) + (-30));
    trace(sumnum);
}
function compass(e:Event) {
    compass_mc.rotation = .5*(compass_mc.rotation+sumnum);
    if(Math.abs(compass_mc.rotation-sumnum)<1){
        randomizer();
    }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 30, 2009 Dec 30, 2009

That did it! Thanks a alot!

I had tried some code myself and got it really close to what I wanted it to do

but your code if far more elegant.

var sumnum:Number= 1
var myrotation:Number=0
var myTimer:Timer = new Timer(3500, 0); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, randomizer);
myTimer.start();
function randomizer (e:Event)
{
if (!((myrotation > 45.0) && (myrotation < 315)))
{
sumnum=(Math.floor(Math.random() * (30 - (-30) + 1)) + (-30));
}
}
//compass_mc.rotation=30
compass_mc.addEventListener(Event.ENTER_FRAME, compass);
function compass(e:Event)
{
if ((e.target.rotation > 30.0) && (e.target.rotation < 180))
{
sumnum = -1;
}
if ((e.target.rotation > 180) && (e.target.rotation < 330))
{
sumnum = 1;
}
if (sumnum>0)
{
e.target.rotation +=.45;
//trace("positive "+e.target.rotation)
}
if (sumnum<0)
{
e.target.rotation -=.45;
//trace("negative "+ e.target.rotation)
}
myrotation = e.target.rotation;
}

I'm sadly not a good mathamatichain. so I will have to take a closer look at your code

to really see what its doing (so it makes sence to me),  learn from it, and maybe adapt

it (slower rotation)

Thank you again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 30, 2009 Dec 30, 2009

you're welcome.  if you want to adjust the easing use:

var sumnum:Number;
var easeSpeed:Number = .2;  // use a number between 0 and 1

compass_mc.addEventListener(Event.ENTER_FRAME, compass);
randomizer()
function randomizer() {
    sumnum=(Math.floor(Math.random() * (30 - (-30) + 1)) + (-30));
    trace(sumnum);
}
function compass(e:Event) {
    compass_mc.rotation = easeSpeed*compass_mc.rotation+(1-easeSpeed)*sumnum;
    if(Math.abs(compass_mc.rotation-sumnum)<1){
        randomizer();
    }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 31, 2009 Dec 31, 2009

I've given the equation a look and I'm a bit confused. The first equation in compass is somewhat straight forward. The end number gives it the rotation

between -30 and 30 (the sumnum will give its direction)(?). The two things I'm unclear on is how the easing works (how dose the math slow it down or speed it up)

and why do you use the absolute vale as a test?

Thanks for time...and have a Happy New Year

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 31, 2009 Dec 31, 2009
LATEST

the key line is:

compass_mc.rotation = easeSpeed*compass_mc.rotation+(1-easeSpeed)*sumnum;

which defines a sequence of compass_mc.rotation numbers, r1,r2,r3,... .  this sequence converges to sumnum.  (eg, take the limit of both sides so the compass_mc.rotation on the left and the right are the same number, do a little algebra and you'll find compass_mc.rotation = sumnum.)

the rate of convergence is determine by easeSpeed.  if easeSpeed=0, then convergence is immediate.  ie, r1=r2=...=sumnum.  as easeSpeed approaches 1 (from the left), the convergence slows.  ie, r2 is just slightly different from r1 and r3 is just slightly different than r2 etc.

i use Math.abs() to ensure compass_mc.rotation-sumnum are close to each other without having to worry about which is greater.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines