Welcome to the first actual post on physics and in this part I’ll be talking about Ballistic Motion. Just like I did, you might think to yourself: “What is Ballistic Motion?” Let’s take a look at what Wikipedia has to say.

Ballistics (gr. βάλλειν (‘ba’llein’), “throw”) is the science of mechanics that deals with the flight, behavior, and effects of projectiles, especially bulletsgravity bombsrockets, or the like; the science or art of designing and accelerating projectiles so as to achieve a desired performance.

ballistic body is a body which is free to move, behave, and be modified in appearance, contour, or texture by ambient conditions, substances, or forces, as by the pressure of gases in a gun, by rifling in a barrel, by gravity, by temperature, or by air particles. A ballistic missile is a missile only guided during the relatively brief initial powered phase of flight, whose course is subsequently governed by the laws of classical mechanics.

If you’re not familiar with physics, this might be some information to swallow, but let’s narrow it down to what I’ll be discussing here: How can we make an object (in this case, a ball) fall and bounce on the ground. That narrows it down quite a bit doesn’t it? So what you will see here will be 2 different implementations of a ball that falls down to the ground under the influence of gravity and bounces when it hits the ground. These 2 different implementations will be the Euler Integrator and the Perfect Integrator.

Euler Integrator

So we want to make the ball bounce in the most simplest way, but still convincing. First let’s define some things we need to make this happen. The ball obviously has a position in space, but that’s not going to make it move. We’ll also need the velocity and the rate at which it will accelerate, these 3 vectors will do just fine for what we want to achieve. Let’s do the math:

The key to make the ball move is to integrate, we do that as following:

```Velocity += Acceleration * Timestep;
Position += Velocity * Timestep;```

And this is really all there is to make a convincing falling ball. Now to get to the bouncing part. As I’m only interested (at the moment) with bouncing off the ground I will only show that, to make it also bounce against walls and such it requires a bit more work. So let’s make it bounce.

```float CoefficientOfRestitution = 1.0f;

{
Velocity *= Vector(1.0f, -CoefficientOfRestitution, 1.0f);&nbsp;
}```

As we multiply the Velocity vector with the other vector we just defined, we negate the y component of the Velocity vector with the Coefficient of Restitution, making it move up again after the collision and there it is, the ball is now bouncing on the ground. As fun as this may seem, to make it a bit more realistic we need to change the Coefficient of Restitution to make it look like it’s losing energy every bounce. Change it to 0.9f and it will bounce lower after every bounce eventually stopping.

If you run the simulation you might have noticed that something is not going well sometimes, the ball will fall below the ground and not bounce anymore. This can easily be fixed by saving the previous position before the collision occurred and with this addition, the entire simulation is as following:

```float CoefficientOfRestitution = 1.0f;
vector3 Acceleration = vector3(0.0f, -0.5f, 0.0f);
vector3 Velocity = vector3(0.0f, 0.0f, 0.0f);
vector3 Position = vector3(0.0f, 50.0f, 0.0f);

void Simulate()
{
vector3 PrevPos = Position;

Velocity += Acceleration * TimeStep;
Position += Velocity * TimeStep;
{
Velocity *= Vector(1.0f, -CoefficientOfRestitution, 1.0f);
Position = PrevPos;
}
}```

This is all there is to it for making a convincing bouncing ball and if everything is done right, it should look something like this:

Perfect integrator

Although the above is more than fine in most situations, it is sometimes desired to get a perfect accurate representation of the simulation and to achieve that we will have to use a perfect integrator and with the following formulae we can figure out the exact time of impact.

The integration for the Velocity of the ball will stay the same, but the calculation of the position of the ball changes to get something more accurate.

`vector3 NewPosition = 0.5f * (Acceleration * TimeStep * TimeStep) + Velocity * TimeStep + Position;`

Now with this accurate position we want to find out the exact moment of collision with the following function:

```float GetTimeOfCollision( Vector3 a_Acceleration, Vector3 a_Velocity, Vector3 a_Position )
{
float a = a_Acceleration.y;
float b = a_Velocity.y;
float c = a_Position.y - m_Sphere->GetRadius();

float time;

fl numer = -b - (sqrtf((b * b) - (4.0f * a * c)));

if(numer > 0)
{
time = numer / (2.0f * a);
}
else
{
time = 0.0f;
}

return time;
}```

Now that we know the exact position in time of the collision we can set the ball at that position and set the velocity at that point in time like this:

```			Vector3 prevpos = m_Sphere->GetPosition();
Vector3 prevvel = m_Sphere->GetVelocity();
Vector3 prevacc = m_Sphere->GetAcceleration();
Vector3 newvel;

m_Sphere->SetVelocity(GetVelocityAtTimeT(a_DT, m_Sphere->GetAcceleration(), m_Sphere->GetVelocity()));
m_Sphere->SetPosition(GetPositionAtTimeT(a_DT, m_Sphere->GetAcceleration(), m_Sphere->GetVelocity(), m_Sphere->GetPosition()));

{
float TimeOfCollision = GetTimeOfCollision(prevacc, prevvel, prevpos);
Vector3 PositionAtCollision = GetPositionAtTimeT(TimeOfCollision, prevacc, prevvel, prevpos);
Vector3 VelocityAtCollision = GetVelocityAtTimeT(TimeOfCollision, prevacc, prevvel);

newvel = VelocityAtCollision * Vector3(1, -m_Sphere->GetCoefficientOfRestitution(), 1);

float resttime = a_DT - TimeOfCollision;

m_Sphere->SetVelocity(GetVelocityAtTimeT(resttime, prevacc, newvel));
m_Sphere->SetPosition(GetPositionAtTimeT(resttime, prevacc, newvel, PositionAtCollision));
}```

Before the collision check (the if statement) I just integrate as normal with the formula I previously gave. Once we hit the ground (in this case simply 0 with the radius of the ball to prevent it from going in the ground) I take the previous acceleration, velocity and position before the collision and calculate the exact time of collision at that point. With that time we can calculate that position and velocity. Though this looks correct already, a collision can most likely also occur in between frames and we need to calculate the position and velocity for that time. We can just simply extract the previous time of collision with the delta time and calculate the exact position and velocity at that point. And there we have it, a perfectly bouncing ball! Now I am not going to post a video about that, as it pretty much looks the same as the euler one, but try it out and see for yourself.

This pretty much sums it all up what I wanted to share and what I also had to make for my physics assignment, I hope it is useful for those who read it and if anything is not clear or plain wrong, leave a comment or mail me. Until next time where I will discuss Orbital motion!