当前位置:网站首页>QT based "synthetic watermelon" game
QT based "synthetic watermelon" game
2022-06-26 20:59:00 【biyezuopinvip】
Resource download address :https://download.csdn.net/download/sheziqiong/85788271
Resource download address :https://download.csdn.net/download/sheziqiong/85788271
Main process
Overall process
Realize the idea : By opening Mainscene Interface , Define fruits fruit, Implement various operations .

Algorithm or formula
The core algorithm : Two dimensional collision
principle : Because two balls collide , The velocities on the tangent are parallel to each other , There is no force and the connecting lines collide with each other ( As shown in the figure below ), There will be forces , So we just need to find the ball 1 And the ball 2 The velocity in the direction of the centreline of . Then, according to the law of conservation of momentum and the law of conservation of mechanical energy, we can find the ball after collision 1 And the ball 2 The direction of the connecting center line of . Finally, by adding their respective velocities on the tangent to each other, we can get their respective velocities after collision x Speed ,y Speed .

and v1t : It's the ball 1 The projection speed in the direction of the connecting center line and the tangent line
- and v2t : It's the ball 2 The projection speed in the direction of the connecting center line and the tangent line
The ball 1 The direction of the speed , be equal to v1n + v1t
The ball 2 The direction of the speed , be equal to v2n + v2t
distance = Math.sqrt(Math.pow((ball1.pointX - ball2.pointX),2) + Math.pow((ball1.pointY - ball2.pointY),2));
radius = ball1.r + ball2.r;
dx = ball1.pointX - ball2.pointX
dy = ball1.pointY - ball2.pointY
ex = dx / radius;
ey = dy / radius; // Get the unit vector of the centerline (ex,ey)
= ex * ball1.vx + ey * ball1.vy
= ex * ball2.vx + ey * ball2.vy

’ : The ball 1 The direction of velocity after the collision , be equal to v1n’ + v1t
’ : The ball 2 The direction of velocity after the collision , be equal to v2n’ + v2t
‘ and v2n’ :
Projection velocity of two small balls after collision
If the two balls are the same size , v1n’ and v2n’ The value is :
’ = v2n
’ = v1n
And according to the law of conservation of momentum and the law of conservation of mechanical energy :

and v2 : Speed before two balls hit .
and m2 : The mass of the two balls
' and v2' :
The speed of the two balls after they hit each other
double ux=-dy/distance,uy=dx/distance;// Tangential unit vector (ux,uy), Direction is (ex,ey) Counter clockwise rotation π/2
// The velocity projection of the ball in the direction of the line between the two balls is v=(vx,vy)·(ex,ey)
double v1=ex*balls[i].vx+ey*balls[i].vy;//i Normal velocity of
double v2=ex*balls[j].vx+ey*balls[j].vy;//j Normal velocity of
if (v2<v1) continue;// Two balls passed by , No collision
// The velocity projection of the ball in the tangent direction is v'=(vx,vy)·(ux,uy)
double v11=ux*balls[i].vx+uy*balls[i].vy;//i Tangential velocity of
double v22=ux*balls[j].vx+uy*balls[j].vy;//j Tangential velocity of
// According to the conservation of momentum and kinetic energy theorem, the velocity in the direction of the connecting line between the two balls after collision is calculated u1 and u2, among K Indicates the degree of kinetic energy loss
double m1=balls[i].mass,m2=balls[j].mass;
double u1=K*((m1-m2)*v1+2*m2*v2)/(m1+m2);//i Normal velocity after collision
double u2=K*((m2-m1)*v2+2*m1*v1)/(m1+m2);//j Normal velocity after collision
velocity vector (vx,vy)=(u1ex+v11ux,u1ey+v11uy)·(i,j)
balls[i].vx=1*(u1*ex+v11*ux);
balls[i].vy=1*(u1*ey+v11*uy);
balls[j].vx=1*(u2*ex+v22*ux);
balls[j].vy=1*(u2*ey+v22*uy);
Synthesis algorithm
combine(int i, int j)
{
Ball b;
initball(balls[i].type+1);
double vi=sqrt(balls[i].vx*balls[i].vx+balls[i].vy*balls[i].vy);
double vj=sqrt(balls[j].vx*balls[j].vx+balls[j].vy*balls[j].vy);
if (vi<vj)// The position of the composite is the ball with lower speed
{
=b._x=balls[i].x;
=b._y=balls[i].y;
}
else {
=b._x=balls[j].x;
=b._y=balls[j].y;
}
vx=(balls[i].vx+balls[j].vx)/2.0;
vy=(balls[i].vy+balls[j].vy)/2.0;
ay=1;
falling=1;
balls[i]=b; // Replace the new ball with balls[i]
collideWall(i);
balls[j]=balls[balls.size()-1];
balls.pop_back(); // from balls Delete in array balls[j]
gamescore+=2*balls[i].type;
str.sprintf("score:%d",gamescore);
q->setText(str);
}
Harvest
unit testing

( Start interface )

( Game pause )

( The game is in progress )
Resource download address :https://download.csdn.net/download/sheziqiong/85788271
Resource download address :https://download.csdn.net/download/sheziqiong/85788271
边栏推荐
- 【protobuf 】protobuf 升级后带来的一些坑
- 手机股票注册开户有没有什么风险?安全吗?
- What are the specific steps for opening a stock account? Is it safe to open an account online?
- Fixed length memory pool
- Gee: calculate the maximum and minimum values of pixels in the image area
- Feitian +cipu body brings more imagination to the metauniverse
- Garbage collection mechanism of browser
- 基于QT实现简单的连连看小游戏
- Swagger: how to generate beautiful static document description pages
- SentinelResource注解详解
猜你喜欢
Detailed explanation of stored procedures in MySQL
![[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination](/img/f9/68b5b5ce21f4f851439fa061b477c9.jpg)
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination

超分之VRT

Flutter TextField详解

12个MySQL慢查询的原因分析

leetcode刷题:字符串02( 反转字符串II)

leetcode刷题:字符串06(实现 strStr())

Record a redis large key troubleshooting

好物推荐:移动端开发安全工具

Leetcode question brushing: String 03 (Sword finger offer 05. replace space)
随机推荐
Sword finger offer II 098 Number of paths / Sword finger offer II 099 Sum of minimum paths
0基础学c语言(3)
好物推薦:移動端開發安全工具
Feitian +cipu body brings more imagination to the metauniverse
证券开户安全吗,有没有什么危险呢
Jz-062- the k-th node of binary search tree
Good thing recommendation: mobile terminal development security tool
The source code that everyone can understand (I) the overall architecture of ahooks
Unity——Mathf. Similarities and differences between atan and atan2
515. 在每个树行中找最大值
好物推荐:移动端开发安全工具
宝藏又小众的覆盖物PBR多通道贴图素材网站分享
MySQL - database creation and management
【贝叶斯分类3】半朴素贝叶斯分类器
c语言简单的登录
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data
Gd32 USB composite device file descriptor
Flutter TextField详解
Leetcode question brushing: String 03 (Sword finger offer 05. replace space)
动态规划111