-
Papervision with collision detection
Posted on April 8th, 2009 12 commentsThis movie requires Flash Player 10
This is my version of Papervision collision detection. The project actually started out as a 2D isometric engine, in the end I decided to map the 2D grid to a 3D environment.
The collision detection is also handled by the isometric engine, left-click on the upper-left framerate box to show or hide the 2D grid.Use the numpad arrows or W-S-A-D to move around and click-drag the mouse to look around.
It might take some time to load all the textures, so be patient, I didn’t implement a loader!The source can be downloaded here.
As mentioned above, this is NOT a full fledged fps engine. My initial idea was to have a pseudo 2D-3D world, where the camera would be positioned above the player (as in Diablo II for example).
This means that collision detection is only applied on an XY-axis and not the expected XYZ-axis. (eg a player can jump through a ceiling, but cannot traverse a solid wall).The world is actually tile-based with performance in mind. Every keypress that moves the player will calculate if the player is allowed to move in a certain direction, if the test fails, a mini path findinding script will return the best non-colliding position closest to the requested position.
The final version would ideally combine papervision 3D objects with static 2D objects and feature a diablo-like top down camera position. Unfortunately, I almost never finish projects I start in my spare time :-/
I do still plan to create a very basic world editor to render a demo which is a lil’ more interesting to explore.Below is an excerpt of the main setup script :
-
// The container used to render the 3D in
-
_appContainer = appContainer;
-
// The isometric display layer
-
_canvas2D = new UIComponent();
-
// The papervision display layer
-
_canvas3D = new Basic3DSetup(500, 400);
-
_canvas3D.addEventListener(Event.INIT, onEngine3DInit);
-
// Create a new world grid, the grid is infinite in size,
-
// the 2 constructor parameters define the width and height of a single grid tile
-
// the smaller the size, the better the collision detection, yet the slower the algorithm
-
_grid = new world.grid.Grid();
-
_grid.create(10, 10);
-
// Initialize the player pawn(s) container
-
_pawnList = new Vector.<Pawn>;
-
// Load and parse a compatible XML file to a papervision 3D world
-
_f3dParser = new F3DParser(_grid.tileWidth, _grid.tileHeight);
-
_f3dParser.addEventListener(F3DParserEvent.PARSER_COMPLETE, onParserComplete);
-
_f3dParser.loadAndParse("http://www.frankula.com/lab/swf/museum.xml");
-
// Initialize the player
-
// passing PawnType.TERRAIN_BLOCK tells the engine that this pawn is solid and
-
// thus collides with other solid objects.
-
_playerPawn = new Pawn(PawnType.TERRAIN_BLOCK);
-
_playerPawn.playerControlled = true;
-
// Set the display object on which to render on (2D)
-
_playerPawn.canvas = _canvas2D;
-
_playerPawn.grid = _grid;
-
// Define the collision area of this Pawn, in this case
-
// the player is represented as a 50×50 px circle on the grid.
-
// the actual player model may be smaller or larger, the cirlce is only
-
// used for collision detection
-
_playerPawn.shapeClass = Circle;
-
_playerPawn.setSize(50, 50);
-
_playerPawn.updateShape();
-
_grid.updateTilesForPawn(_playerPawn);
-
_playerPawn.draw();
-
_pawnList.push(_playerPawn);
-
_canvas2D.visible = false;
-
_canvas2D.x = 300;
-
_canvas2D.y = 100;
-
_canvas2D.scaleX = _canvas2D.scaleY = .5;
-
_fpsTicker = new UITextField();
-
_fpsTicker.background = true;
-
_fpsTicker.backgroundColor = 0xffffff;
-
_fpsTicker.autoSize = TextFieldAutoSize.LEFT;
-
_fpsTicker.text = "LOADING…";
-
_canvas3D.mouseEnabled = false;
-
_canvas2D.mouseEnabled = false;
-
//_fpsTicker.mouseEnabled = _fpsTicker.mouseWheelEnabled = false;
-
_appContainer.addChild(_canvas3D);
-
_appContainer.addChild(_canvas2D);
-
_appContainer.addChild(_fpsTicker);
-
_framesRendered = 0;
-
_fpsTimer = new Timer(1000);
-
_fpsTimer.addEventListener(TimerEvent.TIMER, showFPS);
-
_fpsTimer.start();
-
_fpsTicker.addEventListener(MouseEvent.CLICK, onToggle2D);
-


