Guest User

Untitled

a guest
May 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. //
  3. class CBP55_Euler_ProjectileArrow extends CBP55_Actor ClassGroup(CBP55) placeable;
  4. //
  5. // CBPrice
  6. // Orig 23-09-11.
  7. // Rev1 10-10-11
  8. // Status Release.
  9. //
  10. // Rotating projectile (eg arrow or rocket) which is confined to the Y-Z plane.
  11. // Uses Euler solver with specified deltaT.
  12. //
  13. //=============================================================================
  14.  
  15. //variables*********************************************************************
  16.  
  17. //static mesh components that make up this model--------------------------------
  18.  
  19. var StaticMeshComponent BasicArrow;
  20.  
  21. // Editable variables ----------------------------------------------------------
  22. // These are normally the parameters of the model and the model's initial
  23. // conditions. These should have default properties assigned.
  24.  
  25. // Parameters
  26. var(CBP55) float gravityZ;
  27. var(CBP55) float mmass;
  28. var(CBP55) float drag;
  29.  
  30. // Initial Conditions
  31. var(CBP55) float initSpeed;
  32. var(CBP55) float initTheta;
  33.  
  34. // Euler Integrator control
  35. var(CBP55) float deltaT;
  36.  
  37. // Internal variables ----------------------------------------------------------
  38. // These are the "state" variables and may be the "output" variables, and also
  39. // any internal constants (ie "numbers" which are required but which do not
  40. // change.                                        +
  41.  
  42. // Internal "state" variables
  43. var float velyY;
  44. var float dispY;
  45. var float velyZ;
  46. var float dispZ;
  47. var float accelZ;
  48. var float accelY;
  49. var float forceZ;
  50. var float forceY;
  51. var float speed;
  52. var float dragY;
  53. var float dragZ;
  54.  
  55. var float time;
  56. //var float theta;
  57.  
  58. // Internal Constants. These may be used for conversions between units.
  59. var float scaling;
  60.  
  61. // functions********************************************************************
  62.  
  63. // Initialisation of this actor. do not change this. ---------------------------
  64.  
  65. function Actor_Initialize(PlayerController newPlayer){
  66.  
  67.   bRunning = false;
  68.  
  69.   super.Actor_Initialize(newPlayer);
  70.  
  71.   setLogFileColumnLabels();
  72.   setInitialConditions();
  73.   visualization();
  74.  
  75.   logDataRecord();
  76.  
  77.   setTimer(deltaT,true,'computeTimer');
  78.   setTimer(logInterval,true,'logTimer');
  79.  
  80. }
  81.  
  82. // Sets the initial values for this actor. You will need to write this code ----
  83.  
  84. function setInitialConditions() {
  85.   time = 0;
  86.   velyY = initSpeed*(sin(initTheta*pi/180));
  87.   velyZ = initSpeed*(cos(initTheta*pi/180));
  88. }
  89.  
  90. // Resets the actor to its initial location and/or rotation and resets the
  91. // initial values of the "state" variables.
  92.  
  93. function reset() {
  94.   initializeVariables();         // set the variables to zeros
  95.   setInitialConditions();        // set init conds of variables which need it
  96.   visualization();
  97. }
  98.  
  99. // Here you must set all internal "state" variables to zero.
  100.  
  101. function initializeVariables() {
  102.  
  103.   dispY = 0;
  104.   velyY = 0;
  105.   dispZ = 0;
  106.   velyZ = 0;
  107.   time = 0;
  108.  
  109. }
  110. // Sets the column labels in the data log file. One line of code for each label.
  111.  
  112. function setLogFileColumnLabels() {
  113.   local array<String> columnLabels;
  114.  
  115.   columnLabels.length = 0; //empty array.
  116.  
  117.   columnLabels[0] = "time";
  118.   columnLabels[1] = "dispY";
  119.   columnLabels[2] = "dispZ";
  120.   columnLabels[3] = "solnZ";
  121.   columnLabels[4] = "error";
  122.  
  123.   writeLogFileHeader(columnLabels);
  124. }
  125.  
  126. // Here you specify which VARIABLES will be logged. This should agree with your
  127. // column labels.
  128.  
  129. function logDataRecord() {
  130.  
  131.   local array<float> data${1}< ${3} >
  132.  
  133.   local float solnZnoDrag;
  134.   local float errorZnoDrag;
  135.  
  136.   dataArray.length = 0; //empty array.
  137.  
  138.   dataArray[0] = time;
  139.   dataArray[1] = dispY;
  140.   dataArray[2] = dispZ;
  141.  
  142.   calcSolnZNoDrag(solnZnoDrag,errorZnoDrag);
  143.  
  144.   dataArray[3] = solnZnoDrag;
  145.   dataArray[4] = errorZnoDrag;
  146.  
  147.   writeLogFileRecord(dataArray);
  148.  
  149. }
  150.  
  151. function logTimer() {
  152.  
  153.   if(bRunning) logDataRecord();
  154.  
  155. }
  156.  
  157. // ============================ CVI ============================================
  158.  
  159.  
  160. function computeTimer(){ //-----------------------------------------------------
  161.  
  162.  if(bRunning) {
  163.    computation(deltaT);
  164.    visualization();
  165.  }
  166.  
  167. }
  168.  
  169. // COMPUTATION
  170.  
  171. // Here you will write your code based on your mathematical model --------------
  172.  
  173. function computation(float dT) {
  174. speed = sqrt(velyY**2 + velyZ**2);
  175.         dragY = -drag*velyY*speed;
  176.         dragZ = -drag*velyZ*speed;
  177.  
  178.         forceZ = -1.0*mmass*gravityZ + dragZ;
  179.         forceY = 0.0+ dragY;
  180.  
  181.         accelZ = forceZ/mmass;
  182.         accelY = forceY/mmass;
  183.  
  184.         velyZ += accelZ*dT;
  185.         velyY += accelY*dT;
  186.  
  187.         dispZ += velyZ*dT;
  188.         dispY += velyY*dT;
  189.   time += dT;
  190.  
  191.   if(dispZ < 0.0) bRunning = false;
  192.  
  193. }
  194.  
  195. // VISUALISATION
  196. // Here you will write your code to visualise your variables -------------------
  197.  
  198. function Visualization(){
  199.  
  200.   local vector newPos;
  201.   local rotator newRot;
  202.   local float theta;
  203.  
  204.   newPos =  BasicArrow.default.Translation;
  205.   newRot =  BasicArrow.default.Rotation;
  206.  
  207.   newPos.Y = newPos.Y + (dispY * scaling);
  208.   newPos.Z = newPos.Z + (dispZ * scaling);
  209.  
  210.   BasicArrow.SetTranslation(newPos);
  211.  
  212.   // get the angle from the velocity.
  213.   theta = atan2(velyY,velyZ);
  214.  
  215.   newRot.Roll = newRot.Roll + 65536*theta/(2*pi);
  216.   BasicArrow.setRotation(newRot);
  217.  
  218. }
  219.  
  220.  
  221. // INTERACTION
  222. // used to display PARAMETERS or VARIABLES on the HUD --------------------------
  223. // Here you will choose which parameters or variables will be displayed on the
  224. // HUD. Note that the zero'th line is reserved to display the running status of
  225. // the actor.
  226.  
  227. function array<string> SendValuesToHUD(){
  228.   local array<string> HUDLine;
  229.  
  230.   HUDLine.length = 0; //empty array.
  231.  
  232.   HUDLine[0] = "Running: "@bRunning;
  233.   HUDLine[1] = "Time: "@time;
  234.   HUDLine[2] = "drag: "@drag;
  235.   HUDLine[3] = "theta: "@InitTheta;
  236.   HUDLine[4] = "speed: "@InitSpeed;
  237.   HUDLine[5] = "velyY: "@velyY;
  238.   HUDLine[6] = "velyZ: "@velyY;
  239.   HUDLine[7] = "dispZ: "@dispZ;
  240.   HUDLine[8] = "dispY; "@dispY;
  241.  
  242.   return HUDLine;
  243. }
  244.  
  245. // INTERACTION
  246. // Here you will write code to be able to change the values of parameters from
  247. // the command line.
  248.  
  249. function SetParam(string paramName,float paramValue){
  250.   WorldInfo.Game.Broadcast(self,paramName@paramValue);
  251.  
  252.   paramName = Caps(paramName); //capitalize so case insensitive
  253.  
  254.   if(paramName == Caps("gravityZ")){
  255.     gravityZ = paramValue;
  256.   }
  257.   else if(paramName == Caps("mmass")){
  258.     mmass = paramValue;
  259.   }
  260.   else if(paramName == Caps("theta")){
  261.     initTheta = paramValue;
  262.     setInitialConditions();        // set init conds of variables which need it
  263.     visualization();
  264.   }
  265.   else if(paramName == Caps("speed")){
  266.     initSpeed = paramValue;
  267.     setInitialConditions();        // set init conds of variables which need it
  268.     visualization();
  269.   }
  270. }
  271.  
  272. // INTERACTION
  273. // Process key presses for this actor-------------------------------------------
  274. // Here you can assign various keys to produce any action you like. Each key is
  275. // associated with a function, or global variable.
  276.  
  277. function ProcessKey(int controllerID,name key,EInputEvent eventType){
  278.   //WorldInfo.Game.Broadcast(self,"key pressed was:"@key);  //displays key pressed
  279.  
  280.   if(eventType == IE_Released){
  281.     if(Key == 'F1'){    //toggles bRunning (global variable)
  282.       bRunning = !bRunning;
  283.     }
  284.     if(Key == 'F2'){
  285.       reset();      // calls function to reset to initial conditions etc.
  286.     }
  287.     if(Key == 'F3'){
  288.       pc.ToggleHUD();  //calls function on playerController to toggle HUD.
  289.     }
  290.   }
  291. }
  292.  
  293. // Helper Functions for this experiment Actor **********************************
  294.  
  295. // calculates the mathematical analytical solution and the error with the
  296. // numerical solution.
  297.  
  298. function  calcSolnZNoDrag(out float soln, out float error) {
  299.  
  300.   soln = initSpeed*cos(initTheta*pi/180)*time - 0.5*gravityZ*time*time;
  301.   error = soln - dispZ;
  302.  
  303. }
  304.  
  305. defaultproperties  //***********************************************************
  306. {
  307.  
  308.  // ----------------------------------------------------------------------------
  309.  
  310.   Begin Object class=StaticMeshComponent Name=StaticMeshComponent0
  311.     StaticMesh=StaticMesh'CBP55_Assets.GeometricalObjects.Cylinder'
  312.     LightEnvironment=MyLightEnvironment
  313.     bUsePrecomputedShadows=FALSE
  314.   End Object
  315.   Components.Add(StaticMeshComponent0)
  316.  
  317.   //Set a reference to the above static mesh component.
  318.   BasicArrow = StaticMeshComponent0
  319.  
  320. // -----------------------------------------------------------------------------
  321.  
  322.   //enable collisions for actor (including traces)
  323.   bCollideActors=true
  324.   bBlockActors=true
  325.  
  326. // -----------------------------------------------------------------------------
  327.  
  328.   //custom var defaults
  329.   bRunning=true
  330.   gravityZ=-9.81
  331.   mmass=1.0
  332.   initSpeed=20.0
  333.   initTheta=30
  334.   scaling=52.5
  335.   deltaT=0.01
  336.  
  337. // -----------------------------------------------------------------------------
  338.  
  339. }
Add Comment
Please, Sign In to add comment