Papervision3D Pendulums NKU Seminar #3
This is the 3rd in a series of free public seminars held at NKU on Papervision3D and CS4. In this seminar, we take advantage of a number of open source references (given at the end of this post): MIT Open Courseware, Wikipedia, and Wolfram.
(Also in this seminar, Cocomo is tested.)
Please download Flash Player 10 to View all Examples
http://get.adobe.com/flashplayer
Intro
This seminar treats four pendulum types in Papervision3D: Simple , Double , Torsion, and Foucault. In addition, numerical techniques such as Runge-Kutta are discussed and source code demonstrated.

4 Pendulums
YouTube
Part 1: Simple & Double
Part 2: Torsion & Foucault
Discussion
In each topic, we introduce an important coding concept. By the end of this seminar you should have a knowledge of basic pendulum types, open source resources, encapsulation, pinning, Runge-Kutta, sliders, and environment maps.
A. Simple Pendulum: Encapsulation
Encapsulation is what really separates men from mice when it come to programming in 3D Flash. Dehash on his Blog post a series of Papervision3D examples: http://www.dehash.com/?page_id=212
The example double plane demonstrates how to properly encapsulate elements in Papervision3D. I’ve extended his example to make the planes oscillated: Check it out here (and included in the source code).
Basically, the overall code is separated into two classes. The main class runs BasicView and is in charge of providing length, pinning, and animation information. The second class holds the elements to be animated.

Two Classes
It’s brilliant (wish I could take credit) and provides the foundations for all types of coolness to be published in future blog posts.
Once the secondary class is imported using
import objects.*;
then you can access all the public classes which let you manipulate the instantiated objects from the sub class, as described in my book.
Of course, the super huge advantage here is that my pendulum elements can be substantiated and pinned as often as needed and used by any number of programs.
B. Double Pendulum: Pinning
The example below demonstrates the importance of pinning. Pinning lets you attach one segment to another.
The two elements above were pinned together using:
myPendulum1.rotationZ = Osc;
myPendulum2.rotationZ = Osc*1.721;
myPendulum2.x = myPendulum1.getPin(Osc).x;
myPendulum2.y = myPendulum1.getPin(Osc).y;
where the getPin(Osc) function gives the end point of the first segment and starting point of the second. Its code for the 2D and 3D cases are:
2D Case
public function getPin(myRot:Number):Point
{
var angle:Number = myRot* Math.PI / 180;
var xPos:Number = this.x + Math.sin(angle) * myLength;
var yPos:Number = this.y – Math.cos(angle) * myLength;
return new Point(xPos, yPos);
}
3D pseudo Code (still needs work)
public function getPin(myTheta:Number, myPhi:Number):Vertex3D
{
var angle1:Number = myTheta* Math.PI / 180;
var angle2:Number = myPhi* Math.PI / 180;
var xPos:Number = this.x + Math.cos(angle1) * Math.sin(angle2)*myLength;
var yPos:Number = this.y + Math.cos(angle2) * myLength;
var zPos:Number = this.z + Math.sin(angle1) * Math.sin(angle2)*myLength;
return new Vertex3D(xPos, yPos, zPos);
}
In both cases, pinning is just a trig-calculation of segment ending points.
This example demonstrates one of the normal modes of the double pendulum: with the second pendulum overextending to demonstrate the second degree of freedom. To properly handle this problem one needs to use numerical analysis (such as Runge-Kutta).
C. Numerical Calculations: Runge-Kutta
Though beyond the scope of this seminar, we have include the Flash code of Runge-Kutta 2 (midpoint), and 4 in your source files. This code was developed by Keith Peters in his book Advanced ActionScript 3.0 Animation. It’s a good starting point and will be used in seminars to come. The code is included in the objects folder of your download and is labelled RK2, and RK4.
Keith Peter’s Demo (bouncing ball RK4)
D. Torsion: Sliders
Here we describe the use of sliders. Sliders let you interact with your animations: they add the thrill of interaction. But there is a fundamental decision to be made when using such elements. Should you use Flash components, Flex components, or make your own?
In this seminar, we use Flex components. And since Flex is free for educators, it’s the logical choice (for this seminar). And in addition, by creating a MXML applications your can just drag those components onto the screen where they are supposed to go.
Here’s the steps:
- Drag your Flex component to the stage and position it
- Use the Flex Properties Panel to assign min, max, tick, etc…
- Incorporate your slider using the following code
- In the HSlider mx tag (which was automatically generated when you dragged the slider to the stage) place a change event: change=”sliderChangeLive2(event);”
<mx:HSlider change=”sliderChangeLive2(event);” x=”10″ y=”67″ value=”6″ maximum=”40″ minimum=”1″ liveDragging=”false” id=”length” enabled=”true” snapInterval=”1″/
- Create the sliderChangeLive2 function
private function sliderChangeLive2(event:SliderEvent):void {
var currentSlider2:Slider=Slider(event.currentTarget);
mySpConst=currentSlider2.value;
}
- Place the mySpConst which is generated by your slider into your equations of motion which are located in your animation loop
var Osc1:Number=80*Math.sin(incre*(Math.sqrt(mySpConst)/20));
E. Foucault: Environment Map
Placing an Environment Map on your primitive adds realism to your object as can be shown below. An Environment Map is a Papervision3D shader material, and a great blog to learn more about shader is Jim Foley’s blog MAD VERTICES. He does a super job with primitives, shaders, and more…and has code, video, and demos.
As mentioned earlier an EnvMapMaterial is a shader material. A shader material is a material that uses a light object to determine how it displays. The EnvMapMaterial accepts 4 parameters, the light object, light map bitmapdata, back environment bitmapdata and the ambient light. The EnvMapMaterial displays as though your object is a mirror that is reflecting its environment.
environment = new EnvMapMaterial(light, envMap, envMap, 0×555555)
F. Application: Butterflies
Seb Lee-Delise on his blog http://www.sebleedelisle.com/ gives an example of a butterfly whose wings oscillate like a simple pendulum
http://clockmaker.jp/labs/as3_pv3d_gw_butterfly/reflection.swf
Essentially, two planes are created and positioned with a sine oscillation. The code required to do this is given below:
var leftWing:DisplayObject3D;
var rightWing:DisplayObject3D;
var butterfly:* = new DisplayObject3D();
leftWing = new DisplayObject3D();
rightWing = new DisplayObject3D();
var mat:* = new BitmapFileMaterial(“butterfly_wind.png”);
mat.doubleSided = true;
var leftWingPlane:* = new Plane(mat, 200, 200, 1, 1);
var rightWingPlane:* = new Plane(mat, 200, 200, 1, 1);
leftWingPlane.scaleX = -1;
leftWingPlane.x = -100;
rightWingPlane.x = 100;
leftWing.addChild(leftWingPlane);
rightWing.addChild(rightWingPlane);
butterfly.addChild(leftWing);
butterfly.addChild(rightWing);
addEventListener(Event.ENTER_FRAME,
function (EffectEvent:Event) : void
{
leftWing.rotationY = Math.sin(getTimer() / 200) * 60;
rightWing.rotationY = Math.sin(getTimer() / 200) * -60;
return;
);
To improve the code one would need to use encapsulation and pinning. This is treated in the book.
G. Speeding Things Up
In the book, we show you how to build these pendulums in CS4, which speeds things up by reducing the overhead of Papervision3D.
Resources
The availability of knowledge provided by the internet is changing the face of education. No longer do we need to leave home to gain excess to the reservoirs of knowledge once cloistered in ivory towers. But now at our finger tips, with a simple click ,we gain excess to more than we could ever retain in a lifetime of study.
It’s changing the way we view education and interact with knowledge. From the “small to large” to the “large to small”, knowledge is in an overwhelming abundance and we must now pare it down and interact with what’s pertinent to our assigned task; which really makes the case for 3D data visualization
Simple Pendulum
http://ocw.mit.edu/OcwWeb/web/home/home/index.htm
http://scienceworld.wolfram.com/physics/
http://scienceworld.wolfram.com/physics/Pendulum.html
http://scienceworld.wolfram.com/physics/PendulumSmallOscillations.html
Double Pendulum
http://scienceworld.wolfram.com/physics/DoublePendulum.htm
Torsional Pendulum
http://scienceworld.wolfram.com/physics/TorsionalPendulum.html
Foucault Pendulum
http://en.wikipedia.org/wiki/Foucault_pendulum




Posted by Mike Lively