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

Author Topic: [Beginner] Help with fixed Timestep  (Read 6452 times)

0 Members and 1 Guest are viewing this topic.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: [Beginner] Help with fixed Timestep
« Reply #15 on: March 05, 2014, 08:12:43 pm »
Yes that is what I meant. Check the for collisions against the new position, then if any collisions happen you can change the angle of the circle, else no collisions happened just move the circle to the new position. To keep it simple if a collision occurs don't bother trying to move the circle again in that physics update.  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with fixed Timestep
« Reply #16 on: March 05, 2014, 11:24:02 pm »
Okay, so now my calculate-collisions part of my code looks like this:
for (int i = 0; i < ballcount; i++)
{
    for (int j = i + 1; j < ballcount; j++)
    {
        sf::Vector2f newPosI(xSpeed[i] * dt.asSeconds(), ySpeed[i] * dt.asSeconds());
        sf::Vector2f newPosJ(xSpeed[j] * dt.asSeconds(), ySpeed[j] * dt.asSeconds());

        if (Collision(Ball[i].getPosition() + newPosI , Ball[j].getPosition() + newPosJ))
        {
            //Calculate and set the new Velocities
            haveCollided = true;
        }
        else haveCollided = false;

    }
}
if(!haveCollided)
{
    for(int i = 0; i < ballcount; i++  )

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

    }
}
 

And this seems to work quite well!
I hope I did everything right, didn't I? :)

Now I'm going to spend one hour watching bouncing circles bounce around my screen and see if the circles still stick together sometimes  ;D

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: [Beginner] Help with fixed Timestep
« Reply #17 on: March 06, 2014, 02:37:20 am »
Don't stop all movement just because one circle collided. Check my logic and see how it is done.

Only do collision detection for a single circle at a time using only that single circles new position. If that single circle's new position collides with any of the other circle's old position then simply don't move that single circle.

else haveCollided = false;

Don't set it to false, it should already start with a value of false outside your inner loop (but still inside your outer loop  :)). And if a collision happens within your inner loop call break to avoid checking for any other collision.

for (int j = i + 1; j < ballcount; j++)

You inner loop still needs to loop over every single circle, not just the circles that come after the one that collision is currently being preformed on. Just add a single check inside the inner loop to avoid checking for collision against the same circle. A simple if (i == j) continue; will work (same as in my code).  ;)


On a side note, if you hop on IRC I would be glad to help get you straightened out (with this or any other problems).  ;D
« Last Edit: March 06, 2014, 03:36:24 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: [Beginner] Help with fixed Timestep
« Reply #18 on: March 06, 2014, 03:36:37 pm »
Thanks zsbzsb!

Problem is finally solved  :)

 

anything