A perfect circle for a perfect circle and a perfect circle for a straight line of collisions Java PROCESSING

I'm new to Java, but decided to make an application that has a bunch of balls bouncing around. Please keep in mind that I know almost nothing about the normals, which I have already mentioned a lot in this thread. I also take only algebra 1 and know a little trigger (sin, cosine and tangent). Anyway...

I have detected collision detection using
if (java.awt.geom.Point2D.distance(X1, Y1, X2, Y2) < ball1.radius + ball2.radius) {return true;} else {return false;}

and if (ball.x + ball.radius > getWidth) {COLLISION}

etc. for all four walls

What I need to do is handle these collisions in a semi-realistic way without switching speeds and directions.

Everything is done in JPanel

Thanks for your help in advance.

+3
2

, , . , . , , , , .:)

- (: = 1), ( 90 ) . , , . , ( , 0,0):

top: (0,1)
bottom: (0,-1)
left: (1,0)
right: (-1,0)

, "" , . :

V2 = V1 - 2*N*(N.dot(V1))

V1 - ( ), N - , , V2 - ( ). "N.dot(V1)" " " N V1, (N.xV1.x + N.yV1.y).

, (P - , Q - ):

alt text

psuedocode:

float nx = 0, ny = 1; // replace these with the normal of the wall you hit
float ix = ball.vx, iy = ball.vy; // incident vector
float dotprod = (nx*ix + ny*iy); // the dot product i mentioned earlier

ball.vx = ix - 2*nx*(dotprod);
ball.vy = iy - 2*ny*(dotprod);

, - .:) , , , , ...

+4
+1

Source: https://habr.com/ru/post/1523457/


All Articles