当前位置:网站首页>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
边栏推荐
- [serial] shuotou O & M monitoring system 01 overview of monitoring system
- 好物推荐:移动端开发安全工具
- 网上开户万一免五到底安不安全?
- The two files are merged into a third file.
- Sword finger offer II 091 Paint the house
- [most detailed] the latest and complete redis interview (70)
- Unity——Mathf. Similarities and differences between atan and atan2
- Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005
- 【贝叶斯分类4】贝叶斯网
- Gee: calculate the maximum and minimum values of pixels in the image area
猜你喜欢

Redis + Guava 本地缓存 API 组合,性能炸裂!

Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005

Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023

How to install mysql8.0 database under Windows system? (Graphic tutorial)

leetcode刷题:哈希表08 (四数之和)
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data

Establish a connection with MySQL

基于QT实现简单的连连看小游戏

GEE:计算image区域内像素最大最小值

回溯思路详解
随机推荐
Serial port application program based on gd32
SentinelResource注解詳解
Leetcode question brushing: String 05 (Sword finger offer 58 - ii. left rotation string)
Gd32 USB composite device file descriptor
Gamefi active users, transaction volume, financing amount and new projects continue to decline. Can axie and stepn get rid of the death spiral? Where is the chain tour?
Gee: calculate the maximum and minimum values of pixels in the image area
Arrête d'être un bébé géant.
[serial] shuotou O & M monitoring system 01 overview of monitoring system
【protobuf 】protobuf 升级后带来的一些坑
Unity - URP get camera stack
超分之VRT
【贝叶斯分类2】朴素贝叶斯分类器
[Bayesian classification 3] semi naive Bayesian classifier
515. find the maximum value in each tree row
BOM and DOM operations
Leetcode: hash table 08 (sum of four numbers)
开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低
Feitian +cipu body brings more imagination to the metauniverse
MySQL中存储过程的详细详解
Detailed explanation of stored procedures in MySQL