forked from: forked from: 3d Racing Game Test
Added turning, you can now freely move around the track
Arrow Keys to move around
test using boxes to render a 3d pathway
inspired by ABA's UpDownRoad
// forked from runouw's forked from: 3d Racing Game Test
// forked from runouw's 3d Racing Game Test
// Added turning, you can now freely move around the track
// Arrow Keys to move around
// test using boxes to render a 3d pathway
// inspired by ABA's UpDownRoad
package {
import flash.display.*
import flash.events.*;
import flash.text.*;
[SWF(width="465",height="465",backgroundColor="0xFFFFFF",frameRate="45")]
public class AS3Code extends Sprite{
public function AS3Code() {
main=this;
Width = stage.stageWidth;
Height = stage.stageHeight;
stage.addChild(main);
debugText = new TextField();
debugText.width = 400;
debugText.height = 300;
debugText.multiline = true;
debugText.wordWrap = true;
debugText.textColor = 0x0000FF;
debugText.selectable = false;
_rotate = new MovieClip();
stage.addChild(_rotate);
_rotate.x = 400;
_rotate.y = 400;
clip = new MovieClip();
_rotate.addChild(clip);
clip2 = new MovieClip();
stage.addChild(clip2);
clip3 = new MovieClip();
stage.addChild(clip3);
clip.x = -400;
clip.y = -400;
//clip2.x = -200;
//clip2.y = -150;
//clip3.x = -200;
//clip3.y = -150;
stage.addChild(debugText);
initialize();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
addEventListener(Event.ENTER_FRAME, EveryFrame);
}
}
}
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.utils.*;
import flash.filters.*
import flash.text.*;
import flash.geom.*
import flash.media.*
import flash.net.*
//
import sandy.core.*;
import sandy.core.data.*;
import sandy.core.scenegraph.*;
import sandy.events.*
import sandy.materials.*;
import sandy.materials.attributes.*;
import sandy.primitive.*;
import sandy.core.scenegraph.mode7.*;
//
var main:Sprite;
var clip:MovieClip;
var clip2:MovieClip;
var clip3:MovieClip;
var _rotate:MovieClip;
var Width:int;
var Height:int;
var debugText:TextField = new TextField();
//
var camera:Camera3D;
var root:Group;
var scene:Scene3D;
//
var camera2:Camera3D;
var root2:Group;
var scene2:Scene3D;
//
var box:Box;
var boxArray:Vector.<boxParticle> = new Vector.<boxParticle>;
var visibleBoxArray:Vector.<boxParticle> = new Vector.<boxParticle>
var particles:uint = 100;
var widthparticles:uint = 4;
var lookForward:Number = 2.5;
var Track:Array;
var TrackLength:int;
function initialize():void {
camera = new Camera3D( 465, 465);
camera.x = 0;
camera.y = 0;
camera.z = 0;
root = new Group();
Track = new Array();
TrackLength = -1;
function AddToTrack(X:Number,Y:Number,Z:Number, Rot:Number = 0, Wide:Number = 100):void{
var _peice:Object = new Object();
_peice.x = X;
_peice.y = Y;
_peice.z = Z;
_peice.rot = Rot;
_peice.wide = Wide;
Track.push(_peice);
TrackLength++;
}
AddToTrack( 0, 0, 100, 0);
AddToTrack( 0, 0, 500, 0);
AddToTrack( 0, 50,1000, 0);
AddToTrack( 0, 150,1500, 2);
AddToTrack( 0, 50,2000, 4);
AddToTrack( 300, 50,2500, Math.PI*2);
AddToTrack( 2000, -50,2500, Math.PI);
AddToTrack( 2000, -50,1500, Math.PI/2);
AddToTrack( 1500, 250,1000, Math.PI/4);
AddToTrack( 1500, 300, 200, -Math.PI/4);
AddToTrack( 1300, 0,-900, Math.PI/3);
AddToTrack( 100, 0, -400, Math.PI/3);
AddToTrack( 0, 0, 100, 0);
courseposition = 1;
box = new Box("box", 10, 5, 20);
box.y = 10;
box.x = 200;
box.z = 400;
var mat:ColorMaterial = new ColorMaterial ( 0x7FDDFF );
mat.attributes = new MaterialAttributes (
new LightAttributes ());
mat.lightingEnable = true;
box.appearance = new Appearance (mat);
root.addChild(box);
scene = new Scene3D( "scene", clip3, camera, root );
for(var i:int=0;i<particles;i++){
for(var h:int=-widthparticles;h<=widthparticles;h++){
var p:boxParticle = new boxParticle();
boxArray.push(p);
clip2.addChild(p);
}
}
camera2 = new CameraMode7( 800, 800);
camera2.x = 0;
camera2.y = 500;
camera2.z = 0;
root2 = new Group();
var box2:Box = new Box("box2",100,1000,100, "tri",2)
box2.y = 500;
box2.x = 200;
box2.z = 800;
box2.appearance = new Appearance (mat);
box2.useSingleContainer = true;
root2.addChild(box2);
var box3:Box = new Box("box3",100,1000,100, "tri",2)
box3.y = 500;
box3.x = -200;
box3.z = 800;
box3.appearance = new Appearance (mat);
box3.useSingleContainer = true;
root2.addChild(box3);
var bmd:BitmapData = new BitmapData(400, 400, false, 0xCCCCCCCC);
var channels:uint = BitmapDataChannel.RED | BitmapDataChannel.BLUE | BitmapDataChannel.GREEN;
bmd.perlinNoise(400, 400, 5, int(Math.random() * 10), true, true, channels, false, null);
//bmd.colorTransform(new Rectangle(0, 0, 400, 400), new ColorTransform(1,1,0.3,1, 40,20,-10, 0));
bmd.applyFilter(bmd, bmd.rect, new Point(), new ColorMatrixFilter([
.5, .2, .1, 0, 0, // red
.5, .5, .1, 0, 0, // green
0, .1, .3, 0, 0, // blue
0, 0, 0, 1, 0] // alpha
)
);
var ground:Mode7 = new Mode7();
ground.setBitmap(bmd, 2, true, false);
ground.setHorizon(false);
//ground.setNearFar (true)
root2.addChild(ground);
scene2 = new Scene3D( "scene2", clip, camera2, root2 );
}
var courseposition:Number = 0;
var coursepositionspeed:Number = 0;
//
var carHorizontal:Number = 0;
var carRot:Number = 0;
var carRotSpeed:Number = 0;
var CourseDirection:Number = 0;
function EveryFrame(event:Event):void{
debugText.text = "";
// Car model positioning
var carposition:Object = TrackMath(courseposition);
var carposition2:Object = TrackMath(courseposition-0.01);
box.x = carposition.x;
box.y = carposition.y;
box.z = carposition.z;
box.lookAt(carposition2.x,carposition2.y,carposition2.z);
var rot:Number = Math.atan2(box.x-carposition2.x,box.z-carposition2.z);
var carRotZ:Number = Math.atan2(carposition2.z-carposition.z,carposition2.x-carposition.x)
box.roll += carposition.rot*180/Math.PI;
box.pan += (carRot-rot)*180/Math.PI;
box.moveForward(100);
var carRotY:Number = Math.atan2(box.y-carposition.y,
Math.sqrt(
Math.pow(box.x-carposition.x,2)+Math.pow(box.z-carposition.z,2)
)
);
box.moveForward(-100);
box.x += Math.sin(carRotZ)*carHorizontal*Math.cos(carposition.rot)*2;
box.y += Math.sin(carposition.rot)*carHorizontal*2;
box.z += Math.sin(carRotZ-Math.PI/2)*carHorizontal*Math.cos(carposition.rot)*2;
box.tilt -= 90;
box.moveForward(10); // floats off of the ground
box.tilt += 90;
// Camera
box.moveForward(250);
//
box.tilt -= 90;
box.moveForward(100);
//
camera.x += ((box.x)-camera.x)/4;
camera.y += ((box.y)-camera.y)/4;
camera.z += ((box.z)-camera.z)/4;
box.moveForward(-100);
box.tilt += 90;
box.moveForward(-250);
camera.lookAt(box.x,box.y,box.z);
camera.roll += -carposition.rot*180/Math.PI;
// keys
if(isDown(Keyboard.UP)){
coursepositionspeed+=0.005;
}
if(isDown(Keyboard.DOWN)){
coursepositionspeed-=0.005;
}
if(isDown(Keyboard.RIGHT)){
carRotSpeed += (0.25-carRotSpeed)/6;
}
if(isDown(Keyboard.LEFT)){
carRotSpeed += (-0.25-carRotSpeed)/6;
}
carRotSpeed *= 0.6;
carRot += carRotSpeed;
coursepositionspeed *= 0.8;
// car movement
var currentposition:Object = TrackMath(courseposition);
var forwardposition:Object = TrackMath(courseposition+0.02);
var forwardposition2:Object = TrackMath(courseposition+0.04);
var RotZ:Number = Math.atan2(forwardposition.z-currentposition.z,forwardposition.x-currentposition.x);
var RotZ2:Number = Math.atan2(forwardposition2.z-forwardposition.z,forwardposition2.x-forwardposition.x);
currentposition.x += Math.sin(RotZ)*carHorizontal*Math.cos(currentposition.rot)*2;
currentposition.y += Math.sin(currentposition.rot)*carHorizontal*2;
currentposition.z += Math.sin(RotZ-Math.PI/2)*carHorizontal*Math.cos(currentposition.rot)*2;
forwardposition.x += Math.sin(RotZ2)*carHorizontal*Math.cos(forwardposition.rot)*2;
forwardposition.y += Math.sin(forwardposition.rot)*carHorizontal*2;
forwardposition.z += Math.sin(RotZ2-Math.PI/2)*carHorizontal*Math.cos(forwardposition.rot)*2;
var dx:Number = forwardposition.x-currentposition.x;
var dy:Number = forwardposition.y-currentposition.y;
var dz:Number = forwardposition.z-currentposition.z;
var d:Number = Math.sqrt(dx*dx+dy*dy+dz*dz);
var rotXZ:Number = Math.atan2(-dx,-dz);
// may want to change how this is calculated....
courseposition -= Math.cos(carRot-rotXZ)*coursepositionspeed*25*(1/d);
carHorizontal += Math.sin(carRot-rotXZ)*coursepositionspeed*500;
//courseposition += (coursepositionspeed/d)*100;
//carRot = Math.atan2(dx,dz);
// walls on left and right side
carHorizontal = Math.min(carHorizontal,currentposition.wide/2-10);
carHorizontal = Math.max(carHorizontal,-currentposition.wide/2+10);
// add (possible) falling off of track maybe?
CourseDirection = -Math.cos(carRot-rotXZ); // -1 for the car going backwards on the track
//debugText.text = String(CourseDirection);
if(courseposition <= 0){
courseposition += TrackLength;
}
if(courseposition >= TrackLength){
courseposition -= TrackLength;
}
// construct the track
var p:boxParticle
var rotX:Number = 0;
var rotY:Number = 0;
var rotZ:Number = 0;
var newpx:Number = 0;
var newpy:Number = 0;
var newpz:Number = 0;
var oldpx:Number = 0;
var oldpy:Number = 0;
var oldpz:Number = 0;
var h:int = -widthparticles;
var i:int = 0;
var positions:Object;
var thing:Number = (particles/lookForward)/3;
var _d:Number = 0;
//
var _position:Number = 0;
if(CourseDirection > 0){
// Camera looking ahead
_position = Math.floor(courseposition)-0.5;
}else{
// Camera looking back
_position = Math.floor(courseposition)+0.5;
}
var _increase:Number = .15;
for each(p in boxArray){
h++;
if(h>widthparticles){
if(_d > 0 && !isNaN(_d)){
if(CourseDirection > 0){
_position += _increase/_d;
if(_position > TrackLength){
//_position -= TrackLength;
}
}else{
// Camera looking back
_position -= _increase/_d;
if(_position < 0){
//_position += TrackLength;
}
}
_increase += 0.01;
}
positions = TrackMath(_position);
oldpx = newpx;
oldpy = newpy;
oldpz = newpz;
newpx = positions.x;
newpy = positions.y;
newpz = positions.z;
if(i==0){
var positions2:Object = TrackMath(_position-.01);
oldpx = positions2.x;
oldpy = positions2.y;
oldpz = positions2.z;
}
var _dx:Number = (oldpx-positions.x);
var _dy:Number = (oldpy-positions.y);
var _dz:Number = (oldpz-positions.z);
_d = Math.sqrt(_dx*_dx+_dy*_dy+_dz*_dz);
rotY = Math.atan2(newpx-oldpx,newpy-oldpy);
rotZ = Math.atan2(oldpz-newpz,oldpx-newpx);
rotX = positions.rot;
var roadwidth:Number = positions.wide;
i++;
h = -widthparticles;
}
//
var hposition:Number = h;
if(h == -widthparticles || h == widthparticles){
hposition = subtract(hposition,0.8);
p.size = 8;
p.red = 144+(i%3)*14;
p.green = 144+(i%3)*8;
p.blue = 144;
}else{
p.size = 15;
p.red = 118;
p.green = 124+((h+i)%3)*5;
p.blue = 184;
}
p.px = newpx+Math.sin(rotZ)*hposition*roadwidth*Math.cos(rotX)/widthparticles;
p.py = newpy+Math.sin(rotX)*hposition*roadwidth/widthparticles;
p.pz = newpz+Math.sin(rotZ-Math.PI/2)*hposition*roadwidth*Math.cos(rotX)/widthparticles;
if(h == -widthparticles || h == widthparticles){
p.px += Math.sin(rotZ)*12*Math.cos(rotX+Math.PI/2);
p.py += Math.sin(rotX+Math.PI/2)*12;
p.pz += Math.sin(rotZ-Math.PI/2)*12*Math.cos(rotX+Math.PI/2);
}
}
// render everything
camera2.y = camera.y;
camera2.x = camera.x;
camera2.z = camera.z;
camera.moveForward(100);
camera2.lookAt(camera.x,camera.y,camera.z);
camera.moveForward(-100);
camera2.y += 500;
scene2.render();
_rotate.rotation = -carposition.rot*180/Math.PI;
scene.render();
clip2.graphics.clear();
for each(p in boxArray){
SortBox(p, camera);
}
boxArray.sort(Zsort);
function Zsort(p1:boxParticle, p2:boxParticle):Number{
if (p1.depth < p2.depth)
return 1;
else if (p1.depth > p2.depth)
return -1;
else
return 0;
}
for each(p in boxArray){
DrawBox(p, clip2);
}
}
function SortBox(_box:boxParticle, camera:Camera3D):void{
var _v:Vertex = new Vertex(_box.px,_box.py,_box.pz);
var m:Matrix4 = camera.invModelMatrix;
_v.wx = _v.x * m.n11 + _v.y * m.n12 + _v.z * m.n13 + m.n14;
_v.wy = _v.x * m.n21 + _v.y * m.n22 + _v.z * m.n23 + m.n24;
_v.wz = _v.x * m.n31 + _v.y * m.n32 + _v.z * m.n33 + m.n34;
camera.projectVertex(_v);
_box.depth = _v.wz+_v.sx/1000;
_box.sx = _v.sx;
_box.sy = _v.sy;
_box.wz = _v.wz;
}
function DrawBox(_box:boxParticle, _clip:MovieClip):void{
if(_box.wz >= camera.near && _box.wz <= camera.far){
var fadeout:Number = 1/(_box.wz/400);
fadeout = Math.max(fadeout,0);
fadeout = Math.min(fadeout,1);
var _color:uint =
_box.red*fadeout+200*(1-fadeout) << 16
| _box.green*fadeout+200*(1-fadeout) << 8
| _box.blue*fadeout+205*(1-fadeout);
_clip.graphics.beginFill(_color);
var _size:Number = _box.size*1000/_box.wz;
_clip.graphics.drawRect( _box.sx-_size/2, _box.sy-_size/2, _size, _size);
_clip.graphics.endFill();
}
}
function TrackMath(_pos:Number):Object{
var _i:int = int(_pos)%(TrackLength);
//_i = Math.max(_i,0);
while(_i < 0){
_i += TrackLength;
}
var a:Number = _pos%1;
var b:Number = 1-a;
//
var _t:Object = new Object();
var tx:Number;
var ty:Number;
var tz:Number;
var trot:Number;
var twide:Number;
//
var px:Number;
var py:Number;
var pz:Number;
var prot:Number;
var pwide:Number;
//
if(_i+1<TrackLength){
tx = (Track[_i+1].x+Track[_i+2].x)/2;
ty = (Track[_i+1].y+Track[_i+2].y)/2;
tz = (Track[_i+1].z+Track[_i+2].z)/2;
trot = (Track[_i+1].rot+Track[_i+2].rot)/2;
twide = (Track[_i+1].wide+Track[_i+2].wide)/2;
}else{
//
tx = Track[_i+1].x;
ty = Track[_i+1].y;
tz = Track[_i+1].z;
trot = Track[_i+1].rot;
twide = Track[_i+1].wide;
}
if(_i>0){
px = (Track[_i].x+Track[_i+1].x)/2;
py = (Track[_i].y+Track[_i+1].y)/2;
pz = (Track[_i].z+Track[_i+1].z)/2;
prot = (Track[_i].rot+Track[_i+1].rot)/2;
pwide = (Track[_i].wide+Track[_i+1].wide)/2;
}else{
px = Track[_i].x;
py = Track[_i].y;
pz = Track[_i].z;
prot = Track[_i].rot;
pwide = Track[_i].wide;
}
_t.x = b*b*px+2*a*b*Track[_i+1].x+a*a*tx;
_t.y = b*b*py+2*a*b*Track[_i+1].y+a*a*ty;
_t.z = b*b*pz+2*a*b*Track[_i+1].z+a*a*tz;
_t.rot = b*b*prot+2*a*b*Track[_i+1].rot+a*a*trot;
_t.wide = b*b*pwide+2*a*b*Track[_i+1].wide+a*a*twide;
return _t;
}
function subtract(num:Number, num2:Number):Number{
if(num > 0){
num = Math.max(0, num-num2);
}else{
num = Math.min(0, num+num2);
}
return num;
}
var initialized:Boolean = false;// marks whether or not the class has been initialized
var keysDown:Object = new Object();// stores key codes of all keys pressed
function isDown(keyCode:uint):Boolean {
return Boolean(keyCode in keysDown);
}
function keyPressed(event:KeyboardEvent):void {
keysDown[event.keyCode] = true;
}
function keyReleased(event:KeyboardEvent):void {
if (event.keyCode in keysDown) {
delete keysDown[event.keyCode];
}
}
class boxParticle extends Sprite{
public var px:Number;
public var py:Number;
public var pz:Number;
public var depth:Number;
public var size:Number;
public var red:Number;
public var green:Number;
public var blue:Number;
public var sx:Number;
public var sy:Number;
public var wz:Number;
}