Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Beginner] Help with Bouncing Circles  (Read 45044 times)

0 Members and 1 Guest are viewing this topic.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
[Beginner] Help with Bouncing Circles
« on: January 31, 2014, 10:53:49 pm »
Hello everybody!

First of all: Excuse my terrible English  ;)

Im new to SFML and I made a little Program that shows some Circles Bouncing around in the window. That works fine.
But now I want that the Circles bounce away when the collide with another Circle.
How can I implement that?


Assassin0795

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #1 on: January 31, 2014, 10:58:09 pm »
You'd want to make it so the circles have boundaries; one of the simplest ways I've worked with collision is box collision - that is, you make a box around the entity you wish to implement collision for, and when two entities collide (for example, when the left boundary of thisCircle is at the same coordinates of the right boundary of thatCircle), you can then add code as to how they behave.

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #2 on: January 31, 2014, 11:00:32 pm »
You don't need bounding boxes for only circles. Circle to circle collisions detection is easy: if the distance between their centers is longer than the sum of their radiuses, they collide.

Here's a good tutorial: http://gamedevelopment.tutsplus.com/tutorials/when-worlds-collide-simulating-circle-circle-collisions--gamedev-769

Assassin0795

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #3 on: February 01, 2014, 03:18:17 am »
You don't need bounding boxes for only circles. Circle to circle collisions detection is easy: if the distance between their centers is longer than the sum of their radiuses, they collide.

Here's a good tutorial: http://gamedevelopment.tutsplus.com/tutorials/when-worlds-collide-simulating-circle-circle-collisions--gamedev-769

^ And it's things like this that I need to remember if I plan to get a job programming... Ignore my first post; sorry!

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #4 on: February 01, 2014, 05:19:18 am »
Read this: http://www.gamasutra.com/view/feature/131424/pool_hall_lessons_fast_accurate_.php?print=1
It describes very nicely how to do circle/circle collision detection.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #5 on: February 01, 2014, 03:38:09 pm »
Okay, Thanks for the fast replies, I will try to implement the methods from the tutorials later.

I found a new Problem: I use a Clock, a xSpeed and a ySpeed to Calculate the Bouncing-Direction, when a Ball Collides with a Window-Border. But sometimes, a Ball slides along the Window Border, it doesnt Bounce away.  :(

Have you any Idea what I can do to solve this Problem?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [Beginner] Help with Bouncing Circles
« Reply #6 on: February 01, 2014, 03:52:09 pm »
When you detect a collision you need to calculate bounce, the new direction and length of your velocity vector, depending on where on the circle the collision happens and the velocities and mass of the objects involved.
This is all described in great detail in the gamasutra article I linked you to above.

A little physics and vector math is really all that's needed and in 2d it's not too complicated.

Ohh and by the way, a sf::Vector2f is a convenient container for you vectors rather than xSpeed/ySpeed variables, but it makes no real difference, it's just convenient :)

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #7 on: February 01, 2014, 03:57:48 pm »
Okay, I solved the Sliding-Window-Border-Problem by making some changes with the Clock.  :)


Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #8 on: February 01, 2014, 05:16:14 pm »
Okay, my Project is nearly complete, but I've noticed that my CPU is getting hot when I run my Application.
It uses a lot of the system resources.  ???

Is there any way to stop that?

I've got Win7 Ultimate, an i5-CPU and 8GB RAM.

Thanks in advance for your Help!
« Last Edit: February 07, 2014, 01:24:11 pm by Cadisol87 »

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #9 on: February 01, 2014, 05:21:11 pm »
Did you enable vertical synchronization or set a frame rate limit?

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #10 on: February 01, 2014, 05:32:03 pm »
Oops... Thanks a lot, G!
I enabled Vertical Sync and now it works perfectly without slowing down my system.  :)

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #11 on: February 01, 2014, 07:35:47 pm »
Theres another problem now... I thought I solved it, but now its there again... Sometimes, when a Ball Collides with the Window-Border and should Bounce away, it sticks on the border, and only moves at one Axis. For example, when the Ball reaches the Top Window-Border and should bounce away, it sticks on the border and only moves from one side to the other on the x-Axis.

Has anyone a possible solution?

I appreciate any help and patience with an Beginner like me!  :)

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #12 on: February 02, 2014, 04:13:10 am »
Theres another problem now... I thought I solved it, but now its there again... Sometimes, when a Ball Collides with the Window-Border and should Bounce away, it sticks on the border, and only moves at one Axis. For example, when the Ball reaches the Top Window-Border and should bounce away, it sticks on the border and only moves from one side to the other on the x-Axis.

Has anyone a possible solution?

I appreciate any help and patience with an Beginner like me!  :)

Can you show us the code?

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #13 on: February 02, 2014, 11:48:23 am »
Of course, Azaral!

Here it is:
#include <SFML/Graphics.hpp>
#include <windows.h>
#include <iostream>
#include <vector>


using namespace std;

    int width = GetSystemMetrics(SM_CXSCREEN); // Get the Screen-Width
    int height =  GetSystemMetrics(SM_CYSCREEN); //Get the Screen-Height
        int ballradius = 50;
    vector <float> xSpeed{53.4f, 71.7f, 76.7f, 72.5f, 76.1f, 46.1f, 74.4f, 75.6f, 53.2f, 51.8f}; // Well, I dont know if thats very clever...
    vector <float> ySpeed{77.9f, 70.9f, 70.9f, 17.9f, 50.9f, 19.2f, 73.6f, 73.5f, 60.1f, 63.9f};

    bool RightBorderTouching(int TempPositionX)         // Yep, I know that you can do this shorter, but I like 'comprehensive' code.
    {
    int RightBorderPosition = TempPositionX + ballradius * 2;
    if(RightBorderPosition >= width){return true;}
    if(RightBorderPosition < width){return false;}
    }

    bool LeftBorderTouching(int TempPositionX)
    {
    int LeftBorderPosition = TempPositionX;
    if(LeftBorderPosition <= 0){return true;}
    if(LeftBorderPosition > 0){return false;}
    }

    bool TopBorderTouching(int TempPositionY)
    {
    int TopBorderPosition = TempPositionY;
    if(TopBorderPosition <= 0){return true;}
    if(TopBorderPosition > 0){return false;}
    }

    bool BottomBorderTouching(int TempPositionY)
    {
    int BottomBorderPosition = TempPositionY + ballradius * 2;
    if(BottomBorderPosition >= height){return true;}
    if(BottomBorderPosition < height){return false;}
    }

int main()

{

cout << "Width: " << width << endl;
cout << "Height: " << height << endl;

vector <sf::CircleShape> Ball(10); // Should I make the Balls within a vector or separately?
    for(int i = 0; i < 10; i++  )
    {
        Ball[i].setRadius(ballradius);
        Ball[i].setPointCount(100);
        Ball[i].setFillColor(sf::Color(34 , 186 , 186 ));
        Ball[i].setOutlineThickness(5);
        Ball[i].setOutlineColor(sf::Color(88, 97, 97));
        Ball[i].setPosition( 245 , 245);
    }

     sf::Clock clock;

     sf::ContextSettings settings;
     settings.antialiasingLevel = 8;

    sf::RenderWindow window(sf::VideoMode(width, height), "Bouncing Balls", sf::Style::Fullscreen, settings);

    window.setVerticalSyncEnabled(true);



    while (window.isOpen())
    {



        sf::Event event;
        while (window.pollEvent(event))
        {

            if (event.type == sf::Event::Closed)
            {
                window.close();
            }

            if (event.type == sf::Event::KeyPressed)
{

    if (event.key.code == sf::Keyboard::Escape)
    {
        window.close();
    }
}


        }

        auto  dt = clock.restart().asSeconds();
        window.clear(sf::Color::White);
        for(int i = 0; i < 10; i++  )

    {
       Ball[i].move(xSpeed[i] * dt, ySpeed[i] * dt );

    }

        for(int i = 0; i < 10; i++  )
    {
        window.draw(Ball[i]);
    }

        window.display();

for(int i = 0; i < 10; i++  )
    {
        if(RightBorderTouching(Ball[i].getPosition().x)) {xSpeed[i] = -xSpeed[i];}
        if(LeftBorderTouching(Ball[i].getPosition().x)) {xSpeed[i] = -xSpeed[i];}
        if(TopBorderTouching(Ball[i].getPosition().y)) {ySpeed[i] = -ySpeed[i];}
        if(BottomBorderTouching(Ball[i].getPosition().y)){ySpeed[i] = -ySpeed[i];}
    }
    }

    return 0;
}
 


krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: [Beginner] Help with Bouncing Circles
« Reply #14 on: February 02, 2014, 02:14:00 pm »
You forgot to move ball so it doesn't collide anymore. For example ball at {x:50 y:0} and radius 10 should be moved to {x: 50 y:10}.
SFML.Utils - useful extensions for SFML.Net

 

anything