Humvee in Away3DLite (and PV3D) wheels and steering

Intro

In chapter 5 of my book, I discuss how to make a car’s wheels move forwards and backwards and steer. It is actually pretty easy to do in PV3D. But in Away3DLite it is not so obvious. So in the video below I discuss the difference and show you how to get wheel movement and steering working for both PV3D and Awa3DLite.

Video

Driving a Humvee in Away3dLite

Try It Out

Driving a Humvee

Driving a Humvee in Away3DLite

Code

To see the code click the link below:

<?xml version=”1.0″ encoding=”utf-8″?>
<s:Application xmlns:fx=”http://ns.adobe.com/mxml/2009&#8243;
xmlns:s=”library://ns.adobe.com/flex/spark”
xmlns:mx=”library://ns.adobe.com/flex/mx”
xmlns:components=”components.*”
minWidth=”955″ minHeight=”600″
creationComplete=”initApp()”
backgroundColor=”0×000000″
xmlns:colordataxml=”services.colordataxml.*”
xmlns:myvectorpath=”services.myvectorpath.*”
xmlns:valueObjects=”valueObjects.*”>

<fx:Script>
<![CDATA[
import away3dlite.cameras.*;
import away3dlite.containers.*;
import away3dlite.containers.ObjectContainer3D;
import away3dlite.core.utils.Cast;
import away3dlite.lights.DirectionalLight3D;
import away3dlite.loaders.*;
import away3dlite.loaders.data.GeometryData;
import away3dlite.loaders.utils.GeometryLibrary;
import away3dlite.materials.*;
import away3dlite.primitives.*;
import away3dlite.core.base.Mesh;
import away3dlite.loaders.data.ObjectData;

import flash.display.*;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.*;
import flash.geom.*;

import flash.ui.Keyboard;
import flash.utils.ByteArray;
import flash.text.TextField;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.managers.SystemManager;

import spark.primitives.*;

private var frameCollada:Collada;
private var doorCollada:Collada;
private var collada:Collada;
private var braketCollada:Collada;
private var glassCollada:Collada;

private var group:ObjectContainer3D;

//texture for truck
[Embed(source="HMMWV/images/Hmv2a.jpg")]
private var HMMWVTruckMap:Class;
//texture for truck
[Embed(source="HMMWV/images/HVTread2.jpg")]
private var HMMWVTreadMap:Class;
//texture for truck
[Embed(source="HMMWV/images/HVWheel.jpg")]
private var HMMWVWheelMap:Class;

//collada file for jet
[Embed(source="HMMWV/hmmwv.DAE",mimeType="application/octet-stream")]
private var MyHMMWV:Class;

private var myCount:int =0;
private var scene:Scene3D;
private var camera:HoverCamera3D;
private var view:View3D;
private var light:DirectionalLight3D;

private var boxWidth:Number = 250;
private var boxHeight:Number = 30;
private var boxDepth:Number = 250;
private var boxThickness:Number = 10;

private var renderNow:Boolean = true;
private var myDiceScale:Number = 30;
private var myDataArray:ArrayCollection = new ArrayCollection();

//material variables
private var material:BitmapMaterial;
private var materialA:BitmapMaterial;
private var materialB:BitmapMaterial;
private var materialC:BitmapMaterial;

private var topSpeed:Number = 0;
private var topSteer:Number = 0;
private var speed:Number = 0;
private var steer:Number = 0;

private var keyRight:Boolean = false;
private var keyLeft:Boolean = false;
private var keyForward:Boolean = false;
private var keyReverse:Boolean = false;

//objectvariables
private var model1:ObjectContainer3D = new ObjectContainer3D;

private function initApp():void{

initaway3dlite();
initMaterials();
createModel();
initListeners();
myPos.visible = false;

}

//Materials
private function initMaterials():void
{

materialA= new BitmapMaterial(Cast.bitmap(HMMWVTruckMap));
materialB = new BitmapMaterial(Cast.bitmap(HMMWVTreadMap));
materialC = new BitmapMaterial(Cast.bitmap(HMMWVWheelMap));

}

private function initaway3dlite():void {
scene = new Scene3D();
camera = new HoverCamera3D();
camera.distance = 600;
camera.zoom = 4;
//camera.x = systemManager.stage.stageWidth/2;

view=new View3D(scene,camera);
view.x=systemManager.stage.stageWidth/2;
view.y=systemManager.stage.stageHeight/2;
mySprite.addChild(view);
}

//Create your model
private function createModel():void {

collada = new Collada();
collada.scaling = 30;
model1 = collada.parseGeometry(MyHMMWV) as ObjectContainer3D;
model1.materialLibrary.getMaterial(“Humvee-material”).material = materialA;
model1.materialLibrary.getMaterial(“HVWheel-material”).material = materialC;
model1.materialLibrary.getMaterial(“HvTread-material”).material = materialB;
//Initiate position of model
myInitialPos();
scene.addChild(model1);

}

//Initiate position
private function myInitialPos():void{

model1.rotationY = 90;
model1.x=200;
model1.y=model1.z=100;

}

//////////////////////////////////////////////////////////////
//  Car Mesh Stuff
/////////////////////////////////////////////////////////////

//Method not used in this application
private function rollWheels(myRot:Number) : void
{
//FL…1
//RL…2
//FR…3
//RR…4

this.getObject(1).rotationY -=  myRot;
this.getObject(2).rotationY -=  myRot;
this.getObject(3).rotationY +=  myRot;
this.getObject(4).rotationY +=  myRot;

}

private function getObject(myWheel:Number) : Mesh

{
return Mesh(ObjectContainer3D(this.model1.getChildAt(myWheel)).children[0]);
}
//Listeners
private function initListeners():void {
systemManager.stage.addEventListener(Event.ENTER_FRAME, render);
systemManager.stage.addEventListener( KeyboardEvent.KEY_DOWN, mykeyDownHandler );
systemManager.stage.addEventListener( KeyboardEvent.KEY_UP, mykeyUpHandler );

}
//Animation Loop
private function render( e:Event ):void {

driveCar();
updateCar();
view.render();
camera.hover();

myText.text =”Coordinates”+”\n”+”x: “+model1.x+”\n”+”y: “+model1.y+”\n”+”z: “+model1.z+”\n”;

}

/////////////////////////////////////////////////////
// Driving Car Methods
////////////////////////////////////////////////////

private function updateCar():void
{

var roll :Number = speed*.2;

if(Math.abs(steer)<60){
//Negatives where a hack since model had issues
this.getObject(1).rotationZ =  steer;
this.getObject(3).rotationZ =  -steer;
}else{
this.getObject(1).rotationZ =  60;
this.getObject(3).rotationZ =  -60;
}

//Negagives where a hack since the Model had issues

rollWheels(roll);

// Steer car
model1.rotationY+= speed * steer/500  ;
model1.y=model1.z;

model1.x+=1.4*speed*Math.sin(3.1415*(model1.rotationY)/180)/20;
model1.z+=1.4*speed*Math.cos(3.1415*(model1.rotationY)/180)/100;

if(model1.z<0){model1.z=0;}
}

private function driveCar():void
{
// Speed
if( keyForward )
{
topSpeed = 55;
speed +=  10;
}
else if( keyReverse )
{
topSpeed = -55;
}
else
{
topSpeed = 0;
}

speed -= ( speed – topSpeed ) / 10;

// Steer
if( keyRight )
{
if( topSteer < 35 )
{
topSteer += 1.5;
speed +=  10;
}
}
else if( keyLeft )
{
if( topSteer > -35 )
{
topSteer -= 1.5;
speed +=  10;
}
}
else
{
topSteer -= topSteer / 24;
}

steer -= ( steer – topSteer ) / 2;
}

private function mykeyDownHandler( event :KeyboardEvent ):void
{
switch( event.keyCode )
{
case “W”.charCodeAt():
case Keyboard.UP:
keyForward = true;
keyReverse = false;
break;

case “S”.charCodeAt():
case Keyboard.DOWN:
keyReverse = true;
keyForward = false;
break;

case “A”.charCodeAt():
case Keyboard.LEFT:
keyLeft = true;
keyRight = false;
break;

case “D”.charCodeAt():
case Keyboard.RIGHT:
keyRight = true;
keyLeft = false;
break;
}
}

private function mykeyUpHandler( event :KeyboardEvent ):void
{
switch( event.keyCode )
{
case “W”.charCodeAt():
case Keyboard.UP:
keyForward = false;
break;

case “S”.charCodeAt():
case Keyboard.DOWN:
keyReverse = false;
break;

case “A”.charCodeAt():
case Keyboard.LEFT:
keyLeft = false;
break;

case “D”.charCodeAt():
case Keyboard.RIGHT:
keyRight = false;
break;
}
}

]]>
</fx:Script>

<s:BitmapImage source=”@Embed(‘assets/images/myBackground.jpg’)” width=”100%” height=”100%” />

<s:SpriteVisualElement id=”mySprite” width=”19″  height=”18″ x=”233″ y=”86″/>
<s:TextArea x=”10″ y=”3″ id=”myText” contentBackgroundAlpha=”0.8″/>
<s:Button id=”myBtn” click=”{myBtn.visible = false}; myPos.visible = true;” label=”Click to Drive – Use Arrow Keys” width=”231″ height=”170″ horizontalCenter=”0″ verticalCenter=”-100″/>
<s:Button x=”233″ y=”10″ label=”Reset Position” width=”113″ height=”44″ id=”myPos”  click=”myInitialPos()”  />
</s:Application>

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: