下面依次说下各个成员方法的作用,部分有代码。
  (1)构造方法CBall(float _x,float _y,float ... ,float _density = 1.0f);对成员变量初始化,需要注意,密度默认是1.0f,而质量通过体积和密度的计算求得。【球体质量 m=ρ*v , v = 4/3*π*r^3】。
  (2)碰撞检测,返回当前对象时候与参数ball发生碰撞,dt是一帧的时间。

 

bool CBall::IsCollision(CBall *ball,float dt)
{
//计算的是下一刻的位置,以免发生粘连
float disX = (this->x+this->speed_x*dt)-(ball->x+ball->speed_x*dt);
float disY = (this->y+this->speed_y*dt)-(ball->y+ball->speed_y*dt);
float dis = sqrt(disX*disX+disY*disY);
//判断下一刻是否 发生碰撞
if(dis < this->radius+ball->radius)
return true;
return false;
}

  (3)弹性正碰 void CBall::CollisionWith(CBall *ball)根据动能定理和动量守恒定律得出公式
  //弹性正碰撞公式
  //v1' = [ (m1-m2)*v1 + 2*m2*v2 ] / (m1+m2)
  //v2' = [ (m2-m1)*v2 + 2*m1*v1 ] / (m1+m2)
  碰撞后,两小球的速度,方向,会发生改变。
  (4)弹性斜碰,文章的主角,由于计算比较复杂所以需要用到向量(Vector)。

 

void CBall::CollisionWith2(CBall *ball)
{
//参考资料:
//http://www.cnblogs.com/kenkofox/archive/2011/09/06/2168944.html
//http://tina0152.blog.163.com/blog/static/119447958200910229109326/
//球心点
float x1 = this->x ;
float y1 = this->y ;
float x2 = ball->x ;
float y2 = ball->y ;
//碰撞处切平面向量t,及其法向量s
hgeVector s(x2-x1, y2-y1);
s.Normalize();//标准化矢量
hgeVector t(x2-x1, y2-y1);
t.Rotate(3.1415926f/2);
t.Normalize();
//速度向量
hgeVector v1(this->speed_x,this->speed_y);
hgeVector v2(ball->speed_x,ball->speed_y);
//先算v1(v1x, v1y)在s和t轴的投影值,分别设为v1s和v1t
//再算v2(v2x, v2y)在s和t轴的投影值,分别设为v2s和v2t:
float v1s = v1.Dot(&s);
float v1t = v1.Dot(&t);
float v2s = v2.Dot(&s);
float v2t = v2.Dot(&t);
//转换后于s向量上的弹性正碰撞。质量不等
//弹性正碰撞公式
//v1' = [ (m1-m2)*v1 + 2*m2*v2 ] / (m1+m2)
//v2' = [ (m2-m1)*v2 + 2*m1*v1 ] / (m1+m2)
float m1 = this->weight;
float m2 = ball->weight;
float temp_v1s = ((m1-m2)*v1s + 2*m2*v2s )/ (m1+m2);
v2s = ((m2-m1)*v2s + 2*m1*v1s )/ (m1+m2);
v1s = temp_v1s;
//首先求出v1t和v2t在t轴的向量v1t'和v2t'(将数值变为向量)
//再求出v1s'和v2s'在s轴的向量v1s'和v2s'(将数值变为向量)
hgeVector v1tVector = t*v1t;
hgeVector v1sVector = s*v1s;
hgeVector v2tVector = t*v2t;
hgeVector v2sVector = s*v2s;
//新速度矢量
hgeVector v1_new = v1tVector+v1sVector;
hgeVector v2_new = v2tVector+v2sVector;
//划分成x,y方向分量速度
this->speed_x = v1_new.x;
this->speed_y = v1_new.y;
ball->speed_x = v2_new.x;
ball->speed_y = v2_new.y;
}