N
N
Nem0_o2015-04-11 14:04:22
Programming
Nem0_o, 2015-04-11 14:04:22

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;
    }
}

Also, all balls are the same size.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2015-04-11
@0neS

ecto.ru/details/simple_drop

S
Skye Trip, 2015-04-12
@Akite

Do you need something similar ?
You can try to take this algorithm as a basis.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question