5. Oktober 2012
Sixth Day: This exercise was created to get a feel for the upcoming mini project we were about to have for the next two days. What we were given were two basketballs which we could interact with by clicking once, dragging the mouse and releasing it (like how a slingshot works), making the balls fly around and rebound at the edges of the window. The balls would just pass through each other while flying around. What Kevin and I tried to do was when the balls collided, they would bounce off each other. It sounds pretty easy but there were bigger obstacles than we expected: Letting them even collide was difficult enough. Afterwards, it depended with what speed the balls collided with each other. If the speed wasn't high, they would actually bounce off each other. However, if the speed was high, the borders of the balls would overlap each other, creating a continuous trembling effect between the two balls. The theory to get rid of that effect (for advanced processing users unlike me): When the radii of the balls are overlapping each other, the overlapped distance would be possible to calculate due to the fact that we know how long the radii of each balls are. If we divide that distance into half, we'll have the length needed for each ball to be pull away from each other. Now "all we have to do" is to create a norm (Vector = 1) and multiply till we get the length of the distance/2 and set it to the other direction (-1) so the balls would be pushed/pulled off each other. Programming it to call up this function when the radii overlaps is also important. But... for now, it's just a theory for me. This is just the result of how far we came during the day (the bouncing ball class stayed the same):BouncingBall ball1; BouncingBall ball2; PVector p1 = new PVector(); PVector p2 = new PVector(); boolean drag = false; void setup() { size(800,600); ball1 = new BouncingBall(100,100); ball2 = new BouncingBall(50,50); // svg bild soll zentriert sein shapeMode(CENTER); smooth(); } void draw() { // hintergrund loeschen // background(255); // ghosting fill(255,255,255,60); rect(0,0,width,height); recalc_collision(); // else if (ball1._pos.y - ball2._pos.y <= ball1._w / 2 + ball2._w / 2 ) // { // _dir.x *=-1; // } if(drag) { // zeichne die abschussrichtung p1.set(ball1._pos.x,ball1._pos.y,0); line(p1.x,p1.y,p2.x,p2.y); } // zeichne den ball ball1.draw(); ball2.draw(); } void mousePressed() { drag = true; p1.set(ball1._pos.x,ball1._pos.y,0); p2.set(mouseX,mouseY,0); } void mouseDragged() { p2.set(mouseX,mouseY,0); } void mouseReleased() { drag = false; // abschuss staerke berechnen PVector dir = PVector.sub(p2,p1); // laenge verkuerzen dir.mult(.09); // der ball wird neu ausgerichtet ball1.set(p1,dir,.993); // ball2.set(p1,dir,.95); } void recalc_collision() { PVector dir = PVector.sub(ball1._pos,ball2._pos); float m = dir.mag(); if(m <= ball1._w/2 + ball2._w/2) { ball1.x *= -1; ball2.x *=-1; println("crash"); } }Unfortunately there is still some cleaning up to do with the syntax and although it is quite easy understanding the theory, it's very problematic for a beginner in processing to convert theory into practice.