I have a script that writes and reads lines from a file, consisting of A,B,C,D positions.
Now as far as writing goes, I output these 4 positions every line to a file.
So the format per line will be: A"float"B"float"C"float"D"float" or: A-39.50768B78.55463C44.15681D-122.7114
when reading the file, i put each line into an array as a string, so that i can access each string at any time i need.
Now i need to have those positions read to apply them to a model.
So what i’m looking for is to read the string, check for the character A, then put the numbers after that to a array of A positions, then the same for B and so on…
So the question is… how do i do this…
i’ve been looking around, but couldn’t find a situation like this.
Here’s my code, it might be a bit messy as i do almost everything in one file so far, but the saving and reading functions are at the bottom.
//public var GUIObj : GameObject;
//public var guiScr = GameObject.GetComponent(GUIObj, "rotateobjec");
//public var guiScr = GUIObj.GetComponent(rotateobjec);
//Arm variables
import System;
import System.IO;
public var MakeLine = false;
public var MakeFile = false;
public var base_height:float = 50.0;
public var arm_1_length:float = 100.0;
public var arm_2_length:float = 100.0;
public var grip_length:float = 60.0;
public var grip_angle_deg:float = 0.0;
public var grip_angle_deg_02:float = 0.0;
//Goal Position
public var target : GameObject;
public var xPos = 0.0;
public var yPos = 0.0;
public var zPos = 0.0;
//ArmRotations
public var Angle1 = 0.0;
public var Angle2 = 0.0;
public var Angle3 = 0.0;
public var Angle4 = 0.0;
//attach script on all joints, in the editor select true or false for the joint
public var applyAngle1 = true;
public var applyAngle2 = false;
public var applyAngle3 = false;
public var applyAngle4 = false;
public var applyAngle5 = false;
public var distance_offset = 4.6;
//define offsets
public var grip_offset_t = 10;
public var arm1_angle_offset = 90;
public var arm2_angle_offset = 180;
public var TargetOffsetX:float = 0.0;
public var TargetOffsetZ:float = 0.0;
public var XR = 0.0;
public var YR = 0.0;
public var D = 0.0;
//error detect vars
public var maxDist = 0;
public var showAlert = false;
var ALines = new Array ();
var BLines = new Array ();
var CLines = new Array ();
var DLines = new Array ();
public var ReadLines = new Array ();
var fileName = "MyFile.txt";
var openFile = false;
function Start () {maxDist=arm_1_length+arm_2_length+grip_length;}
function Update () {
if(openFile)
{
OpenFile(fileName);
openFile = false;
}
if(MakeFile)
{
SaveLines();
MakeFile = false;
}
var targetPos = target.transform.position;
zPos = targetPos.z;
yPos = targetPos.y;
xPos = targetPos.x;
//guiScr
// grip_angle_deg = guiScr.hSliderValue0;
// grip_angle_deg_02 = guiScr.hSliderValue1;
//DoIK(zPos-TargetOffsetZ,xPos-TargetOffsetX,yPos,grip_angle_deg);
ErrorCheck(zPos-TargetOffsetZ,xPos-TargetOffsetX,yPos,grip_angle_deg);
//guiScr = GUIObj.GetComponent(rotateobjec);
}
function ErrorCheck (x:float,y:float,z:float,grip_angle_deg:float)
{
XR = Mathf.Sqrt((x * x) + (y * y));
YR = Mathf.Sqrt((x * x) + (z * z));
var ZR = 0;
if(XR < maxDist)
{
DoIK(zPos-TargetOffsetZ,xPos-TargetOffsetX,yPos,grip_angle_deg);
}
else{ showAlert = true;}
}
function DoIK (x:float,y:float,z:float,grip_angle_deg:float)
{
//precalculation
var arm1sq = arm_1_length * arm_1_length;
var arm2sq = arm_2_length * arm_2_length;
//convert grip angle degrees to radians for use in calculations
var grip_angle_rad = grip_angle_deg * Mathf.Deg2Rad;
//calculate base angle
var base_angle_rad = Mathf.Atan2(x, y);
//calculate new distance
D = Mathf.Sqrt((x * x) + (y * y));
//calculate d and z offset based on grip angle and length
var z_off = Mathf.Asin(grip_angle_rad) * grip_length;
var d_off = Mathf.Acos(grip_angle_rad) * grip_length;
//error checking(needs work)
if(d_off < maxDist)
{
//get wrist position
var wrist_z = (z - z_off) - base_height;
var wrist_d = (D - d_off) + distance_offset;
//calculate arm angles using inverse kinematics
var b_s = (wrist_z * wrist_z) + (wrist_d * wrist_d);
var b_r = Mathf.Sqrt(b_s);
var q1 = Mathf.Atan2(wrist_z, wrist_d);
var q2 = Mathf.Acos(((arm1sq - arm2sq) + b_s) / (2 * arm_1_length * b_r));
var angle_1 = q1 + q2;
var angle_2 = Mathf.Acos((arm1sq + arm2sq - b_s) / (2 * arm_1_length * arm_2_length));
var angle_3 = grip_angle_rad - angle_1 - angle_2;
}
//set radians back to degrees!
Angle1 = base_angle_rad * Mathf.Rad2Deg;
Angle2 = angle_1 * Mathf.Rad2Deg;
Angle3 = angle_2 * Mathf.Rad2Deg;
Angle4 = angle_3 * Mathf.Rad2Deg;
if(MakeLine)
{
MakeLines(Angle1,Angle2,Angle3,Angle4);
MakeLine = false;
}
//display angles to model
if(float.IsNaN(Angle1) || float.IsNaN(Angle2) || float.IsNaN(Angle3) || float.IsNaN(Angle4))
{
showAlert = true;
}
else{
ApplyToModel(Angle1,Angle2,Angle3,Angle4);
showAlert = false;
}
}
function ApplyToModel (angle1:float,angle2:float,angle3:float,angle4:float)
{
/* make euler variables */
var target1 = Quaternion.Euler (0, -angle1, 0);
var target2 = Quaternion.Euler (0, -angle1, angle2-arm1_angle_offset);
var target3 = Quaternion.Euler (0, -angle1, (angle3+(angle2-arm1_angle_offset))+arm2_angle_offset);
var target4 = Quaternion.Euler (grip_angle_deg_02, -angle1, (angle4+(angle3+(angle2-90)))+grip_offset_t);
var target5 = Quaternion.Euler (0, grip_angle_deg_02,0);
/* apply rotation vectors to object */
if(applyAngle1){transform.rotation = target1;}
if(applyAngle4){transform.rotation = target4;}
if(applyAngle3){transform.rotation = target3;}
if(applyAngle2){transform.rotation = target2;}
if(applyAngle5){transform.localRotation = target5;}
}
function OnGUI () {
var bString = Angle1.ToString();
var a1String = Angle2.ToString();
var a2String = Angle3.ToString();
var tString = Angle4.ToString();
var objectDist = XR.ToString();
GUI.Label (Rect (Screen.width-300, 10, 100, 20), "Base Angle: ");
GUI.Label (Rect (Screen.width-200, 10, 100, 20), bString);
GUI.Label (Rect (Screen.width-300, 30, 100, 20), "Arm 1 Angle: ");
GUI.Label (Rect (Screen.width-200, 30, 100, 20), a1String);
GUI.Label (Rect (Screen.width-300, 50, 100, 20), "Arm 2 Angle: ");
GUI.Label (Rect (Screen.width-200, 50, 100, 20), a2String);
GUI.Label (Rect (Screen.width-300, 70, 100, 20), "Tool Angle: ");
GUI.Label (Rect (Screen.width-200, 70, 100, 20), tString);
GUI.Label (Rect (Screen.width-300, 90, 100, 20), "Object Distance: ");
GUI.Label (Rect (Screen.width-200, 90, 100, 20), objectDist);
if(showAlert)
{
GUI.Label (Rect ((Screen.width/2)-50, (Screen.height/2)-10, 100, 20), "ERROR");
GUI.Label (Rect ((Screen.width/2)-90, (Screen.height/2)+10, 180, 20), "ARM LIMITS REACHED");
}
}
function MakeLines (aval:float,bval:float,cval:float,dval:float)
{
//every push also extends lenght of array, push always becomes the highest number in the array
//all array's get one push a time, so all lengths are the same
ALines.Push (aval);
BLines.Push (bval);
CLines.Push (cval);
DLines.Push (dval);
print(ALines.length);
}
function SaveLines ()
{
if (File.Exists(fileName))
{
Debug.Log(fileName+" already exists.");
return;
}
var sr = File.CreateText(fileName);
for(var i = 0; ALines.length > i; i++)
{
sr.WriteLine ("A " + ALines <em>+ " B " + BLines <em>+ " C " + CLines _+ " D " + DLines*);*_</em></em>
}
sr.Close();
}
function OpenFile(file : String)
{
if(File.Exists(file))
{
var sr = File.OpenText(file);
var line = sr.ReadLine();
while(line != null)
{
ReadLines.push(line);
Debug.Log(line);
line = sr.ReadLine();
}
}else{
Debug.Log(“Could not open the file” + file);
}
}