package BoomBoom;
public class Vector2D {
public double x;
public double y;
public Vector2D()
{
x = 0;
y = 0;
}
public Vector2D(double _x, double _y)
{
x = _x;
y = _y;
}
//获取弧度
public double getRadian()
{
return Math.atan2(y, x);
}
//获取角度
public double getAngle()
{
return getRadian() / Math.PI * 180;
}
public Vector2D clone()
{
return new Vector2D(x,y);
}
public double getLength()
{
return Math.sqrt(getLengthSQ());
}
public double getLengthSQ()
{
return x * x + y * y;
}
//向量置零
public Vector2D Zero()
{
x = 0;
y = 0;
return this;
}
public boolean isZero()
{
return x == 0 && y == 0;
}
//向量的长度设置为我们期待的value
public void setLength(double value)
{
double _angle = getAngle();
x = Math.cos(_angle) * value;
y = Math.sin(_angle) * value;
}
//向量的标准化(方向不变,长度为1
public Vector2D normalize()
{
double length = getLength();
x = x / length;
y = y / length;
return this;
}
//是否已经标准化
public boolean isNormalized()
{
return getLength() == 1.0;
}
//向量的方向翻转
public Vector2D reverse()
{
x = -x;
y = -y;
return this;
}
//2个向量的数量积(点积)
public double dotProduct(Vector2D v)
{
return x * v.x + y * v.y;
}
//2个向量的向量积(叉积)
public double crossProduct(Vector2D v)
{
return x * v.y - y * v.x;
}
//计算2个向量的夹角弧度
//参考点积公式:v1 * v2 = cos<v1,v2> * |v1| *|v2|
public static double radianBetween(Vector2D v1, Vector2D v2)
{
if(!v1.isNormalized()) v1 = v1.clone().normalize(); // |v1| = 1
if(!v2.isNormalized()) v2 = v2.clone().normalize(); // |v2| = 1
return Math.acos(v1.dotProduct(v2));
}
//弧度 = 角度乘以PI后再除以180、 推理可得弧度换算角度的公式
//弧度转角度
public static double radian2Angle(double radian)
{
return radian / Math.PI * 180;
}
//向量加
public Vector2D add(Vector2D v)
{
return new Vector2D(x + v.x, y + v.y);
}
//向量减
public Vector2D subtract(Vector2D v)
{
return new Vector2D(x - v.x, y - v.y);
}
//向量乘
public Vector2D multiply(double value)
{
return new Vector2D(x * value, y * value);
}
//向量除
public Vector2D divide(double value)
{
return new Vector2D(x / value, y / value);
}
}
public boolean isCollision(Ball other) {
if(this.id==other.id) return false;
double disX=(this.x+this.speedx)-(other.x+other.speedx);
double disY=(this.y+this.speedy)-(other.y+other.speedy);
double dis=Math.sqrt(disX*disX+disY*disY);
if(dis<=(this.radius+other.radius)){
return true;
}
else
return false;
}
public void collisionWith2(Ball other) {
double x1=this.x;
double y1=this.y;
double x2=other.x;
double y2=other.y;
Vector2D s=new Vector2D(x2-x1,y2-y1);
s.normalize();
Vector2D t=new Vector2D(y1-y2,x2-x1);
t.normalize();
Vector2D v1=new Vector2D(this.speedx,this.speedy);
Vector2D v2=new Vector2D(other.speedx,other.speedy);
double v1s=v1.dotProduct(s);
double v1t=v1.dotProduct(t);
double v2s=v2.dotProduct(s);
double v2t=v2.dotProduct(t);
double m1=this.radius*this.radius*this.radius;
double m2=other.radius*other.radius*other.radius;
double temp_v1s=((m1-m2)*v1s+2*m2*v2s)/(m1+m2);
v2s=((m2-m1)*v2s+2*m1*v1s)/(m1+m2);
v1s=temp_v1s;
double temp_v1t=((m1-m2)*v1t+2*m2*v2t)/(m1+m2);
v2t=((m2-m1)*v2t+2*m1*v1t)/(m1+m2);
v1t=temp_v1t;
Vector2D v1tVector =t.multiply(v1t);
Vector2D v1sVector =s.multiply(v1s);
Vector2D v2tVector =t.multiply(v2t);
Vector2D v2sVector =s.multiply(v2s);
Vector2D v1_new=v1tVector.add(v1sVector);
Vector2D v2_new=v2tVector.add(v2sVector);
this.speedx=v1_new.x;
this.speedy=v1_new.y;
other.speedx=v2_new.x;
other.speedy=v2_new.y;
}