Papervision with 3D Physics WOW engine (Getting started)

Papervision with WOW Engine

Today I decided to create this simple example that used Papervision and the 3D physics WOW Engine. It is actually quite simple. Basically all that happens is you setup your objects for papervision (5 minutes) - setup your objects for the wow engine (5 mintues) then when you render - you just attach the movements of the WOW objects to your Papervision objects. (5 more minutes or less and you're DONE)

So taking out 3D and Papervision all together here is what is happening. Let's say you setup an enterframe and then assign a movieclips x and y positions to your mouse x and y position. SAME Exact thing happening here. Except you are attaching the x y and z properties of a papervision object to a wow object. Pretty simple! I'll take corrections if I am wrong since I have spent all of 30 minutes playing around with it.

Here is a quick sample of what goes on when rendering:

Actionscript:
  1. sphere.x    = wowSphere.px;
  2. sphere.y    = -wowSphere.py;
  3. sphere.z    = wowSphere.pz;

Not too hard right.

Here is the ActionScript code for creating a simple sphere boucing on the stage. Source code is below. (Basically a modified example shown on the WOW site)

Main.as

Actionscript:
  1. package
  2. {
  3. import com.cs54.papervision.PV3DSceneWOW;
  4. import flash.display.Sprite;
  5.  
  6. public class Main extends Sprite
  7. {
  8. protected var papervisionSceneWOW:PV3DSceneWOW
  9.  
  10. public function Main():void
  11. {
  12. papervisionSceneWOW = new PV3DSceneWOW();
  13. papervisionSceneWOW.init(stage.stageWidth, stage.stageHeight)
  14. addChild(papervisionSceneWOW);
  15. }
  16. }
  17. }

PV3DSceneWOW.as:

Actionscript:
  1. package com.cs54.papervision
  2. {
  3.  
  4. import flash.events.Event;
  5. import fr.seraf.wow.core.data.WVector;
  6. import fr.seraf.wow.core.WOWEngine;
  7. import fr.seraf.wow.primitive.WBox;
  8. import fr.seraf.wow.primitive.WOWPlane;
  9. import fr.seraf.wow.primitive.WSphere;
  10. import org.papervision3d.materials.ColorMaterial;
  11. import org.papervision3d.materials.utils.MaterialsList;
  12. import org.papervision3d.materials.WireframeMaterial;
  13. import org.papervision3d.objects.DisplayObject3D;
  14. import org.papervision3d.objects.primitives.Plane;
  15. import org.papervision3d.objects.primitives.Sphere;
  16.  
  17. public class PV3DSceneWOW extends PaperBase
  18. {
  19.  
  20. protected var displayContainer    :DisplayObject3D;
  21. protected var holder            :DisplayObject3D
  22. protected var sphere            :Sphere;
  23. protected var wireframeMat        :WireframeMaterial;
  24. protected var wow                :WOWEngine
  25. protected var wowSphere            :WSphere
  26. override public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
  27. {
  28.  
  29. super.init(vpWidth, vpHeight);
  30. default_camera.y = 300;
  31. }
  32.  
  33. override protected function createChildren3D():void
  34. {
  35. super.createChildren3D();
  36.  
  37. wireframeMat                 = new WireframeMaterial(0, 100, .2);
  38. wireframeMat.doubleSided     = true;
  39. sphere                         = new Sphere(wireframeMat, 100);
  40. default_camera.zoom         = 40;
  41.  
  42. default_scene.addChild(sphere);
  43.  
  44. setupWow();
  45. createGround();
  46. createWOWSphere();
  47. }
  48.  
  49. public function setupWow(): void
  50. {
  51. //this is the physics engine
  52. wow=new WOWEngine(.3);
  53. wow.collisionResponseMode = wow.STANDARD;
  54.  
  55. //setup the gravity
  56. wow.addMasslessForce(new WVector(0,50,0));
  57. }
  58.  
  59. public function createGround(): void
  60. {
  61. //we create a ground on the physics engine
  62. var ground:WOWPlane             = new WOWPlane(0,0,0);
  63. ground.elasticity                =.35;
  64. ground.friction                    =2;
  65.  
  66. wow.addParticle(ground);
  67.  
  68. // we create the ground on the render engine so we start by setup the material...
  69. var material:WireframeMaterial    = new WireframeMaterial(0,100,1)
  70. material.doubleSided            = true
  71.  
  72. var plane:Plane                 = new Plane(material , 1000, 1000, 5, 5 );
  73. plane.rotationX                    =-90
  74.  
  75. default_scene.addChild(plane);
  76. }
  77.  
  78. public function createWOWSphere(): void
  79. {
  80. wowSphere = new WSphere(0,-900,100,100,false,0.1);
  81. wowSphere.elasticity = 0;
  82.  
  83. wowSphere.friction=3320;
  84. wow.addParticle(wowSphere);
  85.  
  86. sphere.x    =-wowSphere.px;
  87. sphere.y    =-wowSphere.py;
  88. sphere.z    =-wowSphere.pz;
  89. }
  90.  
  91. override protected function processFrame():void
  92. {
  93. super.processFrame();
  94.  
  95. wow.step()
  96.  
  97. sphere.x    = wowSphere.px;
  98. sphere.y    = -wowSphere.py;
  99. sphere.z    = wowSphere.pz;
  100.  
  101. }
  102. override protected function renderEnterFrame(ThisEvent:Event):void
  103. {
  104. super.renderEnterFrame(ThisEvent);
  105.  
  106. }
  107.  
  108. }
  109.  
  110. }

The paper base that I reuse:
Based off of this example

Actionscript:
  1. package  com.cs54.papervision
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import org.papervision3d.view.Viewport3D;
  6. import org.papervision3d.cameras.*;
  7. import org.papervision3d.scenes.Scene3D;
  8. import org.papervision3d.render.BasicRenderEngine;
  9.  
  10. public class PaperBase extends Sprite
  11. {
  12.  
  13. public var viewport:Viewport3D;
  14. public var renderer:BasicRenderEngine;
  15. public var default_scene:Scene3D;
  16. public var default_camera:Camera3D;
  17.  
  18. public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
  19. {
  20. initPapervision(vpWidth, vpHeight);
  21. createChildren3D();
  22. createChildren2D();
  23. size();
  24. initEvents();
  25. }
  26. protected function initPapervision(vpWidth:Number, vpHeight:Number):void
  27. {
  28. viewport = new Viewport3D(vpWidth, vpHeight);
  29. addChild(viewport);
  30.  
  31. renderer = new BasicRenderEngine();
  32.  
  33. default_scene = new Scene3D();
  34.  
  35. default_camera = new Camera3D();
  36. }
  37. protected function createChildren3D():void
  38. {
  39. // This function should hold all of the stages needed
  40. // to initialise everything used for papervision.
  41. // Models, materials, cameras etc.
  42. }
  43. protected function createChildren2D():void
  44. {
  45. // This function should create all of the 2d items
  46. // that will be overlayed on your papervision project.
  47. // User interfaces, Heads up displays etc.
  48. }
  49. protected function size():void
  50. {
  51.  
  52. }
  53. protected function initEvents():void
  54. {
  55. addEventListener(Event.ENTER_FRAME, renderEnterFrame);
  56. }
  57. protected function processFrame():void
  58. {
  59. // Process any movement or animation here.
  60. }
  61. protected function renderEnterFrame( ThisEvent:Event ):void
  62. {
  63. //trace("RENDERING")
  64. //We need to render the scene and update anything here.
  65.  
  66. renderer.renderScene(default_scene, default_camera, viewport);
  67. processFrame();
  68. }
  69. }
  70. }

SOURCE CODE

Don't forget to download the latest Papervision Great White Branch.

2 Responses to “Papervision with 3D Physics WOW engine (Getting started)”


Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word


Follow papervision2 on Twitter

RSS Feed