当前位置:网站首页>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
边栏推荐
猜你喜欢

Detailed explanation of shutter textfield
![[serial] shuotou O & M monitoring system 01 overview of monitoring system](/img/b2/bc75a4d0c8d98056d93ba99b3e6193.png)
[serial] shuotou O & M monitoring system 01 overview of monitoring system

回首望月

好物推荐:移动端开发安全工具
![[Bayesian classification 2] naive Bayesian classifier](/img/44/dbff297e536508a7c18b76b21db90a.png)
[Bayesian classification 2] naive Bayesian classifier

IDEA 报错:Process terminated【已解决】

大家都能看得懂的源码(一)ahooks 整体架构篇

Muke 11. User authentication and authorization of microservices

众多碎石3d材质贴图素材一键即可获取

Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印
随机推荐
基于QT实现简单的连连看小游戏
C exercise. Class list plus records, display records and clear records
Sword finger offer II 091 Paint the house
Is it safe to open an account for CICC Wealth Online?
0基础学c语言(1)
回溯思路详解
【山东大学】考研初试复试资料分享
好物推薦:移動端開發安全工具
lotus configurations
郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布
710. random numbers in the blacklist
leetcode刷题:字符串04(颠倒字符串中的单词)
网上开户万一免五到底安不安全?
C primer plus learning notes - 3. Character IO (input / output)
JWT operation tool class sharing
好物推荐:移动端开发安全工具
MySQL - subquery usage
leetcode刷题:哈希表08 (四数之和)
The source code that everyone can understand (I) the overall architecture of ahooks
Sword finger offer II 098 Number of paths / Sword finger offer II 099 Sum of minimum paths