public boolean isHit(Particle p) { /*Another try double angle = Math.atan2(Math.abs(centerY-p.getCenterY()) ,Math.abs(centerX-p.getCenterX())); double distance1 = (Math.abs(centerX-p.getCenterX())- (width/2))*Math.cos(angle); double distance2 = (width/2)*Math.cos(angle); double distance = ((centerX-p.getCenterX())*(centerX-p.getCenterX())) + ((centerY-p.getCenterY()) * (centerY-p.getCenterY())); distance = Math.sqrt(distance); if(distance1+distance2>= distance) { return true; } else { return false; }*/ // --------------------------------------------------- // Checking distance between centers and compare it with radius,width,height // -------------------------------------------------- double distance = ((centerX - p.getCenterX()) * (centerX - p.getCenterX())) + ((centerY - p.getCenterY()) * (centerY - p.getCenterY())); distance = Math.sqrt(distance); double dist = ((width / 2) * (width / 2)) + ((height / 2) * (height / 2)); dist = Math.sqrt(dist); double distance2 = (p.getRadius() + dist); if (distance <= distance2) { hit(p); return true; } return false; }
public void hit(Particle p) { // --------------------------------------------------- // Do the hit action for particle and obstacle // -------------------------------------------------- if (Math.abs(centerY - p.getCenterY()) <= (height / 2) + p.getRadius()) { double a = p.getYVel(); p.setYVel(-a); } if (Math.abs(centerX - p.getCenterX()) <= (width / 2) + p.getRadius()) { double b = p.getXVel(); p.setXVel(-b); } else { p.setXVel(p.getXVel()); p.setYVel(p.getYVel()); } }