Papervision3D: understanding Plane object – part 6

In part 5 we created a playing card.

Now it’s time to make it a bit more realistic. You know when you play again and again with playing cards, they bend.

We’ll learn how to bend our planes.

To bend objects in Papervision3D we need a 3rd party library called AS3Dmod.

AS3Dmod is a framework for creating static and animated modifier stacks, so you can modify the same object with more than one modifier.

So the steps to add a modifier to an object are:

1) Create a modifier stack and apply it to the object

2) Add a modifier to the stack

3) Configure the modifier

4) Repeat from step 2 if you want to add more than one modifier

5) Apply the queue

Now it’s time to download the library from the official Google Code page and copy the com folder in the same path you copied the org Papervision3D folder. Don’t worry if you can’t find it, at the end of the page I included the full source code with all needed libraries in the proper positions.

Now, the script we already saw at part 5 with the new lines commented.

Plese notice at lines 42 and 43 I added more segments to the planes, to have a smoother blend.

To understand what segments represent, read part 1

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.events.InteractiveScene3DEvent;
	// AS3Dmod libraries
	// The Modifier Stack is the base of AS3Dmod. 
	// Holds a reference to the mesh being modified and an array of modifiers.
	import com.as3dmod.ModifierStack;
	// Bend class allows to bend an object along an axis. 
	import com.as3dmod.modifiers.Bend;
	// this is the 3D engine inside AS3Dmod.
	import com.as3dmod.plugins.pv3d.LibraryPv3d;
	public class papervision extends Sprite {
		public var my_card:card = new card();
		public var viewport:Viewport3D=new Viewport3D(500,400,false,true);
		public var scene:Scene3D = new Scene3D();
		public var camera:Camera3D = new Camera3D();
		public var renderer:BasicRenderEngine = new BasicRenderEngine();
		public var front_material:MovieMaterial;
		public var back_material:MovieMaterial;
		public var front_plane:Plane;
		public var back_plane:Plane;
		public var rotation_speed=0;
		public var steps=0;
		// declaring modifiers stacks
		public var front_mod:ModifierStack;
		public var back_mod:ModifierStack;
		// creating bend modifiers
		public var front_bend:Bend = new Bend();
		public var back_bend:Bend = new Bend();
		public function papervision() {
			my_card.gotoAndStop(2);
			front_material=new MovieMaterial(my_card);
			my_card.gotoAndStop(1);
			back_material=new MovieMaterial(my_card);
			front_plane=new Plane(front_material,200,250,8,10);
			back_plane=new Plane(back_material,200,250,8,10);
			addChild(viewport);
			camera.focus=100;
			camera.zoom=10;
			back_plane.rotationY=180;
			front_material.interactive=true;
			back_material.interactive=true;
			// creating front and back modifiers and assigning
			// them to their respective planes
			front_mod = new ModifierStack(new LibraryPv3d(), front_plane);
			back_mod = new ModifierStack(new LibraryPv3d(), back_plane);
			// adding bends to modifier stacks
			front_mod.addModifier(front_bend);
			back_mod.addModifier(back_bend);
			// setting opposite forces 
			front_bend.force = 0.3;
			back_bend.force = -0.3;
			// applying the modifiers
			front_mod.apply();
			back_mod.apply();
			scene.addChild(front_plane);
			scene.addChild(back_plane);
			back_plane.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS,on_plane_clicked);
			front_plane.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS,on_plane_clicked);
			addEventListener(Event.ENTER_FRAME, render);
		}
		public function render(e:Event) {
			if (rotation_speed) {
				steps++;
				front_plane.yaw(rotation_speed);
				back_plane.yaw(rotation_speed);
			}
			if (steps==180/rotation_speed) {
				steps=0;
				rotation_speed=0;
			}
			renderer.renderScene(scene, camera, viewport);
		}
		public function on_plane_clicked(e:InteractiveScene3DEvent) {
			if (steps==0) {
				rotation_speed=4;
			}
		}
	}
}

And this is the result…

… the same as before, but now the card has a bend modifier on it. Click on the card to flip it.

Next time we’ll add some shadows and other good looking effects. Download the source code with all needed libraries.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers