Minecraft Player With Alternativa3d
I have been recently introduced to Minecraft and while i haven’t had too much time to play the game i have had a chance at mimicking some of the environment using alternativa3d. You can quite easily create the minecraft player by creating a series of boxes and applying a skin from the game to have a final result that looks something like this. The first is the default skin you get on minecraft and the second is a skin of a friend who plays the game. If you would like to use your own skin you can quite easily find it by going to http://s3.amazonaws.com/MinecraftSkins/char.png and replacing the word char with your username. Anyway on to the code, in order to create this you the alternativa3d 7.7.0 library which you can get here and then you can put together the code below or download the example files at the end of this post. The code consists of downloading the skin png file, cropping sections of the bitmap into texture materials and then applying it to boxes created using code. So there are no 3d models loaded or imported here its completely created in code.
package {
import alternativa.engine3d.core.Camera3D;
import alternativa.engine3d.core.Object3DContainer;
import alternativa.engine3d.containers.*;
import alternativa.engine3d.controllers.SimpleObjectController;
import alternativa.engine3d.core.View;
import alternativa.engine3d.materials.FillMaterial;
import alternativa.engine3d.materials.TextureMaterial;
import alternativa.engine3d.primitives.Box;
import alternativa.engine3d.core.Debug;
import alternativa.engine3d.core.MipMapping;
import alternativa.engine3d.core.Sorting;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.StageQuality;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.display.Loader;
import flash.net.URLRequest;
public class Character extends Sprite {
private var player:Object3DContainer = new Object3DContainer();
private var container:ConflictContainer = new ConflictContainer();
private var camera:Camera3D;
private var controller:SimpleObjectController;
private var headml:Object;
private var headEml:Object;
private var bodyml:Object;
private var armRml:Object;
private var armLml:Object;
private var legRml:Object;
private var legLml:Object;
private var drawSmooth:Boolean = true;
public function Character()
{
//stage settings
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.HIGH;
//container settings
container.resolveByAABB=true;
container.resolveByOOBB=true;
// Camera and view
camera = new Camera3D();
camera.view = new View(stage.stageWidth, stage.stageHeight);
addChild(camera.view);
addChild(camera.diagram);
// Initial position
camera.rotationX = -1.989675521850586;
camera.rotationY = -1.365318347268385e-8;
camera.rotationZ = -3.071779489517212;
camera.y = 47.222782135009766;
camera.z = 20.583066940307617;
camera.x = -2.5103461742401123;
container.addChild(camera);
// Camera controller
controller = new SimpleObjectController(stage, camera, 200, 3);
// Load Skin
var ldr:Loader = new Loader();
var req:URLRequest = new URLRequest("ShadowsDieAway.png");
ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoaderProgress);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
ldr.load(req);
// Listeners
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(Event.RESIZE, onResize);
// Initial Render
camera.render();
}
public function onLoaderProgress(e:ProgressEvent) {
trace(e.bytesLoaded, e.bytesTotal);
}
private function onComplete(e:Event):void
{
// Lets get our skin we loaded as bitmapdata
var ldr:Loader = Loader(e.target.loader);
var bd:BitmapData = new BitmapData(ldr.width, ldr.height, true, 0x000000);
bd.draw(ldr);
// Setup TextureMaterials for each body part
createPlayerMaterials(bd);
// Create Head
var head:Box = new Box(8, 8, 8, 2, 2, 2, false, false, headml.left, headml.right, headml.back, headml.front, headml.bottom, headml.top);
head.z = 8;
head.sorting = Sorting.DYNAMIC_BSP;
// Create Head Outer Box
var headE:Box = new Box(8.9, 8.9, 8.9, 2, 2, 2, false, false, headEml.left, headEml.right, headEml.back, headEml.front, headEml.bottom, headEml.top);
headE.z = 8;
headE.sorting = Sorting.DYNAMIC_BSP;
// Create Body
var body:Box = new Box(8, 4, 12, 4, 4, 4, false, false, bodyml.left, bodyml.right, bodyml.back, bodyml.front, bodyml.bottom, bodyml.top);
body.z = -2;
// Create Left Leg
var legL:Box = new Box(4, 4, 12, 2, 2, 2, false, false, legLml.left, legLml.right, legLml.back, legLml.front, legLml.bottom, legLml.top);
legL.z = -14;
legL.x = 2;
// Create Right Leg
var legR:Box = new Box(4, 4, 12, 2, 2, 2, false, false, legRml.left, legRml.right, legRml.back, legRml.front, legRml.bottom, legRml.top);
legR.z = -14;
legR.x = -2;
// Create Left Arm
var armL:Box = new Box(4, 4, 12, 2, 2, 2, false, false, armLml.left, armLml.right, armLml.back, armLml.front, armLml.bottom, armLml.top);
armL.z = -2;
armL.x = 6;
// Create Right Arm
var armR:Box = new Box(4, 4, 12, 4, 4, 4, false, false, armRml.left, armRml.right, armRml.back, armRml.front, armRml.bottom, armRml.top);
armR.z = -2;
armR.x = -6;
// Add all body parts to the player object
player.addChild(head);
player.addChild(headE);
player.addChild(body);
player.addChild(legL);
player.addChild(legR);
player.addChild(armL);
player.addChild(armR);
// Add player to the container
container.addChild(player);
}
private function onEnterFrame(e:Event):void {
// Width and height of view
camera.view.width = stage.stageWidth;
camera.view.height = stage.stageHeight;
// Rotation
player.rotationZ -= 0.05;
// controller
controller.update();
// Render
camera.render();
}
private function createPlayerMaterials(src:BitmapData):void
{
// Texture material settings,
// you should probably extend TextureMaterial Rather than do this but meh
var repeat:Boolean = false;
var smooth:Boolean = false;
var mipMapping:int = 0;
var resolution:Number = 1;
// HEAD texture materials
var front_head:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(8, 8), 8, 8),repeat,smooth,mipMapping,resolution);
var back_head:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(24, 8), 8, 8),repeat,smooth,mipMapping,resolution);
var left_head:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(16, 8), 8, 8),repeat,smooth,mipMapping,resolution);
var right_head:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(0, 8), 8, 8),repeat,smooth,mipMapping,resolution);
var top_head:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(8, 0), 8, 8),repeat,smooth,mipMapping,resolution);
var bottom_head:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(16, 0), 8, 8),repeat,smooth,mipMapping,resolution);
// add materials to global object for easy access
headml = new Object();
headml.front = front_head;
headml.back = back_head;
headml.left = left_head;
headml.right = right_head;
headml.top = top_head;
headml.bottom = bottom_head;
// HEADE texture materials
var front_heade:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(40, 8), 8, 8),repeat,smooth,mipMapping,resolution);
var back_heade:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(56, 8), 8, 8),repeat,smooth,mipMapping,resolution);
var left_heade:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(48, 8), 8, 8),repeat,smooth,mipMapping,resolution);
var right_heade:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(32, 8), 8, 8),repeat,smooth,mipMapping,resolution);
var top_heade:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(40, 0), 8, 8),repeat,smooth,mipMapping,resolution);
var bottom_heade:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(48, 0), 8, 8),repeat,smooth,mipMapping,resolution);
// add materials to global object for easy access
headEml = new Object();
headEml.front = front_heade;
headEml.back = back_heade;
headEml.left = left_heade;
headEml.right = right_heade;
headEml.top = top_heade;
headEml.bottom = bottom_heade;
// BODY texture materials
var front_body:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(20, 20), 8, 12),repeat,smooth,mipMapping,resolution);
var back_body:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(32, 20), 8, 12),repeat,smooth,mipMapping,resolution);
var left_body:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(29, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var right_body:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(16, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var top_body:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(20, 16), 8, 4),repeat,smooth,mipMapping,resolution);
var bottom_body:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(28, 16), 8, 4),repeat,smooth,mipMapping,resolution);
// add materials to global object for easy access
bodyml = new Object();
bodyml.front = front_body;
bodyml.back = back_body;
bodyml.left = left_body;
bodyml.right = right_body;
bodyml.top = top_body;
bodyml.bottom = bottom_body;
// LEGR texture materials
var front_legR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(4, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var back_legR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(12, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var left_legR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(0, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var right_legR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(0, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var top_legR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(4, 16), 4, 4),repeat,smooth,mipMapping,resolution);
var bottom_legR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(8, 16), 4, 4),repeat,smooth,mipMapping,resolution);
// add materials to global object for easy access
legRml = new Object();
legRml.front = front_legR;
legRml.back = back_legR;
legRml.left = left_legR;
legRml.right = right_legR;
legRml.top = top_legR;
legRml.bottom = bottom_legR;
// LEGL texture materials
var front_legL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(4, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var back_legL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(12, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var left_legL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(8, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var right_legL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(0, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var top_legL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(4, 16), 4, 4),repeat,smooth,mipMapping,resolution);
var bottom_legL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(8, 16), 4, 4),repeat,smooth,mipMapping,resolution);
// add materials to global object for easy access
legLml = new Object();
legLml.front = front_legL;
legLml.back = back_legL;
legLml.left = left_legL;
legLml.right = right_legL;
legLml.top = top_legL;
legLml.bottom = bottom_legL;
// ARML texture materials
var front_armL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(44, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var back_armL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(52, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var left_armL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(48, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var right_armL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(40, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var top_armL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(44, 16), 4, 4),repeat,smooth,mipMapping,resolution);
var bottom_armL:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(48, 16), 4, 4),repeat,smooth,mipMapping,resolution);
// add materials to global object for easy access
armLml = new Object();
armLml.front = front_armL;
armLml.back = back_armL;
armLml.left = left_armL;
armLml.right = right_armL;
armLml.top = top_armL;
armLml.bottom = bottom_armL;
// ARMR texture materials
var front_armR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(44, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var back_armR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(52, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var left_armR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(40, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var right_armR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(48, 20), 4, 12),repeat,smooth,mipMapping,resolution);
var top_armR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(44, 16), 4, 4),repeat,smooth,mipMapping,resolution);
var bottom_armR:TextureMaterial = new TextureMaterial(cropBitmapData(src, new Point(48, 16), 4, 4),repeat,smooth,mipMapping,resolution);
// add materials to global object for easy access
armRml = new Object();
armRml.front = front_armR;
armRml.back = back_armR;
armRml.left = left_armR;
armRml.right = right_armR;
armRml.top = top_armR;
armRml.bottom = bottom_armR;
}
private function cropBitmapData(sourceBitmapData:BitmapData, startPoint:Point, width:Number, height:Number):BitmapData
{
var croppedBD:BitmapData = new BitmapData(width, height, true);
croppedBD.copyPixels(sourceBitmapData, new Rectangle(startPoint.x, startPoint.y, width, height), new Point(0, 0));
return croppedBD.clone();
croppedBD.dispose();
}
private function onResize(e:Event = null):void {
camera.view.width = stage.stageWidth;
camera.view.height = stage.stageHeight;
}
}
}
View the Demo Here Download The Files Here - includes flex and flash examples
Comments
Comments are currently closed