#include <iostream>
#include <math.h>
//顶点,向量
typedef struct {
float x, y, z;
} VECTOR3;
typedef struct
{
float fRadius; //球的半径
VECTOR3 vSpeed; //球的速度
VECTOR3 vPosition; //球的位置
}BoB;
char bob_collision_check(BoB* b1,BoB* b2);//碰撞检测,返回值 1发生碰撞,0未发生碰撞
char bobs_collision_check(BoB* BoBArray[],unsigned char number);//number个球之间的碰撞,要求更新每个球的速度方向
VECTOR3 unitFunc(VECTOR3 V); //求单位向量
VECTOR3 normalFunc(VECTOR3 V);//法微量及单位向量
char bob_collision_check(BoB* b1,BoB* b2)//碰撞检测,返回值 1发生碰撞,0未发生碰撞
{
float temp;
temp = (b1->vPosition.x - b2->vPosition.x) * (b1->vPosition.x - b2->vPosition.x ) + (b1->vPosition.y - b2->vPosition.y) * (b1->vPosition.y - b2->vPosition.y ) ;
float dis = sqrt( temp);
if( dis <= b1->fRadius + b2->fRadius)
{
printf("碰撞/n/n");
return 1;
}
else
return 0;
}
char bobs_collision_check(BoB * BoBArray[],unsigned char number)//number个球之间的碰撞,要求更新每个球的速度方向
{
number = 2;
for(int i=0; i<2; ++i)
{
printf("第%d个球半径%f 位置x:%fy:%f 速度方向x:%fy:%f/n",i,BoBArray[i]->fRadius,BoBArray[i]->vPosition.x ,BoBArray[i]->vPosition.y,BoBArray[i]->vSpeed.x ,BoBArray[i]->vSpeed.y);
}
float temp1,temp2;
temp1 = BoBArray[0]->vSpeed.x *BoBArray[0]->vSpeed.x + BoBArray[0]->vSpeed.y *BoBArray[0]->vSpeed.y ;
temp2 = BoBArray[1]->vSpeed.x *BoBArray[1]->vSpeed.x + BoBArray[1]->vSpeed.y *BoBArray[1]->vSpeed.y ;
float speed1 = sqrt( temp1); //求BoB1 速度大小
float speed2 = sqrt( temp2); //求BoB2 速度大小
printf("球1 , 球2 的速度大小: %f/t %f /t/n",speed1,speed2);
float temp;
temp = (BoBArray[0]->vPosition.x - BoBArray[1]->vPosition.x) * (BoBArray[0]->vPosition.x - BoBArray[1]->vPosition.x ) + (BoBArray[0]->vPosition.y - BoBArray[1]->vPosition.y) * (BoBArray[0]->vPosition.y - BoBArray[1]->vPosition.y ) ;
float dis = sqrt( temp);
if( bob_collision_check(BoBArray[0],BoBArray[1]) == 1) //碰撞更新速度
{
VECTOR3 b_con,b_con_unit;//两球连线向量
b_con.x = BoBArray[1]->vPosition.x - BoBArray[0]->vPosition.x ;
b_con.y = BoBArray[1]->vPosition.y - BoBArray[0]->vPosition.y ;
printf("连线向量 %f %f/n",b_con.x,b_con.y);
b_con_unit = unitFunc(b_con);
printf("连线间单位向量 %f %f/n",b_con_unit.x,b_con_unit.y);
VECTOR3 normal; //法向量
normal = normalFunc(b_con_unit);
printf("连线单位法向量 %f %f/n/n",normal.x,normal.y);
float dx,dy;
dx = BoBArray[1]->vPosition.x - BoBArray[0]->vPosition.x;
dy = BoBArray[1]->vPosition.y - BoBArray[0]->vPosition.y;
printf("距离差x %f y %f/n",dx,dy);
printf("球1 球2 之间的距离 %f /t/n/n",dis );
float ax,ay ;
ax = dx / dis;
ay = dy / dis;
float tempVn_1,tempVn_2;
tempVn_1 = speed1 * ax;
tempVn_2 = speed2 * ax;
VECTOR3 Vn_1,Vn_2; //切线方向
Vn_1.x = tempVn_1 * normal.x;
Vn_1.y = tempVn_1 * normal.y;
printf("球1 切线方向 速度 %f %f /t/n",Vn_1.x ,Vn_1.y );
Vn_2.x = tempVn_2 * normal.x;
Vn_2.y = tempVn_2 * normal.y;
printf("球2 切线方向 速度 %f %f /t/n",Vn_2.x ,Vn_2.y );
double tempVt_1,tempVt_2; //连线方向
tempVt_1 = speed1 * ay;
tempVt_2 = speed2 * ay;
printf("连线方向 球1 球2 速度大小 %f %f /n",tempVt_1 ,tempVt_1);
VECTOR3 Vt_1,Vt_2;
Vt_1.x = tempVt_1 * b_con_unit.x;
Vt_1.y = tempVt_1 * b_con_unit.y;
printf("球1 连线方向 速度 %f %f /t/n",Vt_1.x ,Vt_1.y );
Vt_2.x =- tempVt_2 * b_con_unit.x;
Vt_2.y = -tempVt_2 * b_con_unit.y;
printf("球2 连线方向 速度 %f %f /t/n",Vt_2.x ,Vt_2.y );
//speedVec tempV1,tempV2;
BoBArray[0]->vSpeed.x = Vn_1.x - Vt_1.x;
BoBArray[0]->vSpeed.y = Vn_1.x - Vt_1.y;
BoBArray[1]->vSpeed.x = Vn_2.x - Vt_2.x;
BoBArray[1]->vSpeed.y = Vn_2.x - Vt_2.y;
}
for(int i=0; i<2; ++i)
{
printf("第%d个球半径%f 位置x:%fy:%f 速度方向x:%fy:%f/n",i,BoBArray[i]->fRadius,BoBArray[i]->vPosition.x ,BoBArray[i]->vPosition.y,BoBArray[i]->vSpeed.x ,BoBArray[i]->vSpeed.y);
}
return 0;
}
//求单位
VECTOR3 unitFunc(VECTOR3 V)
{
VECTOR3 result;
if( V.y == 0 && V.x == 0)
{
VECTOR3 v;
v.x = 0;
v.y = 0;
v.z = 0;
return v;
}else if( V.y == 0 && V.x != 0)
{
if (V.x > 0) result.x = 1 ;
if (V.x < 0) result.x = -1 ;
result.y = 0;
return result;
}
else if( V.x == 0 && V.y != 0)
{
if (V.y > 0) result.y = 1 ;
if (V.y < 0) result.y = -1 ;
result.x = 0;
return result;
}
else if( V.y != 0 && V.x != 0)
{
double s;
s = V.x * V.x + V.y * V.y;
double d;
d = sqrt (s );
result.x = V.x / d ;
result.y = V.y / d ;
return result;
}
}
//法向量
VECTOR3 normalFunc(VECTOR3 V)
{
VECTOR3 result;
if( V.y == 0 && V.x == 0)
{
VECTOR3 v;
v.x = 0;
v.y = 0;
v.z = 0;
return v;
}else if( V.y == 0 && V.x != 0) //水平向量
{
result.x = 0 ;
result.y = 1;
return result;
}
else if( V.y != 0 && V.x == 0) //垂直向量
{
result.x = 1 ;
result.y = 0;
return result;
}
else if( V.y != 0 && V.x != 0)
{
VECTOR3 t;
t.x = - V.y/ V.x ;
t.y = 1;
double s;
s = t.x * t.x + t.y * t.y;
double d;
d = sqrt (s );
result.x = t.x / d ;
result.y = t.y / d ;
return result;
}
}
int main()
{
BoB* b1,*b2;
b1 = new BoB;
b2 = new BoB;
b1->fRadius = 11;
b1->vPosition.x = 100;
b1->vPosition.y = 100;
b1->vSpeed.x = 10;
b1->vSpeed.y = 10;
b2->fRadius = 10;
b2->vPosition.x = 100;
b2->vPosition.y = 120;
b2->vSpeed.x = -10;
b2->vSpeed.y = -10;
bob_collision_check(b1,b2);
BoB * ba[2];
for(int i=0; i<2; ++i)
{
ba[i] = new BoB; //下面记得delete
}
ba[0] = b1;
ba[1] = b2;
bobs_collision_check(ba,2);//number个球之间的碰撞,要求更新每个球的速度方向
::getchar ();
return 0;
}