Sou a: Inici / Usuaris / Victor Carceler / pro / ejemplos / j2me / gravity / particle.java

particle.java

package vch.gravity; public class Particle { private static int count = 0; private int id; public int r,g,b; public int x,y; // Posición X,Y public double m; // Masa public double q; // Carga public double speed_x=0.0d, speed_y=0.0d; // Velocidad actual public double force_x=0.0d, force_y=0.0d; // Fuerza sobre la partícula public Particle() { id = count++; } public String toString() { return "P" + id + " speed: " + speed_x + "," + speed_y + " force: " + force_x + "," + force_y + " pos: " + x + "," + y; } public void resetForce() { force_x = 0.0d; force_y = 0.0d; } public void addForce(Particle p) { double dist, dist_c; int sign; dist = p.x - x; if(Math.abs(dist) > 50) { force_x += m*p.m/dist; } else if(dist != 0.0d){ force_x -= m*p.m/dist; } dist = p.y - y; if(Math.abs(dist) > 50) { force_y += m*p.m/dist; } else if(dist != 0.0d) { force_y -= m*p.m/dist; } /* // Cálculo con gravedad y campo eléctrico dist = p.x - x; dist_c = dist * dist; if(dist < 0) {sign = -1;} else {sign = 1;} if(dist_c!=0.0d) { force_x += 9.8*(m*p.m)/dist_c * sign; force_x -= (q*p.q)/(dist_c*1.1) * sign; } dist = p.y - y; dist_c = dist * dist; if(dist < 0) {sign = -1;} else {sign = 1;} if(dist_c!=0.0d) { force_y += 9.8*(m*p.m)/dist_c * sign; force_y -= (q*p.q)/(dist_c*1.1) * sign; } */ } public void updateSpeed(double t) { speed_x += t*force_x/m; speed_y += t*force_y/m; } public void updatePosition(double t) { x += speed_x * t; y += speed_y * t; if(x<0) {x=0; speed_x = 0;} if(x>230) {x=230; speed_x = 0;} if(y<0) {y=0; speed_y = 0;} if(y>278) {y=278; speed_y = 0;} System.out.println(this); } }