PDA

Click to See Complete Forum and Search --> : random motion

gerbick
03-12-2002, 08:36 PM
there HAS to be a quicker way to get random movement.

function getdistance (x, y, x1, y1) {
var run, rise;
run = x1-x;
rise = y1-y;
return (hyp(run, rise));
}
function hyp (a, b) {
return (Math.sqrt(a*a+b*b));
}
MovieClip.prototype.reset = function () {
var dist, norm, movie_height, movie_width;

movie_height = 12;
movie_width = 310;

speed = Math.random()*4+2;
targx = Math.random()*(movie_width-_width);
targy = Math.random()*(movie_height-_height);
dist = _root.getdistance(_x, _y, targx, targy);

norm = speed/dist;
diffx = (targx-_x)*norm;
diffy = (targy-_y)*norm;
};

MovieClip.prototype.move = function () {
var cycle;

cycle = 100;

if (_root.getdistance(_x, _y, targx, targy)>speed) {x += diffx;y += diffy;
} else {x = targx;
y = targy;
if (!this.t) {
t = getTimer();
}if (getTimer()-t>cycle) {reset();
t = 0;
}
}
_x = x;
_y = y;
}

then, for each symbol I use

onClipEvent (enterFrame) {
move();
}

there has GOTTA be a quicker method.

jdura99
03-13-2002, 10:06 AM
minX = 0;
maxX = 550;
minY = 0;
maxY = 400;
speed = 5;
xDest = random(maxX - minX) + minX;
yDest = random(maxY - minY) + minY;
}

onClipEvent (enterFrame) {
if (Math.round(_x) != xDest) {
_x += xDif;
xDif = (xDest - _x) / speed;
} else {
xDest = random(maxX - minX) + minX;
}
if (Math.round(_y) != yDest) {
_y += yDif;
yDif = (yDest - _y) / speed;
} else {
yDest = random(maxY - minY) + minY;
}
}

This might work?

Josh Dura

RazoRmedia
03-13-2002, 10:07 AM
gerbick, what are you trying to do?

Your code above just moves the mc left and right a bit. What is your objective, what do you want it to do?

gerbick
03-13-2002, 12:26 PM
yeah razor... that code is directly from my footer. I want to take a MC, or multiple versions thereof, and basically create TRUE random motion.

jdura... thanks for the code. but I gotta ask. how do I attach that to a MC?

yes, you can even treat me like an idiot on the answer ;)

RazoRmedia
03-13-2002, 12:38 PM
Originally posted by gerbick
jdura... thanks for the code. but I gotta ask. how do I attach that to a MC?

create a movieclip, right click on it and choose actions. then paste the text. Don't put it on a frame or it will generate errors.

RazoRmedia
03-13-2002, 12:47 PM
heres a simpler albeit 'jerkier' version. Speed the frame rate up to 40 and watch it go.

min = "0";
max = "200";
}
onClipEvent (enterFrame) {
if (this._x <= min) {
setProperty (this, _x, "100");
}
if (this._y <= min) {
setProperty (this, _y, "100");
}
if (this._x >= max) {
setProperty (this, _x, "100");
}
if (this._y >= min) {
setProperty (this, _y, "100");
}
newx = random(20)-10;
newy = random(20)-10;
setProperty (this, _x, this._x + newx);
setProperty (this, _y, this._y + newy);
}

Ed Mack
03-13-2002, 02:15 PM
Okay, heres a stripped down version of some code I used ages ago to make "intellegant" movment. The use of trig isn't neccessary.

Adjust the random to give you the amount of rotation.

function Move (deg, speed) {
_y += y;
_x += x;
}
speed = 2;
angle = Math.random()*360;

xb = 400;
yb = 400;
}
onClipEvent (enterFrame) {
angle +=( Math.random()*40)-20;
Move(angle, speed);

if(_x < 0 || _x > xb || _y < 0 || _y > yb){
angle += 30;
}
}

theMatrix
03-13-2002, 05:06 PM
Originally posted by gerbick
and basically create TRUE random motion.

Sounds like what you want is Brownian motion -- a bounded random walk over a 2D lattice. A web search should turn up dozens of pages that will help you out. I used this same technique to animate a flickering flame in MAX using script controllers not too long ago.

have fun :)

too bad I don't speak ActionScript :(

Ed Mack
03-13-2002, 05:27 PM
If you want brownian motion, change the random line in my script to this:

Change
angle +=( Math.random()*40)-20;

To
angle = Math.round(Math.random()*5)==5 ? Math.random()*360 : angle;

So it makes this:

function Move (deg, speed) {
_y += y;
_x += x;
}
speed = 10;
angle = Math.random()*360;

xb = 400;
yb = 400;
}
onClipEvent (enterFrame) {
angle = Math.round(Math.random()*2)==2 ? angle + Math.random()*160 + 100 : angle;
Move(angle, speed);

if(_x < 0 || _x > xb || _y < 0 || _y > yb){
angle += 30;
}
}