Resources
Skyline resources; Third party applications, Demo Scenes, Action plug-ins, Action Scripts, Presets etc any thing that can be used by Skyline

Aeroplane Script

Aeroplane Script

Postby SolarPortal » 24 Apr 2015, 19:39

Hey,
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 :)
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.
We are still working on this and will keep this thread updated. Hoping to get Joypad controls working too :D
Look forward to seeing some more videos from Tattie ;) and any other work people make using this script.

To get this working:
> Add your mesh
> Add a microscript or external script and add the code from above.
> Press play and fly around. :)

Enjoy!
Conceive, Quantify & Do!
Skyline Game Engine - Lead Developer.
Please provide as much information as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 3770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3151
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Aeroplane Script

Postby SolarPortal » 25 Apr 2015, 21:19

Hi guys, got another update for this. Just had a spare 10 minutes and added Joypad (Xbox 360 controller) control to the plane.
Now the left stick will pitch and roll the plane. It does get a bit to get used to, but feels pretty good.

The code in the first post has been updated. :)
Conceive, Quantify & Do!
Skyline Game Engine - Lead Developer.
Please provide as much information as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 3770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3151
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Aeroplane Script

Postby TattieBoJangle » 25 Apr 2015, 21:30

Thanks! :D
Case: CM Storm Trooper CPU: I7 5930k X99 Cooler: Noctua NH-D15 Graphics: Asus GTX 1080 Motherboard: Rampage Extreme V x99 Ram: RipJaws DDR4 3000mhz Storage: x2 SSD Crucial 500GB + x5 2TB Hdd PSU: Evga 1500w OS: Windows 10
User avatar
TattieBoJangle
Community Manager
Community Manager
 
Posts: 858
Joined: 26 Jan 2015, 00:15
Location: United Kingdom
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer
Skill: Great creative


Return to Resources

Who is online

Users browsing this forum: No registered users and 1 guest

cron