Flare3D 2.0 + Flare3D Physics overview

Some days ago the Flare3D official blog announced the beta of the Flare3D physics engine, which will allow us to create 3D physics environment without using 3rd party libraries such as JiglibFlash, and make games like CubesOut (based on this prototype) and A Small Car just using Flare3D libraries.

You can download the latest version of the beta from this page but you will need to publish for Flash Player 11 in order to make it work.

I suggest you to read this guide in order to add support for FP11 to your Flash IDE.

I have to say The MXP included did not add any HTML template named “Flash WMODE direct” so I had to set wmode to direct by myself in the HTML page.

You will also need to test your works in an HTML page since publishing directly in the IDE won’t work.

Mixing some code in the demos and adding the camera control I explained in Flash 3D Sokoban prototype with Flare3D I ended with this code:

package {
	import flare.basic.*;
	import flare.core.*;
	import flare.physics.core.PhysicsBox;
	import flare.physics.core.PhysicsPlane;
	import flare.physics.core.PhysicsSphere;
	import flare.physics.core.PhysicsSystemManager;
	import flare.physics.core.RigidBody;
	import flare.physics.vehicles.PhysicsVehicle;
	import flare.materials.Shader3D;
	import flare.materials.filters.ColorFilter;
	import flare.primitives.*;
	import flare.utils.*;
	import flare.system.*;
	import flare.loaders.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.*;
	public class Main extends Sprite {
		[Embed(source = "carscene.f3d", mimeType = "application/octet-stream")]
		private var embeddedScene:Class;
		private var scene:Scene3D;
		private var physics:PhysicsSystemManager;
		private var wheelFL:Pivot3D;
		private var wheelFR:Pivot3D;
		private var wheelRL:Pivot3D;
		private var wheelRR:Pivot3D;
		private var carBody:PhysicsVehicle;
		private var chassis:Pivot3D;
		public function Main():void {
			scene = new Viewer3D(this,"without_this_embed_wont_work.f3d");
			scene.addEventListener(Scene3D.COMPLETE_EVENT, completeEvent);
			physics=PhysicsSystemManager.getInstance();
			for (var i:int;i<20;i++){
				var boxMaterial:Shader3D=new Shader3D("",[new ColorFilter(0xc86464)]);
				var cube:Cube=new Cube("box",5,5,5,1,boxMaterial);
				cube.setPosition((Math.random()-0.5)*100,5,(Math.random()-0.5)*100);
				scene.addChild(cube);
				cube.addComponent(new PhysicsBox());
			}
			var controls:TextField=new TextField();
			controls.autoSize=TextFieldAutoSize.LEFT;
			controls.defaultTextFormat=new TextFormat("Arial",12,0xffffff);
			addChild(controls);
			controls.htmlText = "ARROWS KEY: control the car"+String.fromCharCode(13)+"SPACE: handbrake" + String.fromCharCode(13)+"F1: reset position";
			controls.y=stage.stageHeight-53;
			scene.pause();
		}
		private function processCar():void {
			chassis=scene.getChildByName("chassis");
			chassis.addComponent(new PhysicsBox());
			scene.getChildByName("Sphere01").addComponent(new PhysicsSphere());
			carBody=new PhysicsVehicle(chassis.components[0] as RigidBody,40,2.5,2000);
			carBody.chassis.mass=10;
			carBody.chassis.setPosition(chassis.x, chassis.y, chassis.z);
			var chassisPos:Vector3D=chassis.getPosition();
			wheelFL=scene.getChildByName("FL_tire");
			carBody.addWheel("1", wheelFL, true, true);
			wheelFR=scene.getChildByName("FR_tire");
			carBody.addWheel("2", wheelFR, true, true);
			wheelRL=scene.getChildByName("RL_tire");
			carBody.addWheel("3", wheelRL, false, true);
			wheelRR=scene.getChildByName("RR_tire");
			carBody.addWheel("4", wheelRR, false, true);
			physics.addVehicle(carBody);
		}
		private function completeEvent(e:Event):void {
			var planet:Flare3DLoader = new Flare3DLoader( embeddedScene );
			planet.parent = scene;
			planet.load();
			var floor:Pivot3D=scene.getChildByName("floor");
			var floorPhysics: RigidBody = new PhysicsPlane();
			floor.addComponent(floorPhysics);
			processCar();
			scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );
			scene.resume();
		}
		private function updateEvent(e:Event = null):void {
			carBody.setAccelerate(0);
			carBody.setSteer(0);
			carBody.setHBrake(false);
			if (Input3D.keyDown(Input3D.UP)) {
				carBody.setAccelerate(1);
			}
			if (Input3D.keyDown(Input3D.DOWN)) {
				carBody.setAccelerate(-0.5);
			}
			if (Input3D.keyDown(Input3D.LEFT)) {
				carBody.setSteer(-1);
			}
			if (Input3D.keyDown(Input3D.RIGHT)) {
				carBody.setSteer(1);
			}
			if (Input3D.keyDown(Input3D.SPACE)) {
				carBody.setHBrake(true);
			}

			if (Input3D.keyHit(Input3D.F1)) {
				carBody.chassis.setPosition(0, 2, 0);
				carBody.chassis.setOrientation(new Matrix3D());
				carBody.chassis.setActive(true);
			}
			physics.step();
			Pivot3DUtils.setPositionWithReference(scene.camera,0,10,-20,chassis,0.1);
			Pivot3DUtils.lookAtWithReference(scene.camera,0,0,10,chassis);
		}
	}
}

Which once executed gives this result:

And now you have your A Small Car prototype in just a few lines.

Download the source code with all required libraries and enjoy 3D physics

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

214 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
// 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