collision determination in processing

Asked 2 years ago, Updated 2 years ago, 93 views

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

2022-09-30 19:00

1 Answers

 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.

picture

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.

commonTangentAngle


2022-09-30 19:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.