//The function outputs buttons, text fields, and other interactable UI elements to the Scene in Game view
    void OnGUI()
    {
        if (manual)
        {
            //Getting the inputs from each text field and storing them as strings
            GUI.Label(new Rect(15, 75, 50, 20), "Accel");
            GUI.Label(new Rect(15, 105, 50, 20), "Angle");
            GUI.Label(new Rect(15, 135, 50, 20), "Ball position");
            GUI.Label(new Rect(15, 165, 50, 20), "Hole position");
            GUI.Label(new Rect(15, 195, 50, 20), "Distance");
            m_AccelFactorString = GUI.TextField(new Rect(100, 75, 50, 20), m_AccelFactorString, 25);
            m_AngleString       = GUI.TextField(new Rect(100, 105, 50, 20), m_AngleString, 25);
            m_BallPosString     = GUI.TextField(new Rect(100, 135, 50, 20), m_BallPosString, 25);
            m_HolePosString     = GUI.TextField(new Rect(100, 165, 50, 20), m_HolePosString, 25);
            m_DistanceString    = GUI.TextField(new Rect(100, 195, 50, 20), m_DistanceString, 25);

            //Press the button to reset the GameObject and Rigidbody
            if (GUI.Button(new Rect(10, 5, 150, 30), "Reset"))
            {
                //This switches to the start/reset case
                m_ModeSwitching = ModeSwitching.Manual;
            }

            //If you press the Start Button, switch to Force state
            if (GUI.Button(new Rect(10, 40, 150, 30), "Start"))
            {
                // remove the contstraint (stop)
                m_Rigidbody.constraints = RigidbodyConstraints.None;
                //Switch to Force (apply force to GameObject)
                m_ModeSwitching = ModeSwitching.Start;
            }
        }
    }
예제 #2
0
    // Start is called before the first frame update
    void Start()
    {
        // You get the Rigidbody component you attach to the GameObject
        m_Rigidbody2D      = GetComponent <Rigidbody2D>();
        m_CircleCollider2D = GetComponent <CircleCollider2D>();
        m_SpriteRenderer   = GetComponent <SpriteRenderer>();
        sprites            = Resources.LoadAll <Sprite>("Sprites/asteroid");

        // This starts at first mode (nothing happening yet)
        m_ModeSwitching = ModeSwitching.Impulse;

        // Initialising floats
        angle     = Random.Range(0, 2 * Mathf.PI);
        magnitude = Random.Range(MinImpulseForce, MaxImpulseForce);

        // Initialising the force which is used on GameObject in various ways
        direction = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));

        // Apply impulse force to get game object moving
        if (m_ModeSwitching == ModeSwitching.Impulse)
        {
            m_Rigidbody2D.AddForce(direction * magnitude, ForceMode2D.Impulse);
        }

        // Use System.Random.Next() to generate an integer from 0 to 4
        System.Random rnd = new System.Random();
        spriteSelected          = rnd.Next(0, 4);
        m_SpriteRenderer.sprite = sprites[spriteSelected];
        Vector2 spriteHalfSize = m_SpriteRenderer.sprite.bounds.extents;

        m_CircleCollider2D.radius = spriteHalfSize.x > spriteHalfSize.y ? spriteHalfSize.x : spriteHalfSize.y;
    }
    // Use this for initialization
    void Start()
    {
        // set input variables
        m_AccelFactorString = "150";
        m_AngleString       = "0";
        m_DistanceString    = "0";
        m_HolePosString     = "1";
        m_BallPosString     = "1";
        // get the Rigidbody component you attach to the GameObject
        m_Rigidbody = GetComponent <Rigidbody>();
        // get the renderer attached to the GameObject
        m_Renderer = GetComponent <Renderer>();
        // hide the ball unitl it's positioned
        m_Renderer.enabled = false;
        // check fps
        // Debug.Log("FPS: " + 1.0f / Time.deltaTime);

        // connect to server
        ConnectToTcpServer();

        // wait for new data to be sent by external python process
        m_ModeSwitching = ModeSwitching.Idle;

        if (manual)
        {
            m_ModeSwitching = ModeSwitching.Manual;
        }
    }
예제 #4
0
    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject == endSwingR)
        {
            m_modeSwitching = ModeSwitching.Force;
        }
        if (collision.gameObject == stoper)
        {
            if (m_modeSwitching == ModeSwitching.Start)
            {
                m_ForceX = m_ForceX + 0.3f;
                m_ForceX = m_ForceX * -1;
                m_ForceY = m_ForceY + 0.3f;
                m_ForceY = m_ForceY * -1;
                stop     = false;
                // m_modeSwitching = ModeSwitching.Force;
                //   m_modeSwitching = ModeSwitching.Force;
            }
            if (m_modeSwitching == ModeSwitching.Force)
            {
                m_modeSwitching = ModeSwitching.Start;
                stop            = true;
                //   m_NewForce = m_NewForce * -1;
                endSwingR.GetComponent <endSwing>().reverse = false;
                //   m_modeSwitching = ModeSwitching.Start;
            }
        }

        /* if (collision.gameObject.GetComponent<stoper>().returningBall == true)
         * {
         *   m_modeSwitching = ModeSwitching.Force;
         * }*/
    }
예제 #5
0
    //These are the Buttons for telling what Force to apply as well as resetting
    void OnGUI()
    {
        //If reset button pressed
        if (GUI.Button(new Rect(100, 60, 150, 30), "Reset"))
        {
            //Switch to start/reset case

            m_ModeSwitching = ModeSwitching.Start;
        }

        //Impulse button pressed
        if (GUI.Button(new Rect(100, 90, 150, 30), "Apply Impulse"))
        {
            //Switch to Impulse mode (apply impulse forces to GameObject)

            m_ModeSwitching = ModeSwitching.Impulse;
        }

        //Force Button Pressed
        if (GUI.Button(new Rect(100, 120, 150, 30), "Apply Force"))
        {
            //Switch to Force mode (apply force to GameObject)
            m_ModeSwitching = ModeSwitching.Force;
        }
    }
예제 #6
0
    void FixedUpdate()
    {
        if (m_modeSwitching != ModeSwitching.Start)
        {
            m_NewForce = new Vector3(m_ForceX, m_ForceY, 0);
        }
        switch (m_modeSwitching)
        {
        case ModeSwitching.Start:
            transform.position             = m_StartPos;
            m_Rigidbody.transform.position = m_StartForce;
            m_Rigidbody.velocity           = new Vector3(0f, 0f, 0f);
            break;



        case ModeSwitching.Force:
            // if (endSwingR.GetComponent<endSwing>().reverse == false)
            //{
            MakeCustomForce();
            m_Rigidbody.AddForce(m_NewForce, ForceMode.Force);
            break;

            /*} else
             * if (endSwingR .GetComponent<endSwing>().reverse == true)
             * {
             *
             * }*/
        }


        //reverse the swing when it collides with the box collider
        if (endSwingL.GetComponent <endSwing>().reverse == true)
        {
            m_NewForce      = new Vector3(m_ForceX, m_ForceY, 0);
            m_modeSwitching = ModeSwitching.Force;
            // StartB.GetComponent<buttionCon>().activeated = false;
            //m_Rigidbody.AddForce(m_NewForce, ForceMode.Force);
        }
        // begins the swing when the start buttion is used functional


        //stops the swinging when it hits the next ball

        /* if (SP.GetComponent<stoper>().st == true)
         * {
         *   m_modeSwitching = ModeSwitching.Start;
         *   endSwingL.GetComponent<endSwing>().reverse = false;
         * }*/
        if (stop == true)
        {
            m_modeSwitching = ModeSwitching.Start;
        }
        else if (stop == false)
        {
            m_modeSwitching = ModeSwitching.Force;
        }
    }
예제 #7
0
    void Start()
    {
        //Fetch the RigidBody component attached to the GameObject
        m_Rigidbody = GetComponent <Rigidbody2D>();
        //Start at first mode (nothing happening yet)
        m_ModeSwitching = ModeSwitching.Start;

        //Initialising the force to use on the RigidBody in various ways
        m_NewForce = new Vector2(-5.0f, 1.0f);

        //This is the RigidBody's starting position
        m_StartPosition = m_Rigidbody.transform.position;
    }
예제 #8
0
    //The function outputs buttons, text fields, and other interactable UI elements to the Scene in Game view
    void OnGUI()
    {
        //Getting the inputs from each text field and storing them as strings
        m_ForceXString = GUI.TextField(new Rect(300, 10, 200, 20), m_ForceXString, 25);
        m_ForceYString = GUI.TextField(new Rect(300, 30, 200, 20), m_ForceYString, 25);

        //Press the button to reset the GameObject and Rigidbody
        if (GUI.Button(new Rect(100, 0, 150, 30), "Reset"))
        {
            //This switches to the start/reset case
            m_ModeSwitching = ModeSwitching.Start;
        }

        //When you press the Acceleration button, switch to Acceleration mode
        if (GUI.Button(new Rect(100, 30, 150, 30), "Apply Acceleration"))
        {
            //Switch to Acceleration (apply acceleration force to GameObject)
            m_ModeSwitching = ModeSwitching.Acceleration;
        }

        //If you press the Impulse button
        if (GUI.Button(new Rect(100, 60, 150, 30), "Apply Impulse"))
        {
            //Switch to impulse (apply impulse forces to GameObject)
            m_ModeSwitching = ModeSwitching.Impulse;
        }

        //If you press the Force Button, switch to Force state
        if (GUI.Button(new Rect(100, 90, 150, 30), "Apply Force"))
        {
            //Switch to Force (apply force to GameObject)
            m_ModeSwitching = ModeSwitching.Force;
        }

        //Press the button to switch to VelocityChange state
        if (GUI.Button(new Rect(100, 120, 150, 30), "Apply Velocity Change"))
        {
            //Switch to velocity changing
            m_ModeSwitching = ModeSwitching.VelocityChange;
        }
        if (GUI.Button(new Rect(100, 150, 150, 30), "Apply Velocity shot"))
        {
            //Switch to velocity changing
            m_ModeSwitching = ModeSwitching.Velshot;
        }
        if (GUI.Button(new Rect(100, 180, 150, 30), "Relocate"))
        {
            MoveToRandomDistance();
            GetComponent <Renderer>().material = MaterialDefault;
        }
    }
예제 #9
0
 // Start is called before the first frame update
 void Start()
 {
     m_Rigidbody     = GetComponent <Rigidbody>();
     m_modeSwitching = ModeSwitching.Start;
     m_NewForce      = new Vector3(0, 0, 0);
     m_ForceX        = 1;
     m_ForceY        = 1;
     X            = m_ForceX;
     Y            = m_ForceY;
     m_StartPos   = transform.position;
     m_StartForce = m_Rigidbody.transform.position;
     SP           = stoper.GetComponent <Rigidbody>();
     ES           = endSwingR.GetComponent <Rigidbody>();
     stop         = true;
 }
 void OnTriggerEnter(Collider other)
 {
     if (other.name == "hole")
     {
         // Debug.Log ("Collision....");
         results = "0";
         // increase the mass to fall sharply
         m_Rigidbody.mass = 10;
         // fall
         this.GetComponent <Collider>().enabled = false;
         // set the flag
         hasWon = true;
         // switch to results
         m_ModeSwitching = ModeSwitching.Result;
     }
 }
예제 #11
0
 // Start is called before the first frame update
 void Start()
 {
     returningBall = false;
     GameObject.FindGameObjectWithTag("final_ball");
     m_Rigidbody     = GetComponent <Rigidbody>();
     m_modeSwitching = ModeSwitching.Start;
     m_NewForce      = new Vector3(0, 0, 0);
     m_ForceX        = 0;
     m_ForceY        = 0;
     m_ForceXString  = "1";
     m_ForceYString  = "0";
     m_StartPos      = transform.position;
     m_StartForce    = m_Rigidbody.transform.position;
     // SP = nextBall.GetComponent<Rigidbody>();
     // ES = endSwingR.GetComponent<Rigidbody>();
 }
예제 #12
0
 private void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject == endSwingL)
     {
         m_modeSwitching = ModeSwitching.Force;
     }
     if (collision.gameObject == stoper)
     {
         if (m_modeSwitching == ModeSwitching.Start)
         {
             m_ForceX = m_ForceX + 0.2f;
             m_ForceY = m_ForceY - 0.2f;
             stop     = false;
             // m_modeSwitching = ModeSwitching.Force;
             // m_modeSwitching = ModeSwitching.Force;
         }
         if (m_modeSwitching == ModeSwitching.Force)
         {
             stop = true;
             //   m_NewForce = m_NewForce * -1;
             endSwingL.GetComponent <endSwing>().reverse = false;
             m_modeSwitching = ModeSwitching.Start;
             stoper.GetComponent <stoper>().returningBall = true;
             //   m_modeSwitching = ModeSwitching.Start;
         }
         m_ForceX = m_ForceX + .01f;
         m_ForceY = m_ForceY - .01f;
     }/* else if (GetComponent<stoper>().st == false)
       * {
       *     GetComponent<stoper>().st = true;
       * }
       *
       * if (m_modeSwitching == ModeSwitching.Force)
       * {
       *     m_modeSwitching = ModeSwitching.Start;
       * }
       * if (m_modeSwitching == ModeSwitching.Force)
       * {
       *     m_modeSwitching = ModeSwitching.Start;
       * }*/
 }
예제 #13
0
    void Start()
    {
        //You get the Rigidbody component you attach to the GameObject
        m_Rigidbody = GetComponent <Rigidbody>();

        //This starts at first mode (nothing happening yet)
        m_ModeSwitching = ModeSwitching.Start;

        //Initialising the force which is used on GameObject in various ways
        m_NewForce = new Vector3(-5.0f, 1.0f, 0.0f);

        //Initialising floats
        m_ForceX = 0;
        m_ForceY = 0;

        //The forces typed in from the text fields (the ones you can manipulate in Game view)
        m_ForceXString = "0";
        m_ForceYString = "0";

        //The GameObject's starting position and Rigidbody position
        m_StartPos   = transform.position;
        m_StartForce = m_Rigidbody.transform.position;
    }
    /// Runs in background clientReceiveThread; Listens for incomming data.
    private void ListenForData()
    {
        try {
            socketConnection = new TcpClient(host, port);
            // socketConnection = new TcpClient("localhost", 8989);
            Byte[] bytes = new Byte[1024];
            while (true)
            {
                // Get a stream object for reading
                using (NetworkStream stream = socketConnection.GetStream()) {
                    int length;
                    // Read incomming stream into byte arrary.
                    while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        var incommingData = new byte[length];
                        Array.Copy(bytes, 0, incommingData, 0, length);
                        // Convert byte array to string message.
                        string serverMessage = Encoding.ASCII.GetString(incommingData);
                        // Debug.Log("User message received: " + serverMessage);
                        strArr        = serverMessage.Split(new char[] { ',' });
                        m_HolePos     = int.Parse(strArr [0]);
                        m_BallPos     = int.Parse(strArr [1]);
                        m_Angle       = float.Parse(strArr [2]);
                        m_AccelFactor = float.Parse(strArr [3]) / 10;

                        isdataArrived = true;

                        m_ModeSwitching = ModeSwitching.Socket;
                    }
                }
            }
        }
        catch (SocketException socketException) {
            Debug.Log("Socket exception: " + socketException);
        }
    }
예제 #15
0
    void FixedUpdate()
    {
        if (m_modeSwitching != ModeSwitching.Start)
        {
            m_NewForce = new Vector3(m_ForceX, m_ForceY, 0);
        }
        switch (m_modeSwitching)
        {
        case ModeSwitching.Start:
            transform.position             = m_StartPos;
            m_Rigidbody.transform.position = m_StartForce;
            m_Rigidbody.velocity           = new Vector3(0f, 0f, 0f);
            break;

        case ModeSwitching.Force:
            // if (endSwingR.GetComponent<endSwing>().reverse == false)
            //{
            MakeCustomForce();
            m_Rigidbody.AddForce(m_NewForce, ForceMode.Force);
            break;

            /*} else
             * if (endSwingR .GetComponent<endSwing>().reverse == true)
             * {
             *
             * }*/
        }

        if (st == false)
        {
            m_NewForce      = new Vector3(m_ForceX, m_ForceY, 0);
            m_modeSwitching = ModeSwitching.Force;
        }
        else if (st == true)
        {
            m_modeSwitching = ModeSwitching.Start;
        }

        if (returningBall == true)
        {
            m_ForceX = m_ForceX * -1f;
        }

        //reverse the swing when it collides with the box collider

        /* if (st == true)
         * {
         *   m_NewForce = new Vector3(m_ForceX, m_ForceY, 0);
         *   m_modeSwitching = ModeSwitching.Force;
         *   //m_Rigidbody.AddForce(m_NewForce, ForceMode.Force);
         * }
         * //stops the swinging when it hits the next ball
         * if (nextBall.GetComponent<stoper>().st == true)
         * {
         *   m_modeSwitching = ModeSwitching.Start;
         *   this.st = false;
         * }
         * /*if (finalBall.GetComponent<reverseBallCon>().st == true)
         * {
         *  // m_ForceX = m_ForceX * -1;
         *   m_modeSwitching = ModeSwitching.Start;
         *   this.st = false;
         * }*/
        /*else if (this.st == false) {
         *  m_ForceX = m_ForceX * -1;
         *  m_modeSwitching = ModeSwitching.Force;
         * }*/
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        // switching mode
        switch (m_ModeSwitching)
        {
        //This is the starting mode which resets the GameObject
        case ModeSwitching.Start:
            // reset flag
            hasWon = false;
            // reset drag
            drag = 0.0f;
            // reset the flags and variables
            currentLerpTime = 0;

            // reset the distance
            m_DistanceString = "-1";
            // reset the mass and the drag
            m_Rigidbody.mass = mass;
            m_Rigidbody.drag = 0.0f;
            // enable the collider after a restart
            this.GetComponent <Collider>().enabled = true;
            //This resets the GameObject and Rigidbody to their starting positions
            transform.position = m_StartPos;
            //This resets the velocity of the Rigidbody
            m_Rigidbody.velocity = new Vector3(0f, 0f, 0f);

            // start after the variables have been set
            m_ModeSwitching = ModeSwitching.Force;
            break;

        //This is Force Mode
        case ModeSwitching.Force:
            // remove the contstraint (stop)
            m_Rigidbody.constraints = RigidbodyConstraints.None;

            // position relative to initial position (Vector3)
            // Debug.Log("Relative position: " + (transform.position - m_StartPos));

            // compute angle
            float opositeSide  = Mathf.Abs(transform.position.z) - Mathf.Abs(m_StartPos.z);
            float adjacentSide = Mathf.Abs(transform.position.x) - Mathf.Abs(m_StartPos.x);
            float newAngle     = Mathf.Atan(opositeSide / adjacentSide) * Mathf.Rad2Deg;
            // Debug.Log ("Angle: " + newAngle);

            // distance to the hole (float)
            float newDistanceToHole = Vector3.Distance(transform.position, holeObj.transform.position);
            // Debug.Log("Distance to the hole: " + Vector3.Distance (transform.position, holeObj.transform.position).ToString ("F5"));

            // compute force to apply based on the angle
            vForce = Quaternion.AngleAxis(m_InitialAngle + m_Angle, Vector3.up) * -Vector3.right;
            //Debug.Log ("vForce: " + vForce.ToString ("F2"));

            currentLerpTime += Time.deltaTime;
            if (currentLerpTime > lerpTime)
            {
                currentLerpTime = lerpTime;
            }
            // acceleration/deceleration function (sigmoid)
            float t = currentLerpTime / lerpTime;
            t = t * t * (3f - 2f * t);
            // applied force (Vector3)
            Vector3 appliedForce = vForce * (1.0f - t) * m_AccelFactor;
            float   appliedAngle = 0.0f;
            Vector3 axis         = Vector3.zero;
            Quaternion.Euler(appliedForce).ToAngleAxis(out appliedAngle, out axis);
            // Debug.Log ("Applied Force: " + appliedAngle);

            m_Rigidbody.AddForce(vForce * (1.0f - t) * m_AccelFactor, ForceMode.Force);

            // almost stopped, apply drag
            if (t > dragTrigger)
            {
                drag            += dragFactor;
                m_Rigidbody.drag = drag;
                if (Mathf.Abs(m_Rigidbody.velocity.x) < 0.05)
                {
                    if (!hasWon)
                    {
                        //stop the ball
                        m_Rigidbody.constraints = RigidbodyConstraints.FreezeAll;
                        results = "-99";
                        // switch to results
                        m_ModeSwitching = ModeSwitching.Result;
                    }
                }
            }

            // test if the ball has fallen
            if (m_Rigidbody.position.y < -3)
            {
                if (hasWon == false)
                {
                    //Debug.Log ("Ball has fallen outside the green....");
                    results = "-1";
                    //stop the ball
                    m_Rigidbody.constraints = RigidbodyConstraints.FreezeAll;
                    // switch to results
                    m_ModeSwitching = ModeSwitching.Result;
                }
            }

            // send intermediate data
            string data = m_HolePos.ToString() + "," + m_BallPos.ToString() + "," + newDistanceToHole.ToString("F2") + "," + newAngle.ToString("F2") + "," + appliedAngle.ToString("F2") + "\"";
            // Debug.Log ("Data :" + data);
            SendData(data);
            break;

        case ModeSwitching.Result:
            if (results.Equals("-1"))
            {
                m_DistanceString = "-1";
                // nothing else to do. Ball is in the hole or fell outside of the green
            }
            else if (results.Equals("0"))
            {
                m_DistanceString = "0";
            }
            else
            {
                float dist = Vector3.Distance(transform.position, holeObj.transform.position);
                m_DistanceString = dist.ToString("F2");
                // calculate the relative position to thehole
                Vector3 relativeDistance = holeObj.InverseTransformPoint(transform.position);
                results = m_DistanceString + ", " + relativeDistance.ToString("F2");
            }

            // send the results back to the client
            SendEndEpisode();
            // go to idle state
            m_ModeSwitching = ModeSwitching.Idle;
            break;

        case ModeSwitching.Idle:
            // waiting zone
            break;

        case ModeSwitching.Socket:
            // waiting zone
            if (isdataArrived)
            {
                // position the ball
                //select
                if (m_BallPos == 1)
                {
                    pos = ball_pos_1;
                }
                else if (m_BallPos == 2)
                {
                    pos = ball_pos_2;
                }
                else if (m_BallPos == 3)
                {
                    pos = ball_pos_3;
                }
                //Debug.Log ("Ball Position: " + m_BallPos);
                Vector3 ball_position = new Vector3(pos [0], pos [1], pos [2]);
                //Debug.Log ("Position: " + hole_pos.ToString("F3"));
                transform.position = ball_position;
                //The GameObject's starting position and Rigidbody position
                m_StartPos = transform.position;

                // position the hole
                //select
                if (m_HolePos == 1)
                {
                    pos = hole_pos_1;
                }
                else if (m_HolePos == 2)
                {
                    pos = hole_pos_2;
                }
                else if (m_HolePos == 3)
                {
                    pos = hole_pos_3;
                }

                //Debug.Log("Hole Position: " + m_HolePos);
                Vector3 hole_position = new Vector3(pos[0], pos[1], pos[2]);
                //Debug.Log ("Position: " + hole_pos.ToString("F3"));
                holeObj.transform.position = hole_position;
                Vector3 hole_rotation = new Vector3(pos[3], pos[4], pos[5]);
                holeObj.transform.rotation = Quaternion.Euler(hole_rotation);

                // compute initial distance
                m_InitialDistance = Vector3.Distance(transform.position, holeObj.transform.position);
                // compute angle to the hole
                // sin(a) = holeObj.transform.position.z - transform.position.z / m_InitialDistance;
                m_InitialAngle = Mathf.Rad2Deg * Mathf.Asin((holeObj.transform.position.z - transform.position.z) / m_InitialDistance);
                // Debug.Log ("Angle: " + m_InitialAngle);
                // freeze the ball
                m_Rigidbody.constraints = RigidbodyConstraints.FreezeAll;
                // display the ball
                m_Renderer.enabled = true;

                m_ModeSwitching = ModeSwitching.Start;
                isdataArrived   = false;
            }
            break;

        case ModeSwitching.Manual:
            // reset distance
            m_DistanceString = "0";

            // parse the input values
            m_AccelFactor = float.Parse(m_AccelFactorString) / 10;
            m_Angle       = float.Parse(m_AngleString);
            m_HolePos     = int.Parse(m_HolePosString);
            m_BallPos     = int.Parse(m_BallPosString);

            // position the ball
            //select
            if (m_BallPos == 1)
            {
                pos = ball_pos_1;
            }
            else if (m_BallPos == 2)
            {
                pos = ball_pos_2;
            }
            else if (m_BallPos == 3)
            {
                pos = ball_pos_3;
            }
            //Debug.Log ("Ball Position: " + m_BallPos);
            Vector3 ball_pos = new Vector3(pos [0], pos [1], pos [2]);
            //Debug.Log ("Position: " + hole_pos.ToString("F3"));
            transform.position = ball_pos;
            //The GameObject's starting position and Rigidbody position
            m_StartPos = transform.position;

            // position the hole
            //select
            if (m_HolePos == 1)
            {
                pos = hole_pos_1;
            }
            else if (m_HolePos == 2)
            {
                pos = hole_pos_2;
            }
            else if (m_HolePos == 3)
            {
                pos = hole_pos_3;
            }

            //Debug.Log("Hole Position: " + m_HolePos);
            Vector3 hole_pos = new Vector3(pos[0], pos[1], pos[2]);
            //Debug.Log ("Position: " + hole_pos.ToString("F3"));
            holeObj.transform.position = hole_pos;
            Vector3 hole_rot = new Vector3(pos[3], pos[4], pos[5]);
            holeObj.transform.rotation = Quaternion.Euler(hole_rot);

            // compute initial distance
            m_InitialDistance = Vector3.Distance(transform.position, holeObj.transform.position);
            // compute angle to the hole
            // sin(a) = holeObj.transform.position.z - transform.position.z / m_InitialDistance;
            m_InitialAngle = Mathf.Rad2Deg * Mathf.Asin((holeObj.transform.position.z - transform.position.z) / m_InitialDistance);
            // Debug.Log ("Angle: " + m_InitialAngle);
            // freeze the ball
            m_Rigidbody.constraints = RigidbodyConstraints.FreezeAll;
            // display the ball
            m_Renderer.enabled = true;
            break;
        }
    }
예제 #17
0
    void FixedUpdate()
    {
        var gv2 = new Vector2(
            TransformGoal.position.x,
            TransformGoal.position.z);
        var gv3 = new Vector3(
            TransformGoal.position.x,
            TransformGoal.position.y, TransformGoal.position.z);

        var tv2 = new Vector2(
            transform.position.x, transform.position.z);
        var tv3 = new Vector3(
            transform.position.x, transform.position.y, transform.position.z);

        var dir  = (gv2 - tv2).normalized;
        var dist = (gv2 - tv2).magnitude;

        if (Mathf.Abs(m_Rigidbody.velocity.y) > 0.00001 || Mathf.Abs(m_Rigidbody.velocity.x) > 0.00001)
        {
            Debug.Log(m_Rigidbody.velocity.y > 0);
            Debug.Log(m_Rigidbody.velocity.y.ToString("F5"));
            Debug.Log(m_Rigidbody.velocity.x > 0);
            Debug.Log("velocity: " + m_Rigidbody.velocity.ToString("F5") + " pos " + m_Rigidbody.transform.position.ToString("F5"));
        }

        //If the current mode is not the starting mode (or the GameObject is not reset), the force can change
        if (m_ModeSwitching != ModeSwitching.Start)
        {
            //The force changes depending what you input into the text fields
            m_NewForce = new Vector3(m_ForceX, m_ForceY, 0);
        }
        MakeCustomForce();
        //Here, switching modes depend on button presses in the Game mode
        switch (m_ModeSwitching)
        {
        //This is the starting mode which resets the GameObject
        case ModeSwitching.Start:
            //This resets the GameObject and Rigidbody to their starting positions
            transform.position             = m_StartPos;
            m_Rigidbody.transform.position = m_StartForce;

            //This resets the velocity of the Rigidbody
            m_Rigidbody.velocity = new Vector3(0f, 0f, 0f);
            m_ModeSwitching      = ModeSwitching.Idle;
            break;

        //These are the modes ForceMode can force on a Rigidbody
        //This is Acceleration mode
        case ModeSwitching.Acceleration:
            //The function converts the text fields into floats and updates the Rigidbody’s force
            //MakeCustomForce();
            m_NewForce = new Vector3(m_ForceX, m_ForceY, 0);
            //Use Acceleration as the force on the Rigidbody
            Debug.Log("apply acceleration force" + m_NewForce);
            m_Rigidbody.AddForce(m_NewForce, ForceMode.Acceleration);

            break;

        //This is Force Mode, using a continuous force on the Rigidbody considering its mass
        case ModeSwitching.Force:
            //Converts the text fields into floats and updates the force applied to the Rigidbody
            //MakeCustomForce();
            m_NewForce = new Vector3(m_ForceX, m_ForceY, 0);
            //Use Force as the force on GameObject’s Rigidbody
            Debug.Log("apply force" + m_NewForce);
            m_Rigidbody.AddForce(m_NewForce, ForceMode.Force);
            break;

        //This is Impulse Mode, which involves using the Rigidbody’s mass to apply an instant impulse force.
        case ModeSwitching.Impulse:
            //The function converts the text fields into floats and updates the force applied to the Rigidbody
            // MakeCustomForce();
            m_NewForce = new Vector3(m_ForceX, m_ForceY, 0);
            //Use Impulse as the force on GameObject
            Debug.Log("apply impulse force" + m_NewForce);
            m_Rigidbody.AddForce(m_NewForce, ForceMode.Impulse);
            m_ModeSwitching = ModeSwitching.Idle;
            break;

        //This is VelocityChange which involves ignoring the mass of the GameObject and impacting it with a sudden speed change in a direction
        case ModeSwitching.VelocityChange:
            //Converts the text fields into floats and updates the force applied to the Rigidbody
            //MakeCustomForce();
            m_NewForce = new Vector3(m_ForceX, m_ForceY, 0);
            //Make a Velocity change on the Rigidbody
            m_Rigidbody.AddForce(m_NewForce, ForceMode.VelocityChange);
            m_ModeSwitching = ModeSwitching.Idle;
            break;

        case ModeSwitching.Velshot:
            //dist	vx	deltay	t=dist/vx	vy=deltay+5t^2/t
            var vx     = 5;
            var deltay = gv3.y - tv3.y;
            Debug.Log("deltay: " + deltay + "," + "gv3.y " + gv3.y + ", tv3.y " + tv3.y);
            var t  = dist / vx;
            var vy = (deltay + 5 * Mathf.Pow(t, 2)) / t;
            Debug.Log("vs, deltay, t, vy " + vx + "," + deltay + "," + t + "," + vy);
            Vector3 newvel = new Vector3(
                vx, //m_ForceX, //dir.x ,
                vy, //m_ForceY,  //dist*m_ForceY,dist*10/(2*dir.x)
                dir.y
                );
            Debug.Log("apply velocity: " + newvel + " dist: " + dist + " goal " + gv3 + " mypos " + tv3);
            m_Rigidbody.AddForce(newvel, ForceMode.VelocityChange);
            m_ModeSwitching = ModeSwitching.Idle;
            break;

        case ModeSwitching.Idle:
            break;
        }
    }