J-128

DragonController.py

May 16th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.18 KB | None | 0 0
  1. import bge, Util, math
  2.  
  3. cont = bge.logic.getCurrentController()
  4. scene = bge.logic.getCurrentScene()
  5. own = cont.owner
  6.  
  7. keySens = cont.sensors["ReadInput"]
  8.  
  9. ori = Util.getRotInDegrees(own)
  10.  
  11. own['alt'] = own.worldPosition[2]
  12.  
  13. if own["sim_flightmode"] == False: #We are in basic mode
  14.    
  15.     if [bge.events.LEFTARROWKEY, bge.logic.KX_INPUT_ACTIVE] in keySens.events:
  16.         #ori = Util.getRotInDegrees(own)
  17.         #ori[2] += own["yawRate"]
  18.         #Util.setRotInDegrees(ori, own, False)
  19.         own.applyTorque([0, 0, own['yawRate']])
  20.     elif [bge.events.RIGHTARROWKEY, bge.logic.KX_INPUT_ACTIVE] in keySens.events:
  21.         #ori = Util.getRotInDegrees(own)
  22.         #ori[2] -= own["yawRate"]
  23.         #Util.setRotInDegrees(ori, own, False)
  24.         own.applyTorque([0, 0, -own['yawRate']])
  25.     if [bge.events.UPARROWKEY, bge.logic.KX_INPUT_ACTIVE] in keySens.events:
  26.         #ori = Util.getRotInDegrees(own)
  27.         #ori[0] -= own["pitchRate"]
  28.         own.applyTorque([-own['pitchRate'], 0, 0], True)
  29.        
  30.         if ori[0] < -(own["maxPitch"]):
  31.             ori[0] = -(own["maxPitch"])
  32.         Util.setRotInDegrees(ori, own)
  33.     elif [bge.events.DOWNARROWKEY, bge.logic.KX_INPUT_ACTIVE] in keySens.events:
  34.         #ori = Util.getRotInDegrees(own)
  35.         #ori[0] += own["pitchRate"]
  36.         own.applyTorque([own['pitchRate'], 0, 0], True)
  37.        
  38.         if ori[0] > (own["maxPitch"]):
  39.             ori[0] = (own["maxPitch"])
  40.         Util.setRotInDegrees(ori, own)
  41.        
  42.     if len(keySens.events) == 0 or (len(keySens.events) == 1 and [bge.events.SPACEKEY, bge.logic.KX_INPUT_ACTIVE] in keySens.events):
  43.         if ori[0] > 0:
  44.             ori[0] -= 1 #own["pitchRate"]
  45.         elif ori[0] < 0:
  46.             ori[0] += 1 #own["pitchRate"]
  47.        
  48.         if ori[1] > 0:
  49.             ori[1] -= 1 #own["rollRate"]
  50.         elif ori[1] < 0:
  51.             ori[1] += 1 #own["rollRate"]
  52.        
  53.         ori[0] = int(ori[0])
  54.         ori[1] = int(ori[1])  
  55.        
  56.         Util.setRotInDegrees(ori, own)
  57.  
  58. else: #We are in 'Flight sim' mode
  59.     if [bge.events.LEFTARROWKEY, bge.logic.KX_INPUT_ACTIVE] in keySens.events:
  60.         #ori[1] -= own["rollRate"]
  61.         #Util.setRotInDegrees(ori, own)
  62.         own.applyTorque([0, -own['rollRate'], 0], True)
  63.     elif [bge.events.RIGHTARROWKEY, bge.logic.KX_INPUT_ACTIVE] in keySens.events:
  64.         #ori[1] += own["rollRate"]
  65.         #Util.setRotInDegrees(ori, own)
  66.         own.applyTorque([0, own['rollRate'], 0], True)
  67.    
  68.     if [bge.events.UPARROWKEY, bge.logic.KX_INPUT_ACTIVE] in keySens.events:
  69.         #ori[0] -= own["pitchRate"]
  70.         #Util.setRotInDegrees(ori, own)
  71.         own.applyTorque([-own['pitchRate'], 0, 0], True)
  72.     elif [bge.events.DOWNARROWKEY, bge.logic.KX_INPUT_ACTIVE] in keySens.events:
  73.         #ori[0] += own["pitchRate"]
  74.         #Util.setRotInDegrees(ori, own)
  75.         own.applyTorque([own['pitchRate'], 0, 0], True)
  76.  
  77. own["XRot"] = ori[0]
  78. own["YRot"] = ori[1]
  79. own["ZRot"] = ori[2]
  80.  
  81. ############## Movement handling ##############
  82. #Since this script is run every logic tick (and
  83. #  since it's the "Dragon Controller" script),
  84. #  I don't think there's a better place in which
  85. #  to run the necessary physics, do you?
  86.  
  87. if [bge.events.SPACEKEY, bge.logic.KX_INPUT_ACTIVE] in keySens.events:
  88.     #own.applyForce([0, 35, 9.8125], True)
  89.  
  90.     ##Avar's script##
  91.  
  92.     AltitudeOut = own['alt']
  93.     VelocityOut = own.getLinearVelocity(True)[1]
  94.     AltitudeIn = own['alt']
  95.     VelocityIn = own.getLinearVelocity(True)[1]
  96.  
  97.     #AltitudeIn = AltitudeOut
  98.     #VelocityIn = VelocityOut
  99.     ThrustF = 12000
  100.     Area = 4.0875
  101.     DragCoefficient = 0.2295
  102.     ## Density in kilograms per cubic meter
  103.     Density = 55
  104.     ## Mass in kilograms: SI units used
  105.     Mass = own.mass
  106.     CompressionCoefficient = 0.1
  107.     Pitch = Util.getRotInDegrees(own)[0]
  108.     TurnCoefficient = 1
  109.     g = 9.8
  110.     AtmCeil = 90000
  111.  
  112.     DragF = DragCoefficient * ((Density * VelocityIn ** 2) / 2) * Area  + (ThrustF * VelocityIn * 0.005)
  113.     Acceleration = (ThrustF - DragF) / Mass
  114.     VelocityOut = VelocityIn + Acceleration
  115.     DragF = DragCoefficient * ((Density * VelocityIn ** 2) / 2) * Area  + (ThrustF * VelocityOut * 0.005)
  116.     #Acceleration = (ThrustF - DragF) / Mass
  117.     VelocityOut = VelocityIn + Acceleration
  118.  
  119.     Climb = ((math.tan(Pitch)) * ThrustF / Mass) - g
  120.  
  121.     #VelocityP = round(VelocityOut * 2.234, 2)
  122.    
  123.     ##End Avar's Script##
  124.    
  125.     own.applyForce([0, VelocityOut, Climb], True)
  126.  
  127. else:    
  128.     own.applyForce([0, 0, 9.8]) #Negate gravity's effect
Advertisement
Add Comment
Please, Sign In to add comment