, Jr. Member
OK- I think I figured it out
Now to make the fire look good and tweak the values…
Though now I realize that having boosters on the side makes it kindof pointless to have tilting, or vice-versa…
package
{
import flash.geom.Vector3D;
import away3d.containers.ObjectContainer3D;
import away3d.containers.Scene3D;
import away3d.entities.Mesh;
import away3d.materials.ColorMaterial;
import away3d.materials.lightpickers.StaticLightPicker;
import away3d.primitives.ConeGeometry;
import away3d.primitives.CubeGeometry;
import awayphysics.collision.shapes.AWPBoxShape;
import awayphysics.dynamics.AWPDynamicsWorld;
import awayphysics.dynamics.AWPRigidBody;
public class Ship extends AWPRigidBody
{
public static const FORWARD:String = 'forward';
public static const LEFT:String = 'left';
public static const RIGHT:String = 'right';
public static const BACK:String = 'back';
public static const UP:String = 'up';
public static const DOWN:String = 'down';
private var scene:Scene3D;
private var physicsWorld: AWPDynamicsWorld;
private var lightPicker:StaticLightPicker;
private var shipMesh:Mesh;
private var shipBody:AWPBoxShape;
private var shipGeometry:CubeGeometry;
private var fireContainer:ObjectContainer3D;
private var fireGeometry:ConeGeometry;
private var fireMeshPosition:Vector3D;
private var fireForce:Vector3D;
private var jetStrength:Number = 100;
private var tiltStrength:Number = 2;
private var fireMesh:Mesh;
private var jetDirection:String = BACK;
public function Ship(_scene:Scene3D, _lightPicker:StaticLightPicker, _physicsWorld:AWPDynamicsWorld)
{
scene = _scene;
lightPicker = _lightPicker;
physicsWorld = _physicsWorld;
createShip();
scene.addChild(shipMesh);
super(shipBody, shipMesh, 10);
physicsWorld.addRigidBody(this);
this.gravity = new Vector3D();
createFire();
}
private function createShip() {
var mat:ColorMaterial = new ColorMaterial(0x00FF00);
shipGeometry = new CubeGeometry(50,50,200);
mat.lightPicker = lightPicker;
shipMesh = new Mesh(shipGeometry, mat);
shipBody = new AWPBoxShape(shipGeometry.width, shipGeometry.height, shipGeometry.depth);
}
private function createFire() {
var mat:ColorMaterial = new ColorMaterial(0xFF0000);
mat.lightPicker = lightPicker;
fireGeometry = new ConeGeometry(25);
fireMesh = new Mesh(fireGeometry, mat);
fireMesh.rotationX = -90;
fireMesh.z = -fireGeometry.height/2;
fireContainer = new ObjectContainer3D();
fireContainer.addChild(fireMesh);
scene.addChild(fireContainer);
fireMeshPosition = new Vector3D();
fireForce = new Vector3D();
fireMesh.visible = false;
}
//Triggered via Starling container
public function everyFrame(passedTime:Number) {
if(fireMesh.visible) {
updateJetPosition();
}
}
private function updateJetPosition() {
var halfShipDepth:Number = shipGeometry.depth/2;
var halfShipWidth:Number = shipGeometry.width/2;
switch(jetDirection) {
case FORWARD: fireMeshPosition.copyFrom(shipMesh.backVector);
fireMeshPosition.scaleBy(halfShipDepth);
break;
case LEFT: fireMeshPosition.copyFrom(shipMesh.leftVector);
fireMeshPosition.scaleBy(halfShipWidth);
break;
case RIGHT: fireMeshPosition.copyFrom(shipMesh.rightVector);
fireMeshPosition.scaleBy(halfShipWidth);
break;
case BACK: fireMeshPosition.copyFrom(shipMesh.forwardVector);
fireMeshPosition.scaleBy(halfShipDepth);
break;
default: break;
}
fireMeshPosition.incrementBy(shipMesh.position);
fireContainer.position = fireMeshPosition;
fireContainer.lookAt(shipMesh.position);
}
public function jetsTo(dir:String) {
jetDirection = dir;
fireMesh.visible = true;
updateJetPosition();
switch(jetDirection) {
case FORWARD: fireForce.copyFrom(shipMesh.forwardVector);
break;
case LEFT: fireForce.copyFrom(shipMesh.rightVector);
break;
case RIGHT: fireForce.copyFrom(shipMesh.leftVector);
break;
case BACK: fireForce.copyFrom(shipMesh.backVector);
break;
default: break;
}
fireForce.scaleBy(jetStrength);
this.applyCentralForce(fireForce);
}
public function jetsOff(dir:String) {
fireMesh.visible = false;
}
//triggered external controller that listens for keypresses, etc.
public function tiltTo(dir:String) {
//Note- need to make this smoother, based on timestep in everyFrame, etc.
switch(dir) {
case UP: this.rotationX -= tiltStrength; break;
case DOWN: this.rotationX += tiltStrength; break;
case LEFT: this.rotationY -= tiltStrength; break;
case RIGHT: this.rotationY += tiltStrength; break;
}
}
}
}