import test; import java.awt.*; /* * * ball * */ class ball{ public final int MAX_VEL = 14; public Graphics g; public point size = new point(0,0); public point acc = new point(0,0); public point vel = new point(0,0); public point pos = new point(50,50); public point p = new point(50,50); public wall t_wall = new wall(0,0,1,1); public point t_p = new point(0,0); ball(Graphics g_in, double posx, double posy, double sizex, double sizey ){ pos.init(posx,posy); size.init(sizex,sizey); p.assign(pos); g = g_in; } public void move(){ g.setColor(Color.white); display(p); g.setColor(Color.black); display(pos); } public void accel(point mouse){ p.assign(pos); acc.sub(pos,mouse); // finne distanse til mus acc.mult(Math.pow(500/ (pos.square(mouse) + 10),2),acc); acc.y += 0.5; // addere gravitasjon vel.add(vel,acc); // øke hastighet if (vel.length() > MAX_VEL) vel.set_length(MAX_VEL); // sette maks fart pos.add(pos,vel); // Ny posisjon } public void display(point A){ g.drawOval((int) (A.x - size.x / 2 + 2) ,(int) (A.y - size.y / 2 + 2) ,(int) size.x -3 ,(int) size.y -3); } public void bounce(point edge, double radius){ t_wall.A.assign(p); t_wall.B.assign(pos); t_wall.init(); t_wall.dist(edge); if (t_wall.length < radius){ double t = Math.sqrt(radius*radius - t_wall.D.y*t_wall.D.y); t = t_wall.D.x - (t / t_wall.length); t_p.mult(t,t_wall.E); t_p.sub(t_wall.A,t_p); // Punktet hvor radius treffer t_p.sub(t_p,edge); t_p.init(t_p.y, - t_p.x); // Parallell med treff-flate t_wall.A.add(edge,t_p); t_wall.B.sub(edge,t_p); t_wall.init(); t_wall.bounce(this, radius); // Sprett på denne! } } }