[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [sc-users] collisions
Hi Adam,
after changing 2 things it seems to work:
- set the color to white when entering the outer loop
- call Pen.stroke directly after Pen.addArc
cheers,
Daniel
(
~dist = 10;
w = Window("collisions", Rect(0,0,500, 500)).front;
w.view.background = Color.grey(0.6);
u = UserView(w, Rect(0,0,500,500));
m = 25; //number of spheres
d= {500.rand@xxxxxxxx}.dup(m); // initial positions
u.animate= true;
u.drawFunc = {
Pen.width = 2;
Pen.capStyle = 1;
Pen.strokeColor= Color.white;
d= d.collect{|point|
point+( (5.rand - 2) @(5.rand - 2)) %(600@600);
};
d.do{|point|
Pen.strokeColor= Color.white;
d.do{|point2|
var dx, dy ;
dx = (point2.x - point.x);
dy = (point2.y - point.y);
if (point2 != point) {
if ( ((dx * dx) + (dy * dy)).sqrt < ~dist) {
Pen.strokeColor= Color.red;
}
}
};
Pen.addArc(point, 2, 0, 2pi);
Pen.stroke;
};
};
u.refresh;
)
adam sanches schrieb:
Hello , i been trying to make collisions between moving points
calculating the distance between them using this formula:
d = sqrt((x-x2)*(x-x2) +(y-y2) * (y-y2))
I made this code for visualize the collisions using pen, its suppose
that each spheres change its color when it collides with another, but
im getting unexpected behaivors, all spheres are changing at the same
time, Do anybody have an idea of what is happening?
(
~dist = 10;
w = Window("collisions", Rect(0,0,500, 500)).front;
w.view.background = Color.grey(0.6);
u = UserView(w, Rect(0,0,500,500));
m = 25; //number of spheres
d= {500.rand@xxxxxxxx}.dup(m); // initial positions
u.animate= true;
u.drawFunc = {
Pen.width = 2;
Pen.capStyle = 1;
Pen.strokeColor= Color.white;
d= d.collect{|point|
point+( (5.rand - 2) @(5.rand - 2)) %(600@600);
};
d.do{|point|
d.do{|point2|
var dx, dy ;
dx = (point2.x - point.x);
dy = (point2.y - point.y);
if (point2 != point) {
if ( ((dx * dx) + (dy * dy)).sqrt < ~dist) {
Pen.strokeColor= Color.red;
}
}
};
Pen.addArc(point, 2, 0, 2pi);
};
Pen.stroke;
};
u.refresh;
)
thanks in advance
Adam
_______________________________________________
sc-users mailing list
info (subscription, etc.): http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtml
archive: https://listarc.bham.ac.uk/marchives/sc-users/
search: https://listarc.bham.ac.uk/lists/sc-users/search/