Three Heavenly Angels

Three heavenly angels are watching over Barbara Sue Jordan. A woman who is strong, supportive, and my grandmother. Within the last 16 years, she has lost my grandfather, my step-grandfather, and my…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How a Piece of Cloth can get Students Interested in Programming

I was lucky in one respect, since the course in question, being geared toward programming for engineers, featured a significant unit on numerical methods. All these students had just learned how to write numerical ODE solvers, which lie at the core of a wealth of physical simulations. Are physical sims computationally intensive? Check. Do they produce flashy and impressive results? You betcha. Is it within the capabilities of these students to write one? Well, they’ll be able to understand the code if it’s given to them, at any rate.

I figured that a cloth simulation struck the perfect balance between complexity and spectacle. It’s effectively a solved problem at this point, with some implementations producing impressively realistic results (see below). So while it mightn’t have been the most revolutionary project, I knew that the problem was well-documented with plenty of supporting material, which was beneficial for a class demonstration. There are models of varying complexity, and the simpler ones are easy to follow without seeming trivial.

The model selected treats the cloth as a set of point masses connected by springs. The good thing here was that it could be explained in terms of processes that required only a high school level of physics knowledge, and it was possible, without changing the underlying model, to add additional dynamics that improve accuracy if so desired.

So at its core, the cloth is effectively a grid of particles, with each particle connected to its 8 immediate neighbours by springs like so:

How do you obtain the dynamics of a model like this? If we have the position of each particle at a given instant in time, we can assume that the position a very short time later will be roughly this position plus the instantaneous velocity times that very short period of time. Congratulations, you’ve just understood how an ODE solver works. But to follow this method, we need to obtain an estimate of the velocity of each particle by summing the forces acting on them. We divide by mass, and presto, there’s an acceleration we can use to obtain an estimate of the velocity. How good is Newton’s Second Law?

The forces we’d consider (at a minimum) to be acting on each particle are a weight force, and the 8 contributions from the 8 springs connected to it.

If we wanted to model wind or collisions or whatever, we could add that as well. But essentially, the evolution of the system involves computing, for each time instant, the net force acting on each vertex; from this determining the acceleration vector; and from that finding the velocity and position vectors.

The weight force is easy, that’s just a constant -9.8m in the z direction.

The springs are a little trickier. The force on a damped spring is resistant to both the amount it is currently stretched by, x, and its current instantaneous velocity, v. The formula is therefore:

F = -kx-cv

where k and c are constants.

There’s some trickiness here because the velocity in this case is the relative velocity between the two neighbouring particles. In any case, do that 8 times, once for each immediate neighbour, and sum the results to get the net force on the particle.

For output, I just printed the resulting data to the terminal, one line to a time instant. Remember that the point of this program is to emphasise the importance of the computation itself, and I think it does a good job of that. A lot of effort goes into producing the numbers that are produced as output, both computational resources and mental effort in designing the program.

Fantastic stuff. And here’s the thing, there really wasn’t much post-processing necessary to get that result. I want to be able to convince any student starting their computer science journey that something like this is within their grasp. The renderer isn’t the important part of producing that video, our program is, because without it, there’s nothing to render.

So how did this go down? Well, I showed it to students, and a lot were duly impressed, which is of course flattering and encouraging, but it’s not something I’m all that interested in. Because the real metric for success is whether or not they believed it was within their abilities to do the same, and I hope they did.

When you learn to program for the first time, you get this feeling that all your projects are just tiny parts of ‘real’ programs; or toy examples that don’t mean anything. So I think it’s increasingly important to keep students feeling like what they’re being taught is actually relevant in some way. By giving them examples of how their code might fit into some bigger picture, we empower them not just to continue studies in the field, but to start playing around with programming and perhaps end up making something incredible.

Add a comment

Related posts:

A History of Disruption in the Energy Industry

The long history of disruption and innovation in oil and gas. How will it respond to the inevitable changes ahead? This is the first in a series of posts by Ondiflo, a blockchain-based ticketing…

Convenient Methods to Rotate Axis Tick Labels in Matplotlib

In the previous 3 posts, essentials of data visualization with Matplotlib have been discussed. If you are interested in those posts, you can start to read from this article. Otherwise, just go ahead…

Know these tips and tricks before selecting an upcoming ICO

Cryptocurrency is expanding its roots around the world. Even a school student is investing in crypto by analyzing the market trends. The amount of crypto investment is increasing day after day. A…