Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- //
- class CBP55_Euler_ProjectileArrow extends CBP55_Actor ClassGroup(CBP55) placeable;
- //
- // CBPrice
- // Orig 23-09-11.
- // Rev1 10-10-11
- // Status Release.
- //
- // Rotating projectile (eg arrow or rocket) which is confined to the Y-Z plane.
- // Uses Euler solver with specified deltaT.
- //
- //=============================================================================
- //variables*********************************************************************
- //static mesh components that make up this model--------------------------------
- var StaticMeshComponent BasicArrow;
- // Editable variables ----------------------------------------------------------
- // These are normally the parameters of the model and the model's initial
- // conditions. These should have default properties assigned.
- // Parameters
- var(CBP55) float gravityZ;
- var(CBP55) float mmass;
- var(CBP55) float drag;
- // Initial Conditions
- var(CBP55) float initSpeed;
- var(CBP55) float initTheta;
- // Euler Integrator control
- var(CBP55) float deltaT;
- // Internal variables ----------------------------------------------------------
- // These are the "state" variables and may be the "output" variables, and also
- // any internal constants (ie "numbers" which are required but which do not
- // change. +
- // Internal "state" variables
- var float velyY;
- var float dispY;
- var float velyZ;
- var float dispZ;
- var float accelZ;
- var float accelY;
- var float forceZ;
- var float forceY;
- var float speed;
- var float dragY;
- var float dragZ;
- var float time;
- //var float theta;
- // Internal Constants. These may be used for conversions between units.
- var float scaling;
- // functions********************************************************************
- // Initialisation of this actor. do not change this. ---------------------------
- function Actor_Initialize(PlayerController newPlayer){
- bRunning = false;
- super.Actor_Initialize(newPlayer);
- setLogFileColumnLabels();
- setInitialConditions();
- visualization();
- logDataRecord();
- setTimer(deltaT,true,'computeTimer');
- setTimer(logInterval,true,'logTimer');
- }
- // Sets the initial values for this actor. You will need to write this code ----
- function setInitialConditions() {
- time = 0;
- velyY = initSpeed*(sin(initTheta*pi/180));
- velyZ = initSpeed*(cos(initTheta*pi/180));
- }
- // Resets the actor to its initial location and/or rotation and resets the
- // initial values of the "state" variables.
- function reset() {
- initializeVariables(); // set the variables to zeros
- setInitialConditions(); // set init conds of variables which need it
- visualization();
- }
- // Here you must set all internal "state" variables to zero.
- function initializeVariables() {
- dispY = 0;
- velyY = 0;
- dispZ = 0;
- velyZ = 0;
- time = 0;
- }
- // Sets the column labels in the data log file. One line of code for each label.
- function setLogFileColumnLabels() {
- local array<String> columnLabels;
- columnLabels.length = 0; //empty array.
- columnLabels[0] = "time";
- columnLabels[1] = "dispY";
- columnLabels[2] = "dispZ";
- columnLabels[3] = "solnZ";
- columnLabels[4] = "error";
- writeLogFileHeader(columnLabels);
- }
- // Here you specify which VARIABLES will be logged. This should agree with your
- // column labels.
- function logDataRecord() {
- local array<float> data${1}< ${3} >
- local float solnZnoDrag;
- local float errorZnoDrag;
- dataArray.length = 0; //empty array.
- dataArray[0] = time;
- dataArray[1] = dispY;
- dataArray[2] = dispZ;
- calcSolnZNoDrag(solnZnoDrag,errorZnoDrag);
- dataArray[3] = solnZnoDrag;
- dataArray[4] = errorZnoDrag;
- writeLogFileRecord(dataArray);
- }
- function logTimer() {
- if(bRunning) logDataRecord();
- }
- // ============================ CVI ============================================
- function computeTimer(){ //-----------------------------------------------------
- if(bRunning) {
- computation(deltaT);
- visualization();
- }
- }
- // COMPUTATION
- // Here you will write your code based on your mathematical model --------------
- function computation(float dT) {
- speed = sqrt(velyY**2 + velyZ**2);
- dragY = -drag*velyY*speed;
- dragZ = -drag*velyZ*speed;
- forceZ = -1.0*mmass*gravityZ + dragZ;
- forceY = 0.0+ dragY;
- accelZ = forceZ/mmass;
- accelY = forceY/mmass;
- velyZ += accelZ*dT;
- velyY += accelY*dT;
- dispZ += velyZ*dT;
- dispY += velyY*dT;
- time += dT;
- if(dispZ < 0.0) bRunning = false;
- }
- // VISUALISATION
- // Here you will write your code to visualise your variables -------------------
- function Visualization(){
- local vector newPos;
- local rotator newRot;
- local float theta;
- newPos = BasicArrow.default.Translation;
- newRot = BasicArrow.default.Rotation;
- newPos.Y = newPos.Y + (dispY * scaling);
- newPos.Z = newPos.Z + (dispZ * scaling);
- BasicArrow.SetTranslation(newPos);
- // get the angle from the velocity.
- theta = atan2(velyY,velyZ);
- newRot.Roll = newRot.Roll + 65536*theta/(2*pi);
- BasicArrow.setRotation(newRot);
- }
- // INTERACTION
- // used to display PARAMETERS or VARIABLES on the HUD --------------------------
- // Here you will choose which parameters or variables will be displayed on the
- // HUD. Note that the zero'th line is reserved to display the running status of
- // the actor.
- function array<string> SendValuesToHUD(){
- local array<string> HUDLine;
- HUDLine.length = 0; //empty array.
- HUDLine[0] = "Running: "@bRunning;
- HUDLine[1] = "Time: "@time;
- HUDLine[2] = "drag: "@drag;
- HUDLine[3] = "theta: "@InitTheta;
- HUDLine[4] = "speed: "@InitSpeed;
- HUDLine[5] = "velyY: "@velyY;
- HUDLine[6] = "velyZ: "@velyY;
- HUDLine[7] = "dispZ: "@dispZ;
- HUDLine[8] = "dispY; "@dispY;
- return HUDLine;
- }
- // INTERACTION
- // Here you will write code to be able to change the values of parameters from
- // the command line.
- function SetParam(string paramName,float paramValue){
- WorldInfo.Game.Broadcast(self,paramName@paramValue);
- paramName = Caps(paramName); //capitalize so case insensitive
- if(paramName == Caps("gravityZ")){
- gravityZ = paramValue;
- }
- else if(paramName == Caps("mmass")){
- mmass = paramValue;
- }
- else if(paramName == Caps("theta")){
- initTheta = paramValue;
- setInitialConditions(); // set init conds of variables which need it
- visualization();
- }
- else if(paramName == Caps("speed")){
- initSpeed = paramValue;
- setInitialConditions(); // set init conds of variables which need it
- visualization();
- }
- }
- // INTERACTION
- // Process key presses for this actor-------------------------------------------
- // Here you can assign various keys to produce any action you like. Each key is
- // associated with a function, or global variable.
- function ProcessKey(int controllerID,name key,EInputEvent eventType){
- //WorldInfo.Game.Broadcast(self,"key pressed was:"@key); //displays key pressed
- if(eventType == IE_Released){
- if(Key == 'F1'){ //toggles bRunning (global variable)
- bRunning = !bRunning;
- }
- if(Key == 'F2'){
- reset(); // calls function to reset to initial conditions etc.
- }
- if(Key == 'F3'){
- pc.ToggleHUD(); //calls function on playerController to toggle HUD.
- }
- }
- }
- // Helper Functions for this experiment Actor **********************************
- // calculates the mathematical analytical solution and the error with the
- // numerical solution.
- function calcSolnZNoDrag(out float soln, out float error) {
- soln = initSpeed*cos(initTheta*pi/180)*time - 0.5*gravityZ*time*time;
- error = soln - dispZ;
- }
- defaultproperties //***********************************************************
- {
- // ----------------------------------------------------------------------------
- Begin Object class=StaticMeshComponent Name=StaticMeshComponent0
- StaticMesh=StaticMesh'CBP55_Assets.GeometricalObjects.Cylinder'
- LightEnvironment=MyLightEnvironment
- bUsePrecomputedShadows=FALSE
- End Object
- Components.Add(StaticMeshComponent0)
- //Set a reference to the above static mesh component.
- BasicArrow = StaticMeshComponent0
- // -----------------------------------------------------------------------------
- //enable collisions for actor (including traces)
- bCollideActors=true
- bBlockActors=true
- // -----------------------------------------------------------------------------
- //custom var defaults
- bRunning=true
- gravityZ=-9.81
- mmass=1.0
- initSpeed=20.0
- initTheta=30
- scaling=52.5
- deltaT=0.01
- // -----------------------------------------------------------------------------
- }
Add Comment
Please, Sign In to add comment