peter nitsch.net

peternitsch.net

Leveraging existing 2D code in Papervision3D

It's truly a testament to Papervision3D's ease-of-use that developers can leverage existing routines from previous 2D projects by simply modifying Movieclip references to DisplayObject3D. I put this little demo together with a set of stacked Mistubishi logos reacting to some slinky physics I built for a 2D AS2 project.

Spring physics demo in Papervision3D

Here's an excerpt from the frame code that reproduces the slinky effect. You'll probably recognize this as a fairly conventional method of producing the same result in 2D. The only major difference are the references to Decoration3D which is just a class I built to extend DisplayObject3D.

Actionscript:
  1. //Spin the stack around slowly 
  2. __rootNode.rotationY += 1;
  3.  
  4. for( var i:Number = 0; i<__bubbleArray.length; i++){
  5. var currentBubble:Decoration3D = __bubbleArray[i]; //Decoration3D extends DisplayObject3D
  6.  
  7. if( i == 0 ){
  8.   //Case of first logo in the stack
  9.   var dx:Number = currentBubble.x - __container.mouseX;
  10.   var dy:Number = currentBubble.y - __container.mouseY;
  11.   var angle:Number = Math.atan2(dy, dx);
  12.   var targetX : Number =  __container.mouseX + Math.cos(angle) * __springLengthValue;
  13.   var targetY : Number =  __container.mouseY + Math.sin(angle) * __springLengthValue;
  14. } else {
  15.   //Case of every logo following the first
  16.   var lastBubble:Decoration3D = __bubbleArray[i-1];   //Decoration3D extends DisplayObject3D
  17.  
  18.   var dx:Number = currentBubble.x - lastBubble.x;
  19.   var dy:Number = currentBubble.y - lastBubble.y;
  20.   var angle:Number = Math.atan2(dy, dx);
  21.   var targetX : Number =  lastBubble.x + Math.cos(angle) * __springLengthValue;
  22.   var targetY : Number =  lastBubble.y + Math.sin(angle) * __springLengthValue;
  23. }
  24.  
  25. currentBubble.vx += ( targetX - currentBubble.x ) * __springValue;
  26. currentBubble.vy += ( targetY - currentBubble.y ) * __springValue;
  27.  
  28. currentBubble.vy += __gravityValue;
  29. currentBubble.vx *= __frictionValue;
  30. currentBubble.vy *= __frictionValue;
  31. currentBubble.x += currentBubble.vx;
  32. currentBubble.y += currentBubble.vy;
  33.  
  34. currentBubble.rotationZ = currentBubble.vx * __frictionValue;
  35.  
  36. __scene.renderCamera( __camera );         
  37. }

Category: Papervision3D

Tagged:

Leave a Reply