a1

From [name redacted], 1 Year ago, written in Plain Text, viewed 2 times. This paste will join the choir invisible in 1 Second.
URL https://paste.paivola.fi/view/a73af15f Embed
Download Paste or View Raw
  1. #Sukellusvenettä ohjaava koodi
  2. ##Kirjoitettu GD-scriptillä käyttöön Godot-pelimoottorin sisällä
  3.  
  4. extends KinematicBody
  5.  
  6. #universal variables and constants
  7. var propRight = 0
  8. var propLeft = 0
  9.  
  10. var rudderLength = 2
  11. var sideRudderLength = 0.5
  12.  
  13. var wakeFraction = 0.1
  14. var gearRatio = 10
  15. var thrustDeduction = 0.2
  16.  
  17. var energyconsumption = 0
  18.  
  19. var angularv = 0
  20. var phi = 0
  21.  
  22. var gravAcc = 9.81
  23. var density = 997
  24. var subVolume = 18.6
  25. var subLiftCo = 0.35
  26. var rudderLiftCo = 0.5
  27. var topDragCo = 0.2
  28. var topLiftCo = 0.5
  29. var subArea = 50
  30. var rudderArea = 0.5
  31. var topArea = 2
  32. var frontArea = 200
  33. var backArea = 150
  34.  
  35. var rudderAngle = 0
  36. var topAngle = 0
  37. var subMass = 18600
  38. var prop1Thrust = 0
  39. var propPower = 14000
  40. var topSpeed = 2.57
  41. var topForce = 35932
  42.  
  43.  
  44.  
  45. var totacc = Vector3(0,0,0)
  46. var dragFront
  47. var dragBack
  48.  
  49. var velocity = Vector3(0,0,0)
  50.  
  51. var subDrag = Vector3()
  52. var rudderDrag = Vector3()
  53. var topDrag = Vector3()
  54. var subRotStart = Vector3(0, 0, 0)
  55. var subRotEnd = Vector3()
  56. var angle = Vector3(0,0,5)
  57. var subAngle = 3
  58. #var subfront = Vector3()
  59.  
  60. var subRotAxis = Vector2()
  61. var velocityAxis = Vector2()
  62. var globalx = Vector2(1, 0)
  63. var globaly = Vector2(0, 1)
  64. var test = Vector3(0, 0, 0)
  65. var normVelocity = Vector3()
  66. var normSub = Vector3()
  67.  
  68. var rudderDragTot
  69. var subDragTot
  70. var topDragTot
  71. var vectorRatio
  72. var dragTot
  73.  
  74. var prop1accdir = angle
  75.  
  76.  
  77. var frontDragp = 1
  78. var backDragp = 0.7
  79.  
  80. #dimensions of sub
  81. var length = 7.8
  82. #distance to center of mass from back
  83. var lengthBack = length/2
  84. var width = 3.3
  85. var propSize = width/2
  86.  
  87. #dimensions of rudder
  88.  
  89. var rudderwidth = 0.5
  90. var rudderthick = 0.1
  91.  
  92. #var massfront = subMass/2
  93. #var massback = subMass/2
  94. var frontRadius = 2
  95. var backRadius = 2
  96. var dfromback = 1
  97. var dfromfront = 1
  98. #volume of water
  99. var waterfront = 0
  100. var waterback = 0
  101. var masscenter = length/2
  102. var backBallastMass = 20
  103. var frontBallastMass = 20
  104.  
  105. onready var HUD = get_parent().get_node("HUD")
  106.  
  107.  
  108.  
  109. #connect to signals sent from control center
  110. func _ready():
  111.         add_user_signal("energy_consumption", ["value"])
  112.         HUD.connect("thrust_change", self, "_change_thrust")
  113.         HUD.connect("rudder_angle_change", self, "_change_rudder_angle")
  114.         HUD.connect("top_angle_change", self, "_change_top_angle")
  115.         HUD.connect("waterfront_change", self, "_change_waterfront")
  116.         HUD.connect("waterback_change", self, "_change_waterback")
  117.         HUD.connect("thrustRight_change", self, "_change_thrustRight")
  118.         HUD.connect("thrustLeft_change", self, "_change_thrustLeft")
  119.        
  120.  
  121.  
  122. func _rudder_drag(velocity, rudderAngle):
  123.         var orthoproj = rudderArea * cos(rudderAngle)
  124.         var drag = 0.5 * density * velocity.length() * velocity.length() * (0.009 * pow(2.71828, 0.155 * rudderAngle)) * orthoproj
  125.         return drag
  126.  
  127.  
  128.  
  129. func _sub_drag(velocity, angleto):
  130.         var orthoproj = abs(sin(angleto) * length * width) + abs(cos(angleto) * PI * width * width / 4)
  131.         return -0.5 * density * velocity * orthoproj * (0.0005 * angleto * angleto + 0.0012 * angleto + 0.1322)
  132.        
  133.        
  134.  
  135. func _sub_lift(velocity, angleto, angle):
  136.         var orthoproj = cos(PI/2 - angleto) * length * width + sin(angleto) * PI * width * width
  137.         return  0.5 * density * velocity.length() * orthoproj * (0.023 * angleto + 0.001) * (-angle + velocity.normalized() * cos(angleto) * length)
  138.        
  139.  
  140.  
  141. func _what_angle_to(velocity, subRotation):
  142.         if velocity.y * subRotation.y > 0:
  143.                 if velocity.angle_to(globalx) > subRotation.angle_to(globalx):
  144.                         if velocity.y > 0:
  145.                                 return  velocity.angle_to(subRotation)
  146.                                 #return 0
  147.                         else:
  148.                                 return velocity.angle_to(subRotation)
  149.                                 #return 0
  150.                 else:
  151.                         if velocity.y > 0:
  152.                                 return (-1) * velocity.angle_to(subRotation)
  153.                                 #return 0
  154.                         else:
  155.                                 return  velocity.angle_to(subRotation)
  156.                                 #return 0
  157.         else:
  158.                 if velocity.angle_to(subRotation) < velocity.angle_to(globalx) + subRotation.angle_to(globalx):
  159.                         if velocity.y > 0:
  160.                                 return (-1) * velocity.angle_to(subRotation)
  161.                                 #return 0
  162.                         else:
  163.                                 return  velocity.angle_to(subRotation)
  164.                                 #return 0
  165.                 else:
  166.                         if velocity.y > 0:
  167.                                 return velocity.angle_to(subRotation)
  168.                                 #return 0
  169.                         else:
  170.                                 return (-1) * velocity.angle_to(subRotation)
  171.                                 #return 0
  172.  
  173.  
  174.  
  175. func _rudder_lift(velocity, rudderAngle, angleto, angle):
  176.         var angletorudder = 180- angleto - rudderAngle
  177.         if(angletorudder > 90):
  178.                 angletorudder = 180 - angletorudder
  179.         var orthoproj = rudderArea * sin(angletorudder)
  180.         return  0.5 * density * velocity.length() * velocity.length() * orthoproj * (0.0632 * angletorudder + 0.0031)
  181.  
  182.  
  183.  
  184. func _thrust(propRot, velocity):
  185.         if(propRot == 0):
  186.                 return 0
  187.        
  188.        
  189.        
  190.         var propRotation = propRot / gearRatio
  191.         var kt = -0.359 * velocity * (1-wakeFraction) / (rudderLength * abs(propRotation)/gearRatio) + 0.404
  192.         if(kt < 0):
  193.                 kt = 0.404
  194.        
  195.         return 10 * density * rudderLength * rudderLength * rudderLength * rudderLength * (1-thrustDeduction) * abs(propRotation) * propRotation
  196.  
  197.  
  198.  
  199. func _side_thrust(propRot, velocity):
  200.         var propRotation = propRot / 2
  201.         return 10 * density * sideRudderLength * sideRudderLength * sideRudderLength * sideRudderLength * (1-thrustDeduction) * abs(propRotation) * propRotation
  202.  
  203.  
  204.  
  205. func _rotate(angle, nextangle):
  206.         #x
  207.         if Vector2(angle.z, angle.y).angle_to(Vector2(1, 0)) > Vector2(nextangle.z, nextangle.y).angle_to(Vector2(1,0)):
  208.                 rotate_x(-1 * Vector2(angle.z, angle.y).angle_to(Vector2(nextangle.z, nextangle.y)))
  209.         else:
  210.                 #this may be incorrect
  211.                 rotate_x(-1 * Vector2(angle.z, angle.y).angle_to(Vector2(nextangle.z, nextangle.y)))
  212.        
  213.         #y
  214.         if Vector2(angle.x, angle.z).angle_to(Vector2(1, 0)) > Vector2(nextangle.x, nextangle.z).angle_to(Vector2(1,0)):
  215.                 rotate_x(-1 * Vector2(angle.x, angle.z).angle_to(Vector2(nextangle.x, nextangle.z)))
  216.         else:
  217.                 rotate_x(Vector2(angle.x, angle.z).angle_to(Vector2(nextangle.x, nextangle.z)))
  218.        
  219.         #z
  220.         if Vector2(angle.x, angle.y).angle_to(Vector2(1, 0)) > Vector2(nextangle.x, nextangle.y).angle_to(Vector2(1,0)):
  221.                 rotate_x(-1 * Vector2(angle.x, angle.y).angle_to(Vector2(nextangle.x, nextangle.y)))
  222.         else:
  223.                 rotate_x(Vector2(angle.x, angle.y).angle_to(Vector2(nextangle.x, nextangle.y)))
  224.  
  225.  
  226.  
  227. func _change_thrust(value):
  228.         prop1Thrust = value
  229.         print(value)
  230.  
  231. func _change_rudder_angle(value):
  232.         rudderAngle = value
  233.  
  234. func _change_top_angle(value):
  235.         topAngle = value
  236.  
  237. func _change_waterfront(value):
  238.         waterfront = value
  239.  
  240. func _change_waterback(value):
  241.         waterback = value
  242.  
  243. func _change_thrustLeft(value):
  244.         propLeft = value
  245.  
  246. func _change_thrustRight(value):
  247.         propRight = value
  248.  
  249.  
  250.  
  251. func _process(delta):
  252.        
  253.         totacc = Vector3()
  254.         var angleto = angle.angle_to(velocity)
  255.         var masstot = subMass + (waterfront + waterback) * 997
  256.         var totforce = Vector3(0,0,0)
  257.  
  258.         totforce += angle.normalized() * _thrust(prop1Thrust, velocity.length())
  259.         totforce += angle.normalized() *_side_thrust(propRight, velocity.length())
  260.         totforce += angle.normalized() * _side_thrust(propLeft, velocity.length())
  261.  
  262.         totforce.y += subMass * 9.81
  263.         totforce.y -= gravAcc * masstot
  264.  
  265.         totforce += _sub_drag(velocity, angleto)
  266.         totforce += _sub_lift(velocity, angleto, angle)
  267.        
  268.         var rudderOrient = 180 - angleto - rudderAngle
  269.         if(rudderOrient > 90):
  270.                 rudderOrient = 180 - rudderOrient
  271.        
  272.         totforce -= _rudder_drag(velocity, rudderOrient) * angle.normalized()
  273.  
  274.         var rudderLift = _rudder_lift(velocity, rudderAngle, angleto, angle)
  275.         var anglexylength = sqrt(angle.x * angle.x + angle.z * angle.z)
  276.         var angletox = angle.angle_to(Vector3(1,0,0))
  277.         var normalAngle = Vector3()
  278.         if(angle.y != 0):
  279.                 normalAngle = Vector3(cos(angletox)*angle.y, sin(angletox)*angle.y, anglexylength)
  280.         else:
  281.                 normalAngle = Vector3(0, sqrt(angle.x * angle.x + angle.z * angle.z),0)
  282.        
  283.         if(rudderAngle > 0):
  284.                 totforce += -_rudder_lift(velocity, rudderAngle, angleto, angle) * normalAngle.normalized()
  285.         else:
  286.                 totforce += (_rudder_lift(velocity, rudderAngle, angleto, angle) * normalAngle.normalized())
  287.        
  288.  
  289.         move_and_slide(0.5 * (totforce/masstot) * delta * delta + velocity * delta)
  290.         velocity += (totforce/masstot) * delta
  291.        
  292.        
  293.         var rotacc = Vector3(0, 0, 0)
  294.        
  295.        
  296.         var backAngle = 0.30
  297.         var backBallast = sin(angleto) * (dfromback + backRadius)
  298.         var backWater = sin(angleto) * (dfromback + backRadius) - sin(velocity.angle_to(Vector3(0,1,0)))*((4 * backRadius * sin(backAngle)*sin(backAngle)*sin(backAngle)) / (3* (2* backAngle - sin(2*backAngle))))
  299.         var backtot = (backBallast * backBallastMass + backWater * waterback * density)/(backBallastMass + waterback * density)
  300.        
  301.         var frontAngle = 0.30
  302.         var frontBallast = sin(angleto) * (dfromback + backRadius)
  303.         var frontWater = length - sin(angleto) * (length - dfromback + backRadius) + sin(velocity.angle_to(Vector3(0,1,0)))*((4 * backRadius * sin(backAngle)*sin(backAngle)*sin(backAngle)) / (3* (2* backAngle - sin(2*backAngle))))
  304.         var fronttot = (frontBallast * frontBallastMass + frontWater * waterfront * density)/(frontBallastMass + waterfront * density)
  305.        
  306.         var subcenter = sin(angleto) * length / 2
  307.        
  308.         masscenter = (subcenter * subMass + fronttot * (waterfront * density + frontBallastMass) + backtot * (waterback * density + backBallastMass)) / (subMass + (waterfront + waterback) * density + frontBallastMass + backBallastMass)
  309.        
  310.  
  311.         var sideDrag = (-(_sub_drag(velocity, angleto)) + angle.normalized() * cos(angleto)*_sub_drag(velocity, angleto).length()) * (length/2 - masscenter)/length
  312.        
  313.        
  314.         var rightThrust = Vector3()
  315.         if(masscenter > (length/2)):
  316.                 rightThrust =  _side_thrust(propRight, velocity) * angle.normalized() - sin(atan(width/(2*(- length/2  + masscenter)))) * angle.normalized() * _side_thrust(propRight, velocity)
  317.         else:
  318.                 rightThrust =  _side_thrust(propRight, velocity)* angle.normalized() - cos(atan(width/(2*(- length/2  + masscenter)))) * angle.normalized() * _side_thrust(propRight, velocity)
  319.        
  320.         var leftThrust = Vector3()
  321.         if(masscenter > (length/2)):
  322.                 leftThrust =  _side_thrust(propRight, velocity)*angle.normalized() - sin(atan(width/(2*(- length/2  + masscenter)))) * angle.normalized() * _side_thrust(propRight, velocity)
  323.         else:
  324.                 leftThrust =  _side_thrust(propRight, velocity)*angle.normalized() - cos(atan(width/(2*(- length/2  + masscenter)))) * angle.normalized() * _side_thrust(propRight, velocity)
  325.        
  326.         var sideLift = Vector3()
  327.         if(masscenter < length/2):
  328.                 sideLift = (subMass * 9.81 * 2 * (masscenter - length/2) / length) * Vector3(0,1,0)
  329.         else:
  330.                 sideLift = (subMass * 9.81 * 2 * (masscenter - length/2) / length) * -1 * Vector3(0,1,0)
  331.        
  332.         var rudderDragLift = (-(_rudder_drag(velocity, rudderOrient) + _rudder_lift(velocity, rudderAngle, angleto, angle)) - (cos(rudderAngle) * (_rudder_drag(velocity, rudderOrient) + _rudder_lift(velocity, rudderAngle, angleto, angle))))*normalAngle.normalized()
  333.        
  334.        
  335.         var totmoment = Vector3()
  336.        
  337.         if(masscenter > length/2):
  338.                 totmoment += sideDrag * (masscenter - length/2)
  339.                 totmoment += rightThrust * sqrt(width *width + (masscenter - length/2)*(masscenter - length/2))
  340.                 totmoment += leftThrust * sqrt(width *width + (masscenter - length/2)*(masscenter - length/2))
  341.                 totmoment += sideLift *( masscenter - length/2)
  342.                 totmoment += rudderDragLift * masscenter
  343.         else:
  344.                 totmoment += sideDrag * (length/2 - masscenter)
  345.                 totmoment += rightThrust * sqrt(width *width + (length/2 - masscenter)*(length/2 - masscenter))
  346.                 totmoment += leftThrust * sqrt(width *width + (length/2 - masscenter)*(length/2 - masscenter))
  347.                 totmoment += sideLift * (length/2 - masscenter)
  348.                 totmoment += rudderDragLift * masscenter
  349.                
  350.         var inertia = (masscenter * subMass  / length + backBallastMass) * masscenter * masscenter + ((length - masscenter) * subMass  / length + frontBallastMass) * (length - masscenter)* (length - masscenter)
  351.        
  352.         var accel = inertia/totmoment.length()
  353.        
  354.         phi = angularv * delta + 0.5 * accel * delta * delta
  355.        
  356.         angularv = accel * delta
  357.        
  358.         _rotate(angle, angle * (length-masscenter)/length + totmoment.normalized() * tan(phi) * (length-masscenter))
  359.         look_at(angle, angle * (length-masscenter)/length + totmoment.normalized() * tan(phi) * (length-masscenter))
  360.    
  361.         var totRotForce = Vector3(0,0,0)
  362.         var tot2dforce = Vector2(0,0)
  363.    
  364.        
  365.         emit_signal("energy_consumption", str(energyconsumption + abs(0.405 * prop1Thrust + 0.405 * propRight + 0.405 * propLeft)*delta))
  366.         energyconsumption += abs(0.405 * prop1Thrust + 0.405 * propRight + 0.405 * propLeft)*delta

Reply to "a1"

Here you can reply to the paste above