Questions about Assignment 1?
Questions about last week’s lab?
Otherwise, please sit next to your partner (or someone else if your partner isn’t here)!
Housekeeping…
We are going to make a ball fall and bounce off a table… with physics!
In a nutshell,◦ Get a ball to fall in the framework◦ Collision checking◦ Changing velocity◦ Energy Relationships
And for the over achievers…◦ Realistic animation
What’s the plan for today?
First of all, download the framework for today’s lab ( Resources in class website)
Take a look at the framework. Namely◦ The body class (body.h and body.cpp)◦ lab3.cpp (the driver)
Notice that the body class has◦ 3 vectors: position, velocity, acceleration◦ 2 scalars: mass, energy
So now let’s make the ball fall!
Framework
How do you update the position of the ball?◦ For now, we are going to assume the ball can only
move in the y-direction, so ignore x and z.◦ If you know the velocity and the delta time, you
can update the position!
How do we get delta time?◦ Since we have a clock class, and a clock called
“delta_time” in our driver, all we need to do is call the measure_reset() function We use measure_reset because delta time should be
reset after its value is read.
Exercise 1: Falling Ball (Integration)
Go into body.cpp◦ Edit the “update_position” function
Add the C++ equivalent of:y_position = y_position + ( y_velocity * delta_t )
Go into lab3.cpp◦ Notice the variable “sphere” and its parameters
For this exercise, we let the sphere have an initial velocity of -2.0
◦ Look at the DrawGLScene() function Notice the part commented “Sphere Physics” Under “Sphere Physics” use the global_time object and
update_position function to correctly update sphere’s postion.
Exercise 1
Our ball fell magnificently!◦ But it also fell through the table…
Since we never check for a collision between the ball and the table, there is no interaction between them
We need to fix that!
Success!.... Wait, what?!
Things to consider when doing collision◦ The position of your object (in this case, the ball)◦ It’s size ( radius for us )◦ The position of the object we want to check
You want to make the ball stop when it hits the table.◦ The table is situated at ( 0.0, -0.5, 0.0)◦ The radius of the ball is 0.5◦ Conveniently, this allows us to just check the
collision at 0.0 Go into body.cpp
◦ Modify the update_position function The ball should stop when it touches the table
Exercise 2a: Collision Checking
Our ball now stops at the table…◦ But something is still wrong
The ball is falling at constant speed – which would be OK if there was no acceleration.◦ Unfortunately for us, we have gravity
Exercise 2b: Velocity
So we need to update our velocity based on acceleration
Acceleration due to gravity is -9.81 m/s2
Examine the your change to update_position◦ It should be testing for y_position >=0
Add this same check to update_velocity◦ Inside the check, add the C++ equivalent of:
y_velocity = y_velocity + (delta_t * gravity) This should be the only statement inside of this check
right now.
At this point, our ball will accelerate until it touches the table.
Exercise 2b: Velocity
Next we will make the ball bounce. For now, we will just switch the sign of the y
velocity once the ball has hit the ground Go to body.cpp
◦ In update_velocity, add a check to switch the velocity sign. In other words: If y_position < 0, switch y_velocity signs
The ball now bounces up and down!
Exercise 3: Bouncing
We are still missing one more thing – when the ball hits the ground, it should not bounce up to the same height.
The ball starts off with a certain amount of potential energy. When it hits the ground, this energy is converted to both kinetic and thermal energy.
Thus we our ball will have a lower kinetic energy, and will not bounce as high!
Energy
First, you need to figure out the initial potential energy of the ball.◦ Ep = m*g*h
mass * 9.81 * height of the ball◦ Go to body.cpp, and calculate energy in the
constructor (since we know all our parameters at that point already!)
Next, we need to remove some things◦ Remove the if statement in update_position◦ Remove the if-else in update_velocity
Exercise 4: Implementing Energy
At this point, your update_velocity and update_position functions should have 1 line of code.
We need to know, however, when the ball has hit the table (or gone underneath it)◦ Before you change the y-position in
update_position, add a check for this and manually set the y-position to 0 (I’ll explain why we do this later).
Inside this check you’ve added, we can know calculate energy and our new velocity!
Exercise 4
To account for thermal energy, reduce the ball’s energy by 20%◦ energy *= 0.8
You have your energy and mass, therefore you can solve this equation for velocity◦ Ek = ½ m*v2
Math time! Solve the equation for v to get the equation you need.
◦ Now set your y-velocity to this new velocity Congratulations! You have successfully
implemented a (quasi) realistic bouncing ball!
Exercise 4
Manually setting the position of the ball to 0 is an incorrect way of animating what’s really occurring. The correct way is to detect the instant the ball hits the table, and subtract that from your recorded delta time.◦ animation_t = delta_t – collision_t
Then, you can animate the ball bouncing up for this amount of time, and this will be the “realistic” way of animating your ball.
Sidenotes