This code in the open processing does not make sense.
void bounce (Part a, Part b) {
if(sqrt(power(a.posX-b.posX,2)+power(a.posY-b.posY,2))<(a.radius+b.radius)){
if(sqrt(power(a.posX-b.posX,2)+power(a.posY-b.posY,2))>sqrt(power(
a.posX+a.vX-b.posX-b.vX, 2)
+ power(a.posY+a.vY-b.posY-b.vY,2)){
float commonTangentAngle=atan2(b.posX-a.posX,b.posY
- a.posY)
+ asin(1);
I understand that you are distancing yourself in the first if statement, but for the if conditions that follow
if(sqrt(power(a.posX-b.posX,2)+power(a.posY-b.posY,2))>sqrt(power(
a.posX+a.vX-b.posX-b.vX, 2)
+ power(a.posY+a.vY-b.posY-b.vY,2)){
I don't really understand what commonTangentAngle means. I think it's more of a physical exercise formula than a programming formula. I don't even know elementary physics, so maybe it's a textbook-level formula, but please tell me more about it.
processing
if(sqrt(power(a.posX-b.posX,2)+power(a.posY-b.posY,2))>sqrt(power(
a.posX+a.vX-b.posX-b.vX, 2)
+ power(a.posY+a.vY-b.posY-b.vY,2)){
For this if statement, it determines whether the current distance between the two particles is greater than the distance between the particles in the next frame.In other words, we are checking whether the two particles are close to each other or separated.
Collisions occur, of course, only in close proximity.
By the way, the result is the same without sqrt
, so you can rewrite it a little more neatly.
if(power(a.posX-b.posX, 2)+power(a.posY-b.posY, 2)>
power(a.posX+a.vX-b.posX-b.vX,2)+power(a.posY+a.vY-b.posY-b.vY,2))
commonTangentAngle
is the angle of reflection.
© 2024 OneMinuteCode. All rights reserved.