Vec vec_by_mouse(int x, int y) { Vec ans = new Vec(); ans.x = (x - dim.width / 2.) / size; ans.y = -(y - dim.height / 2.) / size; return ans; }
Vec wall_power(Ball p) { Vec ans = new Vec(); is_colide = false; ans.x = wall_power2(p.pos.x); ans.y = wall_power2(p.pos.y); if (is_colide) ans.sub_to(p.speed.mult(10)); return ans; }
Vec calc_collide_power(Ball p1, Ball p2, double dist) { if (dist > radius * 2) return new Vec(); // Friday, August 29, 2008 15:26:33: stickiness bug fix Vec speed_diff = p2.speed.sub(p1.speed); double force = 1000 * (dist - radius * 2); Vec npos1 = p1.pos.div(dist); // normalized Vec npos2 = p2.pos.div(dist); force += 10 * speed_diff.dot_mult(npos2.sub(npos1)); Vec ans = npos2.sub(npos1).mult(force); return ans; /* colide_power_x=force*(x2-x1)/dist; colide_power_y=force*(y2-y1)/dist;*/ }
void animate() { dim = getSize(); size = (int) (Math.min(dim.height, dim.width) / 2.2); timer.tell_time(); if (timer.time_diff == 0) return; // not enought time has passed, dont animate-crach fix dragged_speed = dragged_vec.sub(last_dragged_vec).div(timer.time_diff); last_dragged_vec = dragged_vec; if (dragged_ball != -1) { balls.get2(dragged_ball).pos = dragged_vec.add(find_offset).trim(-1, 1); balls.get2(dragged_ball).speed = dragged_speed; } balls = new WorldAnimate().calc_new_frame(balls, springs, RADIUS, timer); }
Vec calc_spring_power(Ball p1, Ball p2) { double dist = p1.pos.calc_dist(p2.pos); // if (abs(dist-STRING_LEN)<.1) // return; Vec speed_diff = p2.speed.sub(p1.speed); double force = 1000 * (dist - STRING_LEN); Vec npos1 = p1.pos.div(dist); // normalized Vec npos2 = p2.pos.div(dist); force += 100 * speed_diff.dot_mult(npos2.sub(npos1)); Vec ans = npos2.sub(npos1).mult(force); return ans; // force*=force; // colide_power_x=(x2-x1)/dist*force; // colide_power_y=(y2-y1)/dist*force; }
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; }
Vec mult(double scalar) { Vec ans = new Vec(); ans.x = x * scalar; ans.y = y * scalar; return ans; }
Vec sub(Vec right) { Vec ans = new Vec(); ans.x = x - right.x; ans.y = y - right.y; return ans; }
Vec add(Vec right) { Vec ans = new Vec(); ans.x = x + right.x; ans.y = y + right.y; return ans; }
Vec div(double a) { Vec ans = new Vec(); ans.x = x / a; ans.y = y / a; return ans; }