PDA

View Full Version : Bouncy Balls



freeskier89
January 2nd, 2009, 03:12 AM
Hey! I thought I would start things out with something simple.Its got a few glitchy responses and the code is messy as usual :). Thanks kirupa for holding another AS contest :)! Its been too long since I've been here.



_root.createEmptyMovieClip("container",1);
var d = 1;
function addCircle(x, y, scale) {
var mc = container.attachMovie("Circle", "circle"+d, d++);
mc._x = x;
mc._y = y;
mc._xscale = mc._yscale=scale;
mc.colorobj = new Color(mc.coloroverlay);
return mc;
}
MovieClip.prototype.setHue = function(x, mc) {
//x = 0 - 1
x = Math.max(x*2*Math.PI, 0.01);
r = 128+128*Math.cos(x);
g = 128+128*Math.cos(x+Math.PI*2/3);
b = 128+128*Math.cos(x+Math.PI*4/3);
var hex = r << 16 | g << 8 | b;
this.colorobj.setRGB(hex);
};
var friction = .92;
for (i=0; i<=10; i++) {
var m = random(70)+30;
mc = addCircle(random(500), 160, m);
mc.setHue(0);
mc.vx = Math.random()*3-1.5;
mc.vy = Math.random()*3-1.5;
mc.m = m;
mc.dragging=false;
mc.onPress=function(){
this.dragging=true;
}
mc.onRelease=mc.onReleaseOutside=function(){
this.dragging=false;
}
mc.onEnterFrame = function() {
if (this._x>500-this._width/2 || this._x<this._width/2) {
this.vx = -this.vx*friction;
}
if (this._y>300-this._width/2 || this._y<this._width/2) {
this.vy = -this.vy*friction;
}
this._y = Math.max(this._width/2, Math.min(300-this._width/2, this._y));
this._x = Math.max(this._width/2, Math.min(500-this._width/2, this._x));
collision_mc = null;
var thisv = Math.sqrt(this.vx*this.vx+this.vy*this.vy);
this.setHue(Math.min(.3,thisv/30));
for (i in container) {
var mc = container[i];
if (typeof (mc) == "movieclip" && this != mc) {
var dx = mc._x-this._x;
var dy = mc._y-this._y;
var d = Math.sqrt(dx*dx+dy*dy);
var thisv = (this.vx*this.vx+this.vy*this.vy);
var mcv =(mc.vx*mc.vx+mc.vy*mc.vy);
if (d<this._width/2+mc._width/2 && mcv<thisv) {
var overlap = mc._width/2+this._width/2-d;

var momentumx2 = mc.m*mc.vx;
var momentumx1 = this.m*this.vx;
var momentumy2 = mc.m*mc.vy;
var momentumy1 = this.m*this.vy;
dvx = dx/d;
dvy = dy/d;
this._x-=dvx*overlap/2;
this._y-=dvy*overlap/2;
mc._x+=dvx*overlap/2;
mc._y+=dvy*overlap/2;

//dot the momentum with the action line
E1 = this.m*this.vx*dvx+this.m*this.vy*dvy;
E2 = mc.m*mc.vx*dvx+mc.m*mc.vy*dvy;

momentumx1 += -(E1-E2)*dvx;
momentumy1 += -(E1-E2)*dvy;
momentumx2 += (E1-E2)*dvx;
momentumy2 += (E1-E2)*dvy;
this.vx = momentumx1/this.m;
this.vy = momentumy1/this.m;
mc.vx = momentumx2/mc.m;
mc.vy = momentumy2/mc.m;
mc._x+=mc.vx;
mc._y+=mc.vy;
}
}
}
this._x += this.vx;
this._y += this.vy;
if(!this.dragging)
this.vy += .2;
else{
this.vx=Math.min(10,this._xmouse/5);
this.vy=Math.min(10,this._ymouse/5);
}
};
}
Preview SWF (http://www.megaswf.com/view/a31c07dd7f94cdd163119a370f0408a5.html)

TheCanadian
January 2nd, 2009, 03:15 AM
Wow neither you or your brother have been here in ages! That's a nice effect though, good work!

kirupa
January 2nd, 2009, 03:16 AM
Hey free!
Any chance you can post a link to a preview of it in your original post?

Thanks,
Kirupa :)

freeskier89
January 2nd, 2009, 03:34 AM
Alright done :).

@TC: I know :(... I have been away for flash and kirupa for way too long! I haved missed 'fun' coding like this!

creativeFuzion
January 2nd, 2009, 04:47 AM
very nice. i like it. the colors are mellow & warm, but i think the reactions between the balls are too quick and not really fitting along with the mood. overall, i can assume i'm out of the running for this competition now that you're a competitor. :P

freeskier89
January 2nd, 2009, 05:07 AM
I totally agree! I got fed up with the collision code and just posted it lol, it should be smoother. Don't let me discourage you from entering! Im definitely beatable, and besides I leave to go back to college on Sunday morning, so I doubt I will have a load of time to make stuff (i will make a couple more things though hopefully). I wish you the best for your submissions :)

Pasquale
January 2nd, 2009, 07:21 AM
Hey Sweet work Brandon! I remember the first time I asked if you could develop a bouncy ball app where I could drop balls onto a city hehe :)

Awesome that you still linger around