Particles in XST


winscreen(640, 480, 0);

var _line = {
x: 300,//x position
y: 200,//y position
len: 50,
angle: 0
}

function Ball(x, y, r) {
this.x = x;
this.y = y;
this.r = r;
this.vx = 0;
this.vy = 0;
this.color = getrandomcolor();
Ball.all.push(this);
}

Ball.all = [];

Ball.draw_all = function () {
var i = Ball.all.length;
while (i--) {
Ball.all[i].draw();
}
};

Ball.prototype = {
draw: function () {
fillcircle(this.x, this.y, this.r, this.color);
},
remove: function () {
Ball.all.splice(Ball.all.indexOf(this), 1);
}
}

function main() {
cls(rgba(0, 0, 0, .2));
_line.angle += (PI * 2) / 300;
var x = _line.x + _line.len * cos(_line.angle);
var y = _line.y + _line.len * sin(_line.angle);

if (Ball.all.length < 200) {
for (var i = 0; i < 5; i++) {
var ball = new Ball(x, y, 2);
var random_offset = rnd(1) - .5;
var speed = rnd(15) + 3;
ball.vx = speed * cos(_line.angle + random_offset);
ball.vy = speed * sin(_line.angle + random_offset);
}
}

var i = Ball.all.length;
while (i--) {
var ball = Ball.all[i];
ball.x += ball.vx;
ball.y += ball.vy;
ball.vy += .1;
ball.vx *= .999;
ball.vy *= .99;
if (ball.x % getscreenwidth() !== ball.x) {
ball.remove();
}
else if (ball.y >= getscreenheight()) {
ball.vy = -abs(ball.vy);
ball.vy *= .7;
if (abs(ball.vy) < 1 && abs(ball.vx) < 1) {
ball.remove();
}
}
}
line(_line.x, _line.y, x, y, 1, rgb(255, 255, 255));
Ball.draw_all();
}

 

Test it here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s