I am sure you have all seen Tattie's video of the fighter jet / Spaceship flying around.
Which is here for those that haven't seen: http://www.chi-ad.com/Skyline/Forum/viewtopic.php?f=41&t=1009
This is a flight that is not using physics. It is simply made using the entity lua library.
Here is the plane code:
EDIT: Updated code on 25/04/2015. Added Joypad events for flight
![Smile :)](./images/smilies/icon_e_smile.gif)
- Code: Select all
-- | User Changeable Properties.
-- |=========================================================================================================================
minSpeed = 80; -- Min speed the plane can travel. This is in no unit measurement. Larger = faster. Smaller = Slower
maxSpeed = 120; -- Max speed the plane can travel. This is in no unit measurement. Larger = faster. Smaller = Slower
vBoost = 80; -- Use to rotate the plane on the pitch(up/down) quicker. Larger = faster.
hBoost = 130; -- Use to rotate the plane on the roll(left/right) quicker. Larger = faster.
reactionSpeed = 5; -- This changes how fast you can cahnge direction when another key is pressed. Larger = Firmer. Smaller = Lazier
-- | Required Script Variables. (Globals)
-- |=========================================================================================================================
obj = 0;
speed = 0;
wKeyDown = 0; aKeyDown = 0; sKeyDown = 0; dKeyDown = 0;
cVertTurn = 0;
cHorTurn = 0;
joyPadPitch = 0;
joyPadRoll = 0;
-- | Airplane script
-- |=========================================================================================================================
function onInit(objID)
obj = objID;
end
function onUpdate( timeDelta )
-- Fetch inputs and get smooth values
doInput( timeDelta );
-- Propel the plane forwards
doMovement( timeDelta );
-- Apply Pitch & Roll to control the plane and make it turn corners.
doRotation( timeDelta );
-- Stop the plane from going through the terrain floor.
checkGroundClearance();
end
function doMovement( timeDelta )
-- get the normalised(0-1 range) forward direction of the plane.
forward = newType.vec3 (vector3.normalisedCopy( entity.getForward(obj) ) );
-- set the speed of the plane, slower going up and faster pointing down for gravity effect.
speed = speed - forward.y * timeDelta * 50.0;
if(speed < minSpeed)then speed = minSpeed; end
if(speed > maxSpeed)then speed = maxSpeed; end
-- Now move the entity forwards by the speed and using timedelta for the same flight at all framerates
entity.move(obj, (forward.x * speed) * timeDelta, (forward.y * speed) * timeDelta, (forward.z * speed) * timeDelta )
end
function doRotation( timeDelta )
entity.turn(obj, cVertTurn * vBoost * timeDelta, 0, cHorTurn * hBoost * timeDelta);
end
function checkGroundClearance()
-- Keep plane above terrain ground.
currentPos = newType.vec3(entity.getPosition(obj));
terrainheight = terrain.getHeightAtPoint( currentPos.x, currentPos.z );
if( currentPos.y < terrainheight )then
entity.setPosition( obj, currentPos.x, terrainheight, currentPos.z );
end
end
function doInput( timeDelta )
-- Grab the input values and smooth them out for nicer control
smoothRate = reactionSpeed * timeDelta;
-- Plane pitching Up/Down input
if(wKeyDown == 1)then
cVertTurn = cVertTurn - smoothRate;
elseif(sKeyDown == 1)then
cVertTurn = cVertTurn + smoothRate;
elseif(joyPadPitch > 0.1)then
cVertTurn = ( cVertTurn - smoothRate ) * joyPadPitch;
elseif(joyPadPitch < -0.1)then
cVertTurn = ( cVertTurn + smoothRate ) * -joyPadPitch;
else
if(cVertTurn > 0.1)then
cVertTurn = cVertTurn - smoothRate;
elseif(cVertTurn < -0.1)then
cVertTurn = cVertTurn + smoothRate;
else
cVertTurn = 0;
end
end
if(cVertTurn > 1)then cVertTurn = 1; end
if(cVertTurn < -1)then cVertTurn = -1; end
-- Plane Banking rolling input
if(aKeyDown == 1)then
cHorTurn = cHorTurn - smoothRate;
elseif(dKeyDown == 1)then
cHorTurn = cHorTurn + smoothRate;
elseif(joyPadRoll > 0.1)then
cHorTurn = ( cHorTurn - smoothRate ) * joyPadRoll;
elseif(joyPadRoll < -0.1)then
cHorTurn = ( cHorTurn + smoothRate ) * -joyPadRoll;
else
if(cHorTurn > 0.1)then
cHorTurn = cHorTurn - smoothRate;
elseif(cHorTurn < -0.1)then
cHorTurn = cHorTurn + smoothRate;
else
cHorTurn = 0;
end
end
if(cHorTurn > 1)then cHorTurn = 1; end
if(cHorTurn < -1)then cHorTurn = -1; end
end
-- | Input key events
-- |=========================================================================================================================
function onKeyDown( key )
if(key == "w")then wKeyDown = 1; end
if(key == "a")then aKeyDown = 1; end
if(key == "s")then sKeyDown = 1; end
if(key == "d")then dKeyDown = 1; end
end
function onKeyUp( key )
if(key == "w")then wKeyDown = 0; end
if(key == "a")then aKeyDown = 0; end
if(key == "s")then sKeyDown = 0; end
if(key == "d")then dKeyDown = 0; end
end
---------------------------------------------------------------------------------
-- INPUT: GAMEPAD
---------------------------------------------------------------------------------
function onJSAxisMoved(axis, value)
if(axis==0) then
if(value<-400 )then
joyPadPitch = 1;
elseif(value > 400)then
joyPadPitch = -1;
else
joyPadPitch = 0;
end
end
if(axis==1) then
if(value<-400 )then
joyPadRoll = 1;
elseif(value > 400)then
joyPadRoll = -1;
else
joyPadRoll = 0;
end
end
end
This is a more advanced version of the script which has:
- smooth pitch and roll effects.
- min / max speed
- When you fly up, the plane slows down. When you fly down, the speed increases steadily to max speed which makes it seem as though gravity is affecting it. Use a lazy cam to get cool movements.
- You cannot fly through the terrain ground. Normal entities you can as this is NOT physics based.
- multiple adjustment properties to help make it flyable.
- Fully timedelta'd. This means it will fly the same at 30 fps as it would at 500 fps.
- Keys do not stop working like the previous code did.
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
Look forward to seeing some more videos from Tattie
![Wink ;)](./images/smilies/icon_e_wink.gif)
To get this working:
> Add your mesh
> Add a microscript or external script and add the code from above.
> Press play and fly around.
![Smile :)](./images/smilies/icon_e_smile.gif)
Enjoy!