-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.pde
65 lines (59 loc) · 1.64 KB
/
Ball.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Ball{
PVector pos;
PVector vel;
float radius;
color col;
float gravity=0.15;
int trailLength=200;
ArrayList<PVector> trail;
public Ball(float x,float y, int radius,color c){
this.pos=new PVector(x,y);
this.vel=new PVector(0,0);
this.radius=radius;
this.col=c;
trail=new ArrayList();
}
void show(){
fill(col);
noStroke();
circle(pos.x,pos.y,2*radius);
//Show Trail
fill(col,80);
for(PVector point:trail){
ellipse(point.x,point.y,4,4);
}
}
void collision(){
//calculate distace between centres of ball and bigger circle
PVector dis=PVector.sub(this.pos,new PVector(width/2,height/2));
if(dis.mag()>=container_radius-radius){
//Collisons
//Finding the normal vector
PVector disNorm=dis.normalize();
//Fixing the position first
pos.add(disNorm.mult(-1));
//Finding the projection vector of velocity vector on disNorm vector
float mag= vel.dot(disNorm);
PVector projVec=disNorm.mult(mag);
//New Velocity = OldVelocity-2*projVec;
PVector newVel=vel.add(projVec.mult(-2));
this.vel=newVel;
wave.add(new Circle(container_radius,col));
file.play();
return;
}
PVector force=new PVector(0,gravity);
vel.add(force);
}
void update(){
collision();
pos.add(vel);
if (trail.size()<trailLength){
trail.add(new PVector(pos.x,pos.y));
}
else{
trail.remove(0);
trail.add(new PVector(pos.x,pos.y));
}
}
}