C++??С??????????????HGE???棩
???????????? ???????[ 2014/5/8 11:14:05 ] ????????net ??С?? ???????
?????????????????????????????????????д???
??????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;
}
|
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11