/*
 * DrawableClassicalParticle.java
 *
 * Created on November 7, 2004, 7:16 AM
 */

package JSciDraw.physics;

import JSci.maths.*;
import JSci.physics.*;
import java.awt.geom.*;
import java.awt.*;
import JSciDraw.interfaces.*;
import JSciDraw.physics.*;
import java.util.*;
/**
 *
 * @author  arturo
 */
public abstract class DrawableRigidBodyV2D extends RigidBodyV2D implements Drawable2D, Moveable2D{
    protected Color color = Color.blue;
    protected Shape area; 
    protected Shape areaOld;
    protected AffineTransform af = new AffineTransform();
    protected ArrayList forces = new ArrayList();
    //private AbstractDoubleVector totalForce = new Double2Vector();

    public DrawableRigidBodyV2D() { 
    }
    
    public void draw2D(java.awt.Graphics2D g2D) {
        g2D.setColor(color);
        g2D.fill(area);
    }
    
    public void erase2D(java.awt.Graphics2D g2D) {
        g2D.setColor(Color.white);
        g2D.fill(areaOld);
    }
    
    public void move2D(double dt) {
        double xx = x.getComponent(0);
        double xy = x.getComponent(1);
        double vx = v.getComponent(0);
        double vy = v.getComponent(1);
        af.setToIdentity();
        af.rotate(this.angVel*dt,xx,xy);
        af.translate(vx * dt, vy *dt);
        areaOld = area;
        area = af.createTransformedShape(areaOld);
        super.move(dt);

    }
    
    public void setPosition(AbstractDoubleVector p) {
        double x = p.getComponent(0);
        double y = p.getComponent(1);
        af.setToIdentity();
        af.translate(x, y);
        areaOld = area;
        area = af.createTransformedShape(areaOld);
        super.setPosition(p);
    }
    
    public void setAngle(double angle) {
        double xx = x.getComponent(0);
        double xy = x.getComponent(1);
        af.setToIdentity();
        af.rotate(angle,xx,xy);
        areaOld = area;
        area = af.createTransformedShape(areaOld);
        ang=angle;
    }
    
    
    public Color getColor() { return color; }
    public void setColor(Color c) { color = c; }
    
    public void calculateForces(double dt) {
        Iterator i  = forces.iterator();
        AbstractDoubleVector totalForce = new Double2Vector(0,0);
        while(i.hasNext()) {
            totalForce = totalForce.add(((Force)i.next()).getVector(dt));
        }
        this.applyForce( totalForce,x,dt );
    }
     public DrawableRigidBodyV2D collide(DrawableRigidBodyV2D p,double theta,double e) {
        Area a = new Area(area);
        Area pa = new Area(p.area);
        double cx,cy;
        a.intersect(pa);
        if(!a.isEmpty()) {
            cx = a.getBounds2D().getCenterX();
            cy = a.getBounds2D().getCenterY();
            super.collide(p,theta,e);
        }
        return this;
     }
     
     public void addExtForce(Force f) {
         forces.add(f);
     }
     
}
