标签:style blog class code java ext
程序是一个月前完成的,之前一直没正儿八经的来整理下这个程序,感觉比较简单,不过即使简单的东西也要跟大家分享下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 |
#pragma once #include <math.h> #include "hge.h" class
CBall { public : CBall(); CBall( float
_x, float
_y, float
_speedX , float
_speedY, float
_radius , DWORD
_color, float
_density = 1.0f); ~CBall(){}; public : bool
IsCollision(CBall *ball, float
dt); //碰撞检测 void
CollisionWith(CBall *ball); //弹性正碰 void
CollisionWith2(CBall *ball); //弹性斜碰 void
SwapColor(CBall *ball); //好玩点,加个交换颜色 void
MoveNext( float
dt, float
_width, float
_height); //由于程序不大,方便起见所有就都public了 public : float
x; //x轴坐标 float
y; //y轴坐标 float
speed_x; //x轴方向速度 float
speed_y; //x轴方向速度 float
radius; //球体半径 float
density; //密度 float
weight; //质量 DWORD
color; //混合颜色 }; |
1
2
3
4
5
6
7
8
9
10
11 |
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 ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 |
void
CBall::CollisionWith2(CBall *ball) { //参考资料: //球心点 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; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
void
CBall::MoveNext( float
dt, float
_width, float
_height) { float
moveX = speed_x*dt; float
moveY = speed_y*dt; //x方向边界 if
(x+moveX<radius||x+moveX>_width-radius) speed_x = -speed_x; //Y方向边界 if (y+moveY<radius||y+moveY>_height-radius) speed_y = -speed_y; x+=speed_x*dt; y+=speed_y*dt; } |
C++多小球非对心弹性碰撞(HGE引擎),布布扣,bubuko.com
标签:style blog class code java ext
原文地址:http://www.cnblogs.com/yinlong1991/p/3710247.html