public void draw(Graphics g) { animate(); g.setColor(Color.black); g.fillRect(0, 0, dim.width, dim.height); g.setColor(Color.white); numpaint++; DebugPrinter dbg = new DebugPrinter(g, 50, 60); dbg.print( "Spring-mass demo by yigal irani, drag balls or create new ones by clicking inside box"); dbg.print("frame", numpaint); dbg.print("fps", 1 / timer.time_diff); Point top_left = point_by_vec(new Vec(-1, 1)); g.draw3DRect(top_left.x, top_left.y, screen_dist(2), screen_dist(2), true); for (int i = 0; i < springs.size(); i++) { Spring spring = springs.get2(i); Point p1 = point_by_vec(balls.get2(spring.start).pos); Point p2 = point_by_vec(balls.get2(spring.end).pos); g.drawLine(p1.x, p1.y, p2.x, p2.y); } for (int i = 0; i < balls.size(); i++) { Ball ball = balls.get2(i); Point p = point_by_vec(ball.pos); int screen_radius = screen_dist(RADIUS); g.setColor(Color.blue); g.fillOval(p.x - screen_radius, p.y - screen_radius, screen_radius * 2, screen_radius * 2); g.setColor(Color.white); g.drawOval(p.x - screen_radius, p.y - screen_radius, screen_radius * 2, screen_radius * 2); g.drawString("" + i, p.x, p.y); } }
public BallVector calc_new_frame( BallVector _balls, SpringVector _springs, double _radius, Timer timer) { // return the the balls of the next frame balls = _balls; springs = _springs; radius = _radius; num_balls = balls.size(); int i; for (i = 0; i < NUM_STEPS; i++) call_rk4(timer.cur_time, timer.time_diff / NUM_STEPS); // too: acum the time? return balls; }
int find_ball(Vec v) { int num_balls = balls.size(); for (int i = 0; i < num_balls; i++) { Ball p = balls.get2(i); double dist = v.calc_dist(p.pos); if (dist < RADIUS) { // printf("found ball %d\n",i); find_offset = p.pos.sub(dragged_vec); // last_dragged_vec=v; return i; } } return -1; }
void call_rk4(double cur_time, double time_diff) { num_balls = balls.size(); double[] y = new_vector(num_balls * 4); // double[num_balls*4]; double[] dy = new_vector(num_balls * 4); encode_balls(balls, y); the_derive( cur_time, y, dy); // the current implementation of derive does not uses the time, but can envision an // implementation that might (gravity is off every second, perhaps?) rk4(y, dy, num_balls * 4, cur_time, time_diff, y); // balls=new BallVector(); balls = decode_balls(y); // free_vector(y,1,num_balls*4); // free_vector(dy,1,num_balls*4); }