# How do I prevent jitter amongst near-stationary physics objects?

I have been implementing a custom physics engine, and I'm pretty close to having it working as I would like it to. There is a gravitational force, collisions and collision response. Unfortunately, there seems to be some jitter among near-stationary objects, most likely due to unchangeable low physics ticks.

I have looked online, and tried some of the implementations I have found, including some of my own attempts. Here are the solutions I tried:

• Damping movement when speed/momentum/potential energy is below a threshold.
• Only applying gravity when speed/momentum/potential energy is above threshold.
• Implementing a sleep function. that checks the position of the object for the last 60 frames, and sleeps if it hasn't moved outside a threshold bounding box.
• Iterating through the objects from top to bottom when applying collision testing and resolution.

Here is my code:

for each (auto ball in m_Balls)
{
ball->Update(t);
ball->Accelerate(m_Gravity);
}

// This disgusting hack sorts the balls by height. In a more complete physics
// implementation, I guess I could change the sorting based on the direction of
// gravitational force. This hack is necessary to prevent balls being pulled downwards
// into other balls by gravity; by calculating from the bottom of the pile of
// objects, we avoid issues that occur when adjustments push the object towards gravity.
m_Balls.sort([](const CSprite* a, const CSprite* b)
{return a->m_pos.m_y < b->m_pos.m_y; });

static float cor = 0.8f;

for each (auto ball in m_Balls)
{
for each (auto collider in m_Walls)
{
if (collider->HitTest(ball, 1))
{
float offset = 0;
auto n = Helper::GetNormal(ball, collider, offset);

ball->SetPosition(ball->GetPosition() + (n * offset));

auto r = ball->GetVelocity() - ((1 + cor) * Dot(ball->GetVelocity(), n) * n);

ball->SetVelocity(r);

ball->SetPosition(ball->GetPosition() + ball->GetVelocity() * DeltaTime());
}
}

for each (auto collider in m_Balls)
{
if (ball == collider)
{
break;
}

auto diff = collider->GetPosition() - ball->GetPosition();

float distance = diff.Length();

if (distance <= (ball->GetWidth() / 2) + (collider->GetWidth() / 2))
{
auto midPoint = (ball->GetPosition() + collider->GetPosition()) * 0.5f;

adjustment = diff.Normalise() * (ball->GetWidth() / 2
- Distance(ball->GetPosition(), midPoint));
diff = collider->GetPosition() - ball->GetPosition();

if (Dot(ball->GetVelocity() - collider->GetVelocity(), diff) > 0)
{
auto n = diff.Normalise();
auto u = Dot(cor * ball->GetVelocity() - collider->GetVelocity(), n) * n;
ball->Accelerate(-u);
collider->Accelerate(u);
}
}
}

if (ball->GetSpeed() > MAX_SPEED)
{
ball->SetSpeed(MAX_SPEED);
}
}


How do I prevent jitter amongst near-stationary physics objects?

Well, it turns out that one of my boolean checks was causing this issue.

This code here:

if (ball == collider)
{
break;
}


Was breaking everything. I was under the impression that it would simply ignore collisions with itself but for whatever reason, it was prevent collisions happening in the correct order. Not exactly sure why, I think it's a bug in the game engine I'm using. Anyway, here is the working code, which implements a sleep state for all balls - when movement after 30 frames is confined to certain bounding area, the object is put in a sleep state, during which no forces are applied to it (gravity in this instance). It is woken after it is shifted outside of this bounding area by something - typically a adjustment or collision by another ball.

    // This disgusting hack sorts the balls by height
// In a more complete physics implementation I guess I could change the sorting based on the direction of gravitational force
// This hack is necessary to prevent balls being pulled downwards into other balls by gravity... By calculating
// From the bottom of the pile of objects, we avoid issues that occur when adjustments push the object towards gravity.
m_Balls.sort([](const CSprite* a, const CSprite* b) { return a->m_pos.m_y < b->m_pos.m_y; });

static float cor = 0.5f;

for each (auto ball in m_Balls)
{
ball->Update(t);

if (jitterBoundX[ball].size() < 30)
{
jitterBoundX[ball].push_back(ball->GetX());
jitterBoundY[ball].push_back(ball->GetY());
}
else
{
jitterBoundX[ball].pop_front();
jitterBoundY[ball].pop_front();
jitterBoundX[ball].push_back(ball->GetX());
jitterBoundY[ball].push_back(ball->GetY());

float minx = jitterBoundX[ball].front();
float maxx = minx;

for each (auto f in jitterBoundX[ball])
{
if (f < minx) { minx = f; }
if (f > maxx) { maxx = f; }
}

float miny = jitterBoundY[ball].front();
float maxy = miny;

for each (auto f in jitterBoundY[ball])
{
if (f < miny) { miny = f; }
if (f > maxy) { maxy = f; }
}

auto xdif = maxx - minx;
auto ydif = maxy - miny;

if (ball->GetState() == 0 && xdif < 3 && ydif < 3)
{
ball->SetState(1);
}
else if (ball->GetState() == 1 && (xdif > 3 || ydif > 3))
{
ball->SetState(0);
}
}

if (ball->GetState() == 0)
{
ball->Accelerate(m_Gravity);
}
else
{
ball->SetVelocity(CVector(0, 0));
}

if (IsLButtonDown())
{
ball->Accelerate(0.3f * ((CVector)GetMouseCoords() - ball->GetPosition()));
}

for each (auto collider in m_Walls)
{
if (collider->HitTest(ball, 1))
{
float offset = 0;
auto n = Helper::GetNormal(ball, collider, offset);

ball->SetPosition(ball->GetPosition() + (n * offset));

auto r = ball->GetVelocity() - ((1 + cor) * Dot(ball->GetVelocity(), n) * n);

ball->SetVelocity(r);
}
}

for each (auto collider in m_Balls)
{
// This breaks everything.
//if (ball == collider)
//{
//  break;
//}

if (ball->HitTest(collider, 0))
{
auto diff = collider->GetPosition() - ball->GetPosition();

float distance = diff.Length();

if (ball->HitTest(collider, 0))
{
if (distance < (ball->GetWidth() / 2) + (collider->GetWidth() / 2))
{
auto midPoint = (ball->GetPosition() + collider->GetPosition()) * 0.5f;

auto discrepancy = (collider->GetWidth() / 2 - Distance(collider->GetPosition(), midPoint));
adjustment = diff.Normalise() * discrepancy;
diff = collider->GetPosition() - ball->GetPosition();

//This actually seems to contribute to the wandering issue, it seems worth calculating the opposite collision
//As there may be adjustments made to the position during the previous iteration...
//if (gridSquares[GetGridIndex(midPoint, SPHERE_RAD)] == true)
//{
//  break;
//}
//gridSquares[GetGridIndex(midPoint, SPHERE_RAD)] = true;

if (Dot(ball->GetVelocity() - collider->GetVelocity(), diff) > 0)
{
auto n = diff.Normalise();
auto u = Dot(cor * ball->GetVelocity() - collider->GetVelocity(), n) * n;
ball->Accelerate(-u);
collider->Accelerate(u);
}
}
}
}
}

if (ball->GetSpeed() > MAX_SPEED)
{
ball->SetSpeed(MAX_SPEED);
}
}

• Probably why the break was breaking things: You are looping through colliders and want to skip colliding with yourself. However, when you skip ball, you want to finish going through the rest of the colliders. If you use break, it terminates the loop, and doesn't get a chance to check the rest of the colliders. Your modified code avoids this essentially by checking if ball != collider and then doing all of your stuff. – Richard Hansen Jan 25 '17 at 3:08
• Hi Richard - Realised this while sitting in bed last night... Good example of what kind of code I write without coffee in my veins... – あらまあ Jan 25 '17 at 9:31
• @Gnemlock sorry, done. – あらまあ May 8 '17 at 10:33