[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sc-users] collisions



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 = "">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