PDA

View Full Version : magnetic fields


CnD
03-30-2004, 11:52 AM
I made a very simple code. One ball that runs away from mouse pointer when it comes near the ball.

[as]
var dx:Number=random(9)-5;
var dy:Number=random(9)-5;
var positX:Number;
var positY:Number;
_root.createEmptyMovieClip("circle_mc", 1);
circle_mc._x = 200;
circle_mc._y = 200;
circle_mc.lineStyle(100, 0x125543, 100);
circle_mc.lineTo(1, 0);
positX=this.circle_mc._x;
positY=this.circle_mc._y;
var distan:Number;

onEnterFrame = function()
{
positX+=dx;
positY+=dy;
if (positX<48 || positX>500) {dx=-dx; positX+=dx};
if (positY<50 || positY>350) {dy=-dy; positY+=dy};
distan =Math.sqrt(Math.pow(_xmouse-positX,2) + Math.pow(_ymouse-positY,2));
if (distan<150)
{
if (positX < _xmouse) { dx-=20/Math.exp(distan/30)};
if (positX > _xmouse) { dx+=20/Math.exp(distan/30)};
if (positY < _ymouse) { dy-=20/Math.exp(distan/30)};
if (positY > _ymouse) { dy+=20/Math.exp(distan/30)};
if (dx>10) { dx=10};
if (dx<(-10)) { dx=(-10)};
if (dy>10) { dy=10};
if (dy<(-10)) { dy=(-10)};
};
dx*=.99;
dy*=.99;
this.circle_mc._x=positX;
this.circle_mc._y=positY;
}
[as]


Lets spice it a bit ;)


Cole

liquidj
03-31-2004, 01:41 AM
How about this


var iter = 30;
for (var i = 0; i<iter; i++) {
this.createEmptyMovieClip("circle"+i, i);
var clip = this["circle"+i];
var dx:Number = random(9)-5;
clip.dx = dx;
var dy:Number = random(9)-5;
clip.dy = dy;
var positX:Number;
clip.positX = positX;
var positY:Number;
clip.positY = positY;
clip._x = 200;
clip._y = 200;
clip.lineStyle(Math.random()*15+10, 0xFFFFCC, 100);
clip.lineTo(1, 0);
clip.positX = clip._x;
clip.positY = clip._y;
var distan:Number;
clip.distan = distan;
clip.onEnterFrame = function() {
this.positX += this.dx;
this.positY += this.dy;
if (this.positX<48 || this.positX>500) {
this.dx = -this.dx;
this.positX += this.dx;
}
if (this.positY<50 || this.positY>350) {
this.dy = -this.dy;
this.positY += this.dy;
}
this.distan = Math.sqrt(Math.pow(_xmouse-this.positX, 2)+Math.pow(_ymouse-this.positY, 2));
if (this.distan<150) {
if (this.positX<_xmouse) {
this.dx -= 20/Math.exp(this.distan/30);
}
if (this.positX>_xmouse) {
this.dx += 20/Math.exp(this.distan/30);
}
if (this.positY<_ymouse) {
this.dy -= 20/Math.exp(this.distan/30);
}
if (this.positY>_ymouse) {
this.dy += 20/Math.exp(this.distan/30);
}
if (this.dx>10) {
this.dx = 10;
}
if (this.dx<(-10)) {
this.dx = (-10);
}
if (this.dy>10) {
this.dy = 10;
}
if (this.dy<(-10)) {
this.dy = (-10);
}
}
this.dx *= .99;
this.dy *= .99;
this._x = this.positX;
this._y = this.positY;
};
}



My answer to everything... If one is good, lots are better.
;)

CnD
03-31-2004, 08:14 AM
Nice one...respect...

I am at work right now, so I found a little time to make something different.


[as]
iter=50;
for (var i = 0; i<iter; i++)
{
this.createEmptyMovieClip ("circle"+i,i);
var clip = this ["circle"+i];
var color=random(0xFFFFFF);
clip.lineStyle(Math.random()*30+10, color , 100);
clip.lineTo(1, 0);
clip._x=Math.random()*550;
clip._y=Math.random()*400;
clip.ClipScale=clip._xscale;
clip.opa=clip._alpha;
clip.dScale=Math.random()*3;
clip.dx=Math.random(10)*-5;
clip.dy=Math.random(10)*-5;

clip.onEnterFrame =function()
{
if (this.ClipScale <= 30) {this.ClipScale=30};
this._xscale = this.ClipScale;
if (this.ClipScale==30)
{
this._x=Math.random()*550;
this._y=Math.random()*400;
this.ClipScale=100;
this._alpha=100;
};
this.ClipScale-=this.dScale;
this._alpha=(this._xscale-30)*10/7;
this._x-=this.dx;
this._y-=this.dy;
var dist:Number;
dist=Math.sqrt(Math.pow(_xmouse-this._x, 2)+Math.pow(_ymouse-this._y, 2));
if (dist<150)
{
if (this._x<_xmouse) {this.dx += 20/Math.exp(this.dist/30);}
if (this._x>_xmouse) {this.dx -= 20/Math.exp(this.dist/30);}
if (this._y<_ymouse) {this.dy += 20/Math.exp(this.dist/30);}
if (this._y>_ymouse) {this.dy -= 20/Math.exp(this.dist/30);}
if (this.dx>10) {this.dx = 10;}
if (this.dx<(-10)) {this.dx = (-10);}
if (this.dy>10) {this.dy = 10;}
if (this.dy<(-10)) {this.dy = (-10);}
};
};
}
[as]


Cole

pom
04-14-2004, 08:09 PM
Since large amounts of code seem to be allowed, here you go:_global.root = this ;

this.createEmptyMovieClip("dot", -10) ;
this.dot.lineStyle(5, 0xffffff, 100) ;
this.dot.lineTo(.45, .15) ;
this.dot._visible = 0 ;

min_dist = 100 ;
max_clip = 8 ;
damp = 1 ;
k = .01 ;
left = 0 ;
right = 300 ;
up = 0 ;
down = 300 ;
n_arcs = 2 ;

MovieClip.prototype.electricArc = function (mc1, mc2, alpha, steps, off) {
for (var n=0; n < n_arcs; n++) {
this.lineStyle (random(3), 0xffffff, alpha) ;
this.moveTo (mc1._x, mc1._y) ;
var dx = (mc2._x - mc1._x) / steps ;
var dy = (mc2._y - mc1._y) / steps ;
for (var s=1; s < steps; s++) {
var x = mc1._x + s*dx + getRandom(off) ;
var y = mc1._y + s*dy + getRandom(off) ;
this.lineTo(x, y) ;
}
this.lineTo(mc2._x, mc2._y) ;
}
}
MovieClip.prototype.checkBoundaries = function () {
if (this._x < left) {
this._x = left ;
this.vx *= -1 ;
}
if (this._x > right) {
this._x = right ;
this.vx *= -1 ;
}
if (this._y < up) {
this._y = up ;
this.vy *= -1 ;
}
if (this._y > down) {
this._y = down ;
this.vy *= -1 ;
}
}
function getDistance ( mc1, mc2 ) {
var dx = mc2._x - mc1._x ;
var dy = mc2._y - mc1._y ;
return Math.sqrt ( dx*dx + dy*dy ) ;
}
function getRandom (inter) {
return random(inter) - inter / 2 ;
}
function move () {
this._x += this.vx ;
this._y += this.vy ;
this.checkBoundaries () ;
if (this.num == max_clip-1) root.clear() ;
for (var j=this.num+1; j < max_clip; j++) {
var cl = mcs[j] ;
var dx = cl._x - this._x ;
var dy = cl._y - this._y ;
var dist = Math.sqrt(dx*dx + dy*dy) ;
if (dist < min_dist) {
var angle = Math.atan2(dy, dx) ;
var tx = this._x + min_dist * Math.cos(angle) ;
var ty = this._y + min_dist * Math.sin(angle) ;
this.vx -= (tx - this._x) * k;
this.vy -= (ty - this._y) * k;
cl.vx += (tx - this._x) * k;
cl.vy += (ty - this._y) * k;
}
if ( dist <= min_dist*2 ) {
root.electricArc(this, cl, 100-dist/2, 5, 20) ;
}
}
this.vx *= damp;
this.vy *= damp;
}

mcs = [] ;

for (var i=0; i < max_clip; i++) {
var _mc = this.dot.duplicateMovieClip("d"+i, i) ;
mcs.push(_mc) ;
_mc.num = i ;
_mc._x = random(right) ;
_mc._y = random(down) ;
_mc.vx = getRandom(10) ;
_mc.vy = getRandom(10) ;
_mc.k = k * ( random(2) ? -1 : 1 ) ;
_mc.onEnterFrame = move ;
}Mainly inspired by Bit-101's amazing node garden.

pom
04-22-2004, 04:37 PM
Just a simple one, in 19 lines :)//pom
this.createEmptyMovieClip ("_mc", -5) ;
_mc.lineStyle (0, 0, 100) ;
_mc.moveTo (20, -5) ;
_mc.lineTo (20, 5) ;

_mc.onEnterFrame = rotate ;
for (var i = 0 ; i < 70 ; i++) {
var mc = _mc.duplicateMovieClip ("d" + i, i) ;
mc._x = random (200) ;
mc._y = random (100) ;
mc.onEnterFrame = rotate ;
}

function rotate () {
var dx = _xmouse - this._x ;
var dy = _ymouse - this._y ;
var an = Math.atan2 (dy, dx) ;
this._rotation = an * 180 / Math.PI + 45 ;
}

liquidj
04-23-2004, 03:06 AM
I really just modified the last line of the rotate function.
Any random number changes were just to spread the objects out a bit on a larger field.
That last bit of [pom] code is sweet.


this.createEmptyMovieClip ("_mc", -5) ;
_mc.lineStyle (0, 0, 100) ;
_mc.moveTo (20, -5) ;
_mc.lineTo (20, 5) ;
_mc.onEnterFrame = rotate ;
//
for (var i = 0 ; i < 270 ; i++) {
var mc = _mc.duplicateMovieClip ("d" + i, i) ;
mc._x = random (500) ;
mc._y = random (300) ;
mc.onEnterFrame = rotate ;
}
//
function rotate () {
var dx = _xmouse - this._x ;
var dy = _ymouse - this._y ;
var an = Math.atan2 (dy, dx) ;
this._rotation += ((an * 180 / Math.PI + 45)- this._rotation)/7 ;
}

pom
04-23-2004, 03:26 AM
Nice :)

vulcanpimp
04-26-2004, 08:34 AM
Hey POM

thats actually pretty good...tell me have you used flash before?

CyanBlue
04-26-2004, 08:40 AM
Very nice... Love it... That reminds me of the play that I used to do with the magnet when I was young... :)

pom
04-26-2004, 12:32 PM
Hey POM

thats actually pretty good...tell me have you used flash before?Before what? :rolleyes:

vulcanpimp
04-29-2004, 12:53 PM
don't mind me POM

its just my little attempt at humour...was that script the first script you ever wrote - if so its quite good

guess it didn't quite translate oh well

vous ne plurier pas

pom
04-30-2004, 04:55 AM
Oups, sorry :D

And not at all, I wrote it just before posting it :) It's actually inspired from an effect I found in Flash Math Creativity (a lovely book, by the way).

pom
05-02-2004, 09:15 AM
OK, not at all what I was trying to do but... :D //pom
this.createEmptyMovieClip ("_mc", -5) ;
_mc.lineStyle (2, 0, 100) ;
_mc.lineTo (.45, .15) ;
_mc._visible = false ;

for (var i = 0 ; i < 70 ; i++) {
var mc = _mc.duplicateMovieClip ("d" + i, i) ;
mc._x = mc.x = random (300) ;
mc._y = mc.y = random (200) ;
mc.onEnterFrame = rotate ;
}

function rotate () {
var dx = _xmouse - this._x ;
var dy = _ymouse - this._y ;
if (dx * dx + dy * dy < 2500) {
var an = Math.atan2 (dy, dx) ;
var ax = (_xmouse - this._x) * .2 ;//+ 50 * Math.cos (an) ;
var ay = (_ymouse - this._y) * .2 ; //+ 50 * Math.sin (an) ;
this.vx += ax ;
this.vy += ay ;
}
this._x += (this.vx*=.95) ;
this._y += (this.vy*=.95) ;
if (this._x < 0 || this._x > 300) this.vx *= -.95 ;
if (this._y < 0 || this._y > 200) this.vy *= -.95 ;
}