Beispiel #1
0
        public void CreateContactPoint(Vector3 contactPos)
        {
            var parts = servo.HostPart.FindChildParts <Part>(true);

            footParts = new List <Part>();
            foreach (var part in parts)
            {
                footParts.Add(part);
                Debug.Log(name + " attatched foot parts " + part.name);
            }

            contactPoint = new GameObject().transform;
            contactPoint.SetParent(transform);
            contactPoint.localPosition = contactPos - new Vector3(0, .4f, 0);
            //contactPoint.localPosition = Vector3.zero;
            contactPoint.rotation = memoryBridge.vesselControl.gimbal.rotation;
            DebugVector.DrawVector(contactPoint);


            footRenderer = contactPoint.gameObject.AddComponent <LineRenderer>();
            Material redMat = new Material(Shader.Find("Transparent/Diffuse"));

            redMat.color          = Color.red;
            footRenderer.material = redMat;
            footRenderer.SetWidth(.1f, .1f);
        }
        public void BuildCameraFeed(MemoryBridge memoryBridge, string fileName)
        {
            //Debug.Log("Build Camera Feed");
            this.memoryBridge = memoryBridge;
            this.fileName     = fileName;

            threadList = new List <int>();
            threadList.Add(0);
            threadList.Add(1);
            //   threadList.Add(2);
            //  threadList.Add(3);

            GameObject camObject = new GameObject();

            DebugVector.DrawVector(camObject.transform);
            camObject.transform.SetParent(FlightGlobals.ActiveVessel.vesselTransform);

            feedCam = camObject.AddComponent <Camera>();

            rendTextHeight = feedCam.pixelHeight;
            rendTextWidth  = feedCam.pixelWidth;
            //Debug.Log("height " + rendTextHeight + " width " + rendTextWidth);
            rendText = new RenderTexture(rendTextWidth, rendTextHeight, 16, RenderTextureFormat.ARGB32);
            rendText.Create();
            feedCam.targetTexture = rendText;

            tex     = new Texture2D(rendTextWidth, rendTextWidth, TextureFormat.RGB24, false);
            texCopy = new Texture2D(rendTextWidth, rendTextHeight, TextureFormat.RGB24, false);

            RenderTexture.active = rendText;
            //Debug.Log("Render width " + rendTextWidth);
            //Debug.Log("render height " + rendTextHeight);
            tex.ReadPixels(new Rect(0, 0, rendTextWidth, rendTextHeight), 0, 0);
            tex.Apply();
            camTexture = tex;
            // var colorBuffer = rendText.colorBuffer;
            var textureByte = camTexture.GetRawTextureData();

            camFeedFile = MemoryMappedFile.Create(MapProtection.PageReadWrite, textureByte.Length, "CamFeedFile" + fileName + memoryBridge.cameraFeeds.Count);

            memoryBridge.SetFloat("rendTextHeight" + fileName + memoryBridge.cameraFeeds.Count, rendTextHeight);
            memoryBridge.SetFloat("rendTextWidth" + fileName + memoryBridge.cameraFeeds.Count, rendTextWidth);
            memoryBridge.SetFloat("feedByteCount" + fileName + memoryBridge.cameraFeeds.Count, textureByte.Length);

            ////Debug.Log("ByteSize " + textureByte.Length);
            //Debug.Log("mem map loaded");
        }
Beispiel #3
0
        public void CreateContactPoint()
        {
            // var joint = servo.HostPart.gameObject.AddComponent<SpringJoint>();

            contactPoint = new GameObject().transform;
            contactPoint.SetParent(transform);
            // contactPoint.localPosition = localPos;
            contactPoint.localPosition = Vector3.zero;
            contactPoint.rotation      = memoryBridge.vesselControl.gimbal.rotation;
            DebugVector.DrawVector(contactPoint);

            footRenderer = contactPoint.gameObject.AddComponent <LineRenderer>();
            Material redMat = new Material(Shader.Find("Transparent/Diffuse"));

            redMat.color          = Color.red;
            footRenderer.material = redMat;
            footRenderer.SetWidth(.1f, .1f);
        }
Beispiel #4
0
        void CheckVesselClearance()
        {
            LayerMask mask = (1 << 0) | (1 << 10);

            mask = ~mask;
            RaycastHit hit;

            Vector3 rayRot    = -gimbal.up;// -controller.launchVector.trans.up;
            float   clearance = 0;

            if (Physics.Raycast(vessel.vesselTransform.position, rayRot, out hit, 100, mask))
            {
                clearance = hit.distance;
            }
            if (!groundPoint)
            {
                groundPoint = new GameObject().transform;
                DebugVector.DrawVector(groundPoint);
            }
            groundPoint.transform.position = vessel.vesselTransform.position - new Vector3(0, clearance, 0);
        }
Beispiel #5
0
        public void CustomStart(IRWrapper.IServo servo, MemoryBridge memoryBridge)
        {
            this.servo              = servo;
            this.servo.Speed        = 20;
            this.servo.Acceleration = 20;
            servoName = servo.Name + servo.HostPart.gameObject.GetInstanceID();
            part      = servo.HostPart;

            this.memoryBridge = memoryBridge;

            memoryBridge.SetFloat(servoName + "unityServoPos", servo.Position);

            memoryBridge.SetFloat(servoName + "minPos", servo.MinPosition);
            Debug.Log("servo min pos " + servo.MinPosition);
            memoryBridge.SetFloat(servoName + "maxPos", servo.MaxPosition);
            Debug.Log("servo max pos " + servo.MaxPosition);

            // memoryBridge.SetFloat(servoName + "ServoSetPos", 210f);
            //  memoryBridge.SetFloat(servoName + "ServoSetSpeed", 210f);

            //search the parent part for a transform named "Base"
            var partParent     = part.parent;
            var parentChildren = partParent.GetComponentsInChildren <Transform>();
            var bases          = new List <Transform>();

            foreach (var child in parentChildren)
            {
                if (child.name.ToLower().Contains("base"))
                {
                    bases.Add(child);
                }
            }

            Debug.Log("Bases count " + bases.Count);
            if (bases.Count == 0)
            {
                Debug.Log(servo.Name + " does not have a base");
            }
            else
            {
                //If there are multiple bases on the parent, use the closest one
                if (bases.Count > 1)
                {
                    var minDist = Mathf.Infinity;
                    foreach (var basePart in bases)
                    {
                        var dist = Vector3.Distance(transform.position, basePart.position);
                        if (dist < minDist)
                        {
                            servoBase = basePart;
                            minDist   = dist;
                        }
                    }
                }
                else
                {
                    servoBase = bases[0];
                }

                var newObject = new GameObject();
                newObject.name = "Servo Anchor";
                servoAnchor    = newObject.transform;
                servoAnchor.SetParent(servoBase);
                servoAnchor.localPosition    = Vector3.zero;
                servoAnchor.localEulerAngles = Vector3.zero;

                newObject      = new GameObject();
                newObject.name = "Servo Child";
                anchorChild    = newObject.transform;
                anchorChild.SetParent(servoAnchor);
                anchorChild.localPosition = Vector3.zero;
                anchorChild.rotation      = part.transform.rotation;

                Debug.Log(this.servo.HostPart.name);

                //Move the x axis of the rotatron part. This happens in Unity script as well in Parts
                if (this.servo.HostPart.name == "IR.Rotatron.Basic.v3")
                {
                    servoAnchor.localEulerAngles += new Vector3(0, 0, 90);
                    anchorChild.localEulerAngles += new Vector3(0, 0, 90);
                    // DebugVector.DrawVector(servoAnchor);
                    //  DebugVector.DrawVector(anchorChild);
                    Debug.Log("Draw Vector");
                }

                DebugVector.DrawVector(servoAnchor);
            }
        }
Beispiel #6
0
        public void CheckFootClearance()
        {
            LayerMask mask = (1 << 0) | (1 << 10);

            mask = ~mask;
            RaycastHit hit;

            //Vector3 rayRot = -hipRotServo.transform.right;
            contactPoint.rotation = memoryBridge.vesselControl.gimbal.rotation;

            Vector3 rayRot    = -contactPoint.up;// -controller.launchVector.trans.up;
            float   clearance = 0;

            if (Physics.Raycast(contactPoint.position, rayRot, out hit, 100, mask))
            {
                // if (hit.collider.gameObject.tag != "Runway")
                //    Debug.Log("hitting " + hit.collider.gameObject.name + " at layer " + hit.collider.gameObject.layer.ToString() + " with tag " + hit.collider.gameObject.tag.ToString() + " at distance " + hit.distance);
                // if (hit.collider.gameObject.name == "Kerbin Zn123222323")
                //     controller.walking = false;
                footRenderer.SetPosition(0, contactPoint.position);
                footRenderer.SetPosition(1, hit.point);
                clearance = hit.distance;
            }

            bool groundContact = false;

            foreach (var part in footParts)
            {
                if (part.GroundContact)
                {
                    if (!footPart)
                    {
                        footPart = part;
                        footBody = footPart.Rigidbody;
                        footPart.OnJustAboutToBeDestroyed += PartAboutToBeDestroyed;
                        memoryBridge.SetBool(servoName + "exploded", false);
                    }

                    groundContact = true;

                    var closestPoint = part.collider.ClosestPoint(memoryBridge.vesselControl.vessel.mainBody.transform.position);

                    if (!collisionPoint)
                    {
                        collisionPoint = new GameObject().transform;

                        DebugVector.DrawVector(collisionPoint);
                    }
                    collisionPoint.position = closestPoint;

                    var collisionPointOffset = transform.InverseTransformPoint(collisionPoint.position);
                    memoryBridge.SetVector3(servoName + "CollisionPoint", collisionPointOffset);
                }
            }
            if (footPart)
            {
                memoryBridge.SetVector3(servoName + "torque", footBody.velocity);
                memoryBridge.SetFloat(servoName + "explosionPotential", footPart.explosionPotential);
                memoryBridge.SetFloat(servoName + "gExplodeChance", footPart.gExplodeChance);
                memoryBridge.SetBool(servoName + "active", footPart.gameObject.activeInHierarchy);

                // var events = footPart.Events;
                // events.

                // footPart.explosionPotential;
                // footPart.gExplodeChance()
            }
            // if(servo.HostPart.GroundContact != groundContact)
            //  {
            //     groundContact = servo.HostPart.GroundContact;
            memoryBridge.SetBool(servoName + "GroundContact", groundContact);
            // }
            memoryBridge.SetFloat(servoName + "KSPFootClearance", clearance);
        }