Answer the question
In order to leave comments, you need to log in
How to make the balls bounce from the edges of the screen and from each other?
There is a 2D ball, with coordinates - x, y; the speed of movement - speed and the angle at which it moves - angle.
Do you need an algorithm so that when moving 1. the ball bounces off the established frames and 2. the ball bounces off another ball ?
I made it so that when the ball collides with the edge of the screen, it flies off in the opposite direction:
if(x>0 && y>0 && y<screenHeight && x<screenWidth)
{
x+=dt * speed*cosf(angle * math::PI / 180 );
y+=dt * speed*sinf(angle * math::PI / 180);
if((x<0) || (y<0) || (x>screenWidth) || (y>screenHeight))
{
angle+=180;
if(angle >360)
angle-=360;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question