{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Rag Dolls and Tapestries\n", "\n", "In this experiment, we will explore Object-Oriented Programming by examining Rag Dolls and Tapestries.\n", "\n", "In computer simulations, a rag doll is a stick figure. However, the stick figure is constructed in such a way that the joints can move independently, but the body parts remain connected. In fact, the same idea can be used for any item that we wish to simulate that has joints connecting points of mass. We'll see that rag dolls and tapestries can both be constructed from the same elements.\n", "\n", "To allow body parts to both move independently, and also remain connected, and to make curtains, we have to use some mathematics. To make it fast, we use a technique called _verlet integration_. \n", "\n", "## Points and Links" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This code is based on http://gamedevelopment.tutsplus.com/tutorials/simulate-tearable-cloth-and-ragdolls-with-simple-verlet-integration--gamedev-519\n", "\n", "We need to basic classes:\n", "\n", "1. PointMass - a point in 2D space, with some mass\n", "2. Link - a rigid connection between two PointMasses\n", "\n", "Out of these two classes, we can build all kinds of things, including:\n", "\n", "1. Circle\n", "1. Body (uses Circle for head)\n", "2. Tapestry - a curtain-like matrix composed of a mesh of PointMasses and Links\n", "\n", "This uses the same ideas from the bouncing ball examples, with some minor differences:\n", "\n", "* velocity will be computed based on last_position - current_position\n", "* we will take mass into account\n", "* objects can't move freely... they may have constraints due to their links to other PointMasses" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_1\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_1\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_1\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_1\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #1:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "