Tuesday, April 1, 2014

Havok Physics Engine Tutorial Series: Cloth

Before I start this tutorial, a short disclaimer. This is not the best way to model a cloth. Havok Physics SDK has a separate Havok Cloth package which is optimized for cloth simulation. I would recommend you to go and give it a try. The tutorial is showing one possible way to create cloth using distance constraint.
In this tutorial, I will show you how to create a simple cloth using the simple distance constraint which is wrapped into the hkpStiffSpringConstraint object in the Havok Physics SDK. This tutorial will be building on top of to the Simple Distance Constraint Tutorial , Picking Tutorial and Chain Tutorials that we did earlier. OK so lets get started.

Creating a cloth using simple distance constraint
A cloth can be modeled simply by using a set of masses linked with mass less springs. The springs can be approximated using hkpStiffSpringConstraint in Havok physics sdk. There are three types of springs: structural, shear and bending springs. Details of the cloth model are beyond the scope of this tutorial. Interested readers may drop in a comment and I will happily point you to relevant resources on cloth modeling. The first thing we need is a set of masses which we create using the following code snippet.

This is similar in spirit to the Multiple Bouncing Boxes Tutorial. The only difference is in the position of the boxes so that they lie in the XZ plane at a given Y value.

Setting up structural springs between the masses
OK once we lay down our masses, we can create a bunch of springs between them using the following code snippet. We basically iterate between each pair of rigid bodies and create a stiff spring constraint between them. First we iterate horizontally and then vertically.

I store the indices into a vector so that I would render the cloth springs easily in the OnRender function.

Rendering of Cloth
For this simple cloth model, I render the masses as boxes as was shown in the Multiple Boxes tutorial.

For springs, I iterate through the vector (indices), and create a line between each pair of indices as shown in the code below.

That's all, after compiling and building the given code, you will see a piece of cloth. The mass at each end of the top row are fixed to ensure that the cloth does not fall down. The rest of the boxes can be picked.

You can get the full source code from my github repo https://github.com/mmmovania/HavokPhysicsTutorials

Left click to rotate, left click and drag on a mass to pick and reposition
Middle click to zoom
Right click to pan

What's next:
In the next tutorial, I will show you how to create a basic vehicle.



Popular Posts

Copyright (C) 2011 - Movania Muhammad Mobeen. Awesome Inc. theme. Powered by Blogger.