Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main(string argument)
- {
- List<IMyTerminalBlock> panels = new List<IMyTerminalBlock>(); //Place to put panels
- GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(panels); //Get ALL THE PANELS!
- IMyTextPanel panel = panels[0] as IMyTextPanel; //Get specific panel
- List<IMyTerminalBlock> cameras = new List<IMyTerminalBlock>(); //Place to put cameras
- GridTerminalSystem.GetBlocksOfType<IMyCameraBlock>(cameras); //Get ALL THE CAMERAS!
- IMyCameraBlock camera = cameras[0] as IMyCameraBlock; //Get specific Camera
- List<IMyTerminalBlock> gyros = new List<IMyTerminalBlock>(); //Place to put gyros
- GridTerminalSystem.GetBlocksOfType<IMyGyro>(gyros); //Get ALL THE Gyros!
- IMyGyro gyro = gyros[0] as IMyGyro; //Get specific Gyro
- panel.WritePublicText("");
- stablizeAtVector(80, 80, 80, gyro, camera, panel);
- }
- void stablizeAtVector(float yaw, float pitch, float roll, IMyGyro gyro, IMyTerminalBlock frontObject, IMyTextPanel panel) //Yaw, Pitch, Roll are the desired points in degrees, gyro is gyro, and frontObject is an object in the front of the ship
- {
- List<IMyTerminalBlock> allBlocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocks(allBlocks);
- FixedVector3D rotation = VectorAngle(GetMeanCenter(allBlocks), GetBlockPosFixed(frontObject));
- AddToPanel("XRot: " + rotation.X.ToString(), panel);
- AddToPanel("YRot: " + rotation.Y.ToString(), panel);
- AddToPanel("ZRot: " + rotation.Z.ToString(), panel);
- /*
- float relYaw = yaw - (float)rotation.Y;
- AddToPanel("RelYaw: " + relYaw, panel);
- if (Math.Abs((int)rotation.Y) > 8 && Math.Abs((int)rotation.Y) < 352) //Prevent getting stuck
- {
- if ((int)Math.Abs(relYaw) >= 180) //More Efficient
- {
- relYaw = Math.Sign(relYaw) * (360 - Math.Abs(relYaw));
- }
- }
- */
- float relPitch = pitch - (float)rotation.X;
- if (Math.Abs((int)rotation.X) > 2 && Math.Abs((int)rotation.X) < 358 && Math.Abs((int)relPitch) > 2 && Math.Abs((int)relPitch) < 358) //Prevent getting stuck
- {
- if ((int)Math.Abs(relPitch) <= 180) //More Efficient
- {
- relPitch = Math.Sign(relPitch) * (360 - Math.Abs(relPitch));
- }
- setGyroPower(0, (relPitch / 360) * -6, 0, gyro);
- }
- //AddToPanel("RelYaw: " + relYaw, panel);
- AddToPanel("RelPitch: " + relPitch, panel);
- AddToPanel("XOver: " + gyro.Pitch, panel);
- AddToPanel("YOver: " + gyro.Yaw, panel);
- AddToPanel("ZOver: " + gyro.Roll, panel);
- }
- void setGyroPower(float yaw, float pitch, float roll, IMyGyro gyro) //-6 to 6 and everything in between
- {
- if (gyro.Yaw > yaw)
- {
- gyro.ApplyAction("DecreaseYaw");
- } else
- {
- gyro.ApplyAction("IncreaseYaw");
- }
- if (gyro.Pitch > pitch)
- {
- gyro.ApplyAction("DecreasePitch");
- }
- else
- {
- gyro.ApplyAction("IncreasePitch");
- }
- if (gyro.Roll > roll)
- {
- gyro.ApplyAction("DecreaseRoll");
- }
- else
- {
- gyro.ApplyAction("IncreaseRoll");
- }
- }
- FixedVector3D VectorAngle(FixedVector3D pointA, FixedVector3D pointB)
- {
- FixedVector3D extPoint = new FixedVector3D(pointB.X - pointA.X, pointB.Y - pointA.Y, pointB.Z - pointA.Z);
- FixedVector3D result = new FixedVector3D(0,0,0);
- result.X = RadianToDegree(Math.Atan2(extPoint.Y, extPoint.Z));
- result.Y = RadianToDegree(Math.Atan2(extPoint.X, extPoint.Z));
- result.Z = RadianToDegree(Math.Atan2(extPoint.Y, extPoint.X));
- return result;
- }
- double RadianToDegree(double angle)
- {
- return (angle * (180.0 / Math.PI)) + 180;
- }
- FixedVector3D GetBlockPosRelativeCenter(IMyTerminalBlock block) //Get the block's position relative to the mean center
- {
- List<IMyTerminalBlock> allBlocks = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocks(allBlocks);
- FixedVector3D center = GetMeanCenter(allBlocks);
- FixedVector3D blockPosWorld = GetBlockPosFixed(block);
- return new FixedVector3D(center.X - blockPosWorld.X, center.Y - blockPosWorld.Y, center.Z - blockPosWorld.Z);
- }
- string GetBlockType(IMyTerminalBlock block) //Self-Explainatory
- {
- string[] getType = block.GetType().ToString().Split('.');
- return getType[getType.Length - 1];
- }
- FixedVector3D GetMeanCenter(List<IMyTerminalBlock> blocks) //Get average center of all the blocks on-board
- {
- FixedVector3D result = new FixedVector3D(0,0,0);
- for (int i = 0; i < blocks.Count; i++)
- {
- IMyTerminalBlock block = blocks[i];
- FixedVector3D blockPos = GetBlockPosFixed(block);
- result.X += blockPos.X;
- result.Y += blockPos.Y;
- result.Z += blockPos.Z;
- }
- result.X = result.X / blocks.Count;
- result.Y = result.Y / blocks.Count;
- result.Z = result.Z / blocks.Count;
- return result;
- }
- void AddToPanel(string append, IMyTextPanel panel) //Sorta like Debug.Log() or Serial.Println()
- {
- panel.ShowPublicTextOnScreen();
- panel.WritePublicText(append + "\n", true);
- }
- FixedVector3D GetBlockPosFixed(IMyTerminalBlock block) //Get block position (Without Vector3D Bugs)
- {
- VRageMath.Vector3 blockPosition = block.GetPosition();
- string[] strSeperators = { "{X:", " Y:", " Z:", "}" };
- string[] hackPos = blockPosition.ToString().Split(strSeperators, StringSplitOptions.RemoveEmptyEntries);
- FixedVector3D hackPosition = new FixedVector3D(Convert.ToDouble(hackPos[0]), Convert.ToDouble(hackPos[1]), Convert.ToDouble(hackPos[2]));
- return hackPosition;
- }
- struct FixedVector3D { //Because Keen's Vector3/Vector3D Does not behave, here is a replacement
- public double X; public double Y; public double Z;
- public FixedVector3D(double s_X, double s_Y, double s_Z)
- {
- X = s_X; Y = s_Y; Z = s_Z;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement