// If 2 balls are overlapping by this time, move them back to when they just collided
    void adjustBallsDist(Ball2D ball1, Ball2D ball2, float overlapDistance)
    {
        HVector2D vectorDifference = new HVector2D(ball1.transform.position.x - ball2.transform.position.x, ball1.transform.position.y - ball2.transform.position.y);

        vectorDifference.normalize();
        HVector2D vectorDifference2 = vectorDifference * -1.0f;

        float moveBackDistance = overlapDistance / 2.0f;

        mPos  = new HVector2D(ball1.transform.position.x, ball1.transform.position.y);
        mPos2 = new HVector2D(ball2.transform.position.x, ball2.transform.position.y);

        HVector2D moveBall1 = new HVector2D(vectorDifference.x * moveBackDistance, vectorDifference.y * moveBackDistance);
        HVector2D moveBall2 = new HVector2D(vectorDifference2.x * moveBackDistance, vectorDifference2.y * moveBackDistance);

        mPos  += moveBall1;
        mPos2 += moveBall2;

        tempVec.x  = mPos.x;
        tempVec.y  = mPos.y;
        tempVec2.x = mPos2.x;
        tempVec2.y = mPos2.y;

        if (ball1.mVel.x > 0.0f || ball1.mVel.y > 0.0f)
        {
            ball1.transform.position = tempVec;
        }
        if (ball2.mVel.x > 0.0f || ball2.mVel.y > 0.0f)
        {
            ball2.transform.position = tempVec2;
        }
    }
Example #2
0
    public HVector2D projection(HVector2D vec)
    {
        float AdotB = this.dotProduct(vec);
        float BdotB = vec.dotProduct(vec);

        return(vec * (AdotB / BdotB));
    }
    void handleBallColli(Ball2D ball1, Ball2D ball2, float elapsed)
    {
        if (ball1.isActiveAndEnabled && ball2.isActiveAndEnabled)
        {
            HVector2D ball1Pos = new HVector2D(ball1.transform.position.x, ball1.transform.position.y);
            HVector2D ball2Pos = new HVector2D(ball2.transform.position.x, ball2.transform.position.y);

            HVector2D distance = new HVector2D(ball1Pos.x - ball2Pos.x, ball1Pos.y - ball2Pos.y);

            HVector2D v1p = ball1.mVel.projection(distance);
            HVector2D v2p = ball2.mVel.projection(distance);

            HVector2D v1pp = (v2p * (ball2.mMass * 2.0f)) / (ball1.mMass + ball2.mMass);
            HVector2D v2pp = (v1p * (ball2.mMass * 2.0f)) / (ball1.mMass + ball2.mMass);

            ball1.mVel = ball1.mVel - v1p + v1pp;
            ball2.mVel = ball2.mVel - v2p + v2pp;

            float overlapDistance = ball1.mRadius * 2.0f - GlobalVariable.findDistance(ball1Pos, ball2Pos);
            if (overlapDistance > 0.0f)
            {
                adjustBallsDist(ball1, ball2, overlapDistance);
            }
        }
    }
    public bool updatePhysics(float elapsed)
    {
        // get the object position
        mPos.x = transform.position.x;
        mPos.y = transform.position.y;

        //-------------------------------------------

        mVel = mVel * (1.0f - GlobalVariable.PHYSICS_FRICTION * elapsed);

        float displacementX = mVel.x * elapsed;
        float displacementy = mVel.y * elapsed;

        matrix.setTranslationMat(displacementX, displacementy);
        mPos = matrix * mPos;

        /*Back up solution
         * Vector2 displacement = new Vector2(displacementX, displacementy);
         * mPos.x += displacement.x;
         * mPos.y += displacement.y;*/

        //-----------------------------------------------
        tempPos.x = mPos.x;
        tempPos.y = mPos.y;

        transform.position = tempPos;
        // transform.position = new Vector2(transform.position.x + mVel.x, transform.position.y + mVel.y);
        return(true);
    }
    void updateBoundaryCollision(float elapsed)
    {
        mPos.x = transform.position.x;
        mPos.y = transform.position.y;
        //------------------------------------------------------

        HVector2D tableCenter   = new HVector2D(GlobalVariable.SCREEN_WIDTH / 2.0f, GlobalVariable.SCREEN_HEIGHT / 2.0f);
        Vector3   tableCenterV3 = new Vector3(tableCenter.x, tableCenter.y, 0);

        float moveAreaX = GlobalVariable.SCREEN_WIDTH / 2.0f - GlobalVariable.SHOULDER_WIDTH;
        float moveAreaY = GlobalVariable.SCREEN_HEIGHT / 2.0f - GlobalVariable.SHOULDER_WIDTH;

        if (transform.position.x >= tableCenter.x + moveAreaX - mRadius || transform.position.x <= tableCenter.x - moveAreaX + mRadius)
        {
            mVel.x = -mVel.x * (1.0f - 0.1f * elapsed);
            AudioSource.PlayClipAtPoint(hitSound, tableCenterV3);
        }

        if (transform.position.y >= tableCenter.y + moveAreaY - mRadius || transform.position.y <= tableCenter.y - moveAreaY + mRadius)
        {
            mVel.y = -mVel.y * (1.0f - 0.1f * elapsed);
            AudioSource.PlayClipAtPoint(hitSound, tableCenterV3);
        }

        //--------------------------------------------------------
        tempPos.x = mPos.x;
        tempPos.y = mPos.y;

        transform.position = tempPos;
    }
    // Falls inside a hole
    public bool isInside(Hole2D hole)
    {
        HVector2D thisBall = new HVector2D(transform.position.x, transform.position.y);
        HVector2D holePos  = new HVector2D(hole.transform.position.x, hole.transform.position.y);

        return(GlobalVariable.findDistance(thisBall, holePos) <= hole.mRadius);
    }
    public bool isCollidingWith(Ball2D other)
    {
        HVector2D thisBall = new HVector2D(transform.position.x, transform.position.y);
        HVector2D thatBall = new HVector2D(other.transform.position.x, other.transform.position.y);

        return(GlobalVariable.findDistance(thisBall, thatBall) <= mRadius * 2);
    }
    public bool isCollidingWith(float x, float y)
    {
        HVector2D thisBall = new HVector2D(transform.position.x, transform.position.y);
        HVector2D point    = new HVector2D(x, y);

        return(GlobalVariable.findDistance(thisBall, point) <= mRadius);
    }
Example #9
0
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);              // Start line drawing
            if (ball != null && ball.isCollidingWith(pos.x, pos.y))
            {
                drawnLine = lineFactory.GetLine(pos, pos, 2.0f, Color.black);
                drawnLine.EnableDrawing(true);
            }
        }
        else if (Input.GetMouseButtonUp(0) && drawnLine != null)
        {
            drawnLine.EnableDrawing(false);
            //update the vel of the white ball.
            HVector2D v = new HVector2D(drawnLine.start.x - drawnLine.end.x, drawnLine.start.y - drawnLine.end.y);
            //  v.normalize();

            ball.mVel = v * 3.0f;
            drawnLine = null; // End line drawing
        }

        if (drawnLine != null)
        {
            drawnLine.end = Camera.main.ScreenToWorldPoint(Input.mousePosition);              // Update line end
        }
    }
Example #10
0
    public bool updatePhysics(float elapsed)
    {
        // get the object position
        mPos.x = transform.position.x;
        mPos.y = transform.position.y;

        //-------------------------------------------

        //Stores the displacement in a vector
        HVector2D displacement = mVel * elapsed;

        //Creates a new Matrix
        HMatrix2D transMatrix = new HMatrix2D();

        //Set the translation matrix with displacement values
        transMatrix.setTranslationMat(displacement.x, displacement.y);

        //Updates original vector pos with translation matrix
        mPos = transMatrix * mPos;

        //Adds friction physics to the Velocity, so the ball slows down.
        mVel = mVel * GlobalVariable.PHYSICS_FRICTION;

        //-----------------------------------------------
        tempPos.x = mPos.x;
        tempPos.y = mPos.y;

        transform.position = tempPos;
        //transform.position = new Vector2(transform.position.x + mVel.x, transform.position.y + mVel.y);
        return(true);
    }
    public static HVector2D operator /(HVector2D left, float scalar)
    {
        HVector2D r = new HVector2D();

        r.x = left.x / scalar;
        r.y = left.y / scalar;
        return(r);
    }
    public static HVector2D operator -(HVector2D left, HVector2D right)
    {
        HVector2D r = new HVector2D();

        r.x = left.x - right.x;
        r.y = left.y - right.y;
        return(r);
    }
    public void normalize()
    {
        HVector2D result = new HVector2D();

        result = this / magnitude();
        x      = result.x;
        y      = result.y;
    }
Example #14
0
    public static HVector2D operator *(HVector2D left, float scalar)
    {
        HVector2D vResult = new HVector2D();

        vResult.x = left.x * scalar;
        vResult.y = left.y * scalar;
        return(vResult);
    }
    public static HVector2D operator *(HMatrix2D a, HVector2D right)
    {
        HVector2D product = new HVector2D();

        product.x = a.entries[0, 0] * right.x + a.entries[0, 1] * right.y + a.entries[0, 2] * right.h;
        product.y = a.entries[1, 0] * right.x + a.entries[1, 1] * right.y + a.entries[1, 2] * right.h;
        product.h = a.entries[2, 0] * right.x + a.entries[2, 1] * right.y + a.entries[2, 2] * right.h;
        return(product);
    }
Example #16
0
    public static HVector2D operator +(HVector2D left, HVector2D right)
    {
        HVector2D vResult = new HVector2D();

        vResult.x = left.x + right.x;
        vResult.y = left.y + right.y;

        return(vResult);
    }
Example #17
0
    public static HVector2D operator *(HMatrix2D left, HVector2D right)
    {
        HVector2D vResult = new HVector2D();

        vResult.x = left.entries[0, 0] * right.x + left.entries[0, 1] * right.y + left.entries[0, 2] * right.h;
        vResult.y = left.entries[1, 0] * right.x + left.entries[1, 1] * right.y + left.entries[1, 2] * right.h;

        return(vResult);
    }
Example #18
0
    // Use this for initialization
    void Start()
    {
        HVector2D alpha = new HVector2D(2, 3);

        alpha.print();
        HVector2D beta = new HVector2D(5, 5);

        beta.print();
        HVector2D sum = alpha + beta;

        alpha.Normalize();
        alpha.print();
        sum.print();
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);              // Start line drawing
            if (ball != null && ball.isCollidingWith(pos.x, pos.y))
            {
                drawnLine = lineFactory.GetLine(pos, pos, 2.0f, Color.black);
                drawnLine.EnableDrawing(true);

                drawnLine2 = lineFactory.GetLine(pos, pos, 2.0f, Color.red);
                drawnLine2.EnableDrawing(true);
            }
        }
        else if (Input.GetMouseButtonUp(0) && drawnLine != null)
        {
            Vector3 tableCenter = new Vector3(GlobalVariable.SCREEN_WIDTH / 2.0f, GlobalVariable.SCREEN_HEIGHT / 2.0f, 0);

            AudioSource.PlayClipAtPoint(hitSound, tableCenter);

            drawnLine.EnableDrawing(false);
            drawnLine2.EnableDrawing(false);

            //update the vel of the white ball.
            HVector2D v = new HVector2D(drawnLine.start.x - drawnLine.end.x, drawnLine.start.y - drawnLine.end.y);
            //  v.normalize();

            ball.mVel = v * 3.0f;
            drawnLine = null; // End line drawing

            drawnLine2 = null;
        }

        if (drawnLine != null)
        {
            drawnLine.end = Camera.main.ScreenToWorldPoint(Input.mousePosition);              // Update line end
        }

        if (drawnLine2 != null)
        {
            Vector2 v          = new Vector2(drawnLine.start.x - drawnLine.end.x, drawnLine.start.y - drawnLine.end.y);
            Vector2 currentPos = new Vector2(drawnLine2.start.x, drawnLine2.start.y);
            drawnLine2.end = currentPos - v * -1.0f * 3.0f * (1.0f - GlobalVariable.PHYSICS_FRICTION * Time.deltaTime);;
        }
    }
Example #20
0
    // Update is called once per frame
    void Update()
    {
        HVector2D vA = new HVector2D();

        vA.x = 1;
        vA.y = 2;

        HVector2D vB   = new HVector2D(5, 7);
        HVector2D vSum = vA + vB;

        float dp = vA.dotProduct(vB);

        HVector2D mPos         = new HVector2D(10, 10);
        HVector2D displacement = new HVector2D(3, 4);

        HMatrix2D translationMatrix = new HMatrix2D();

        translationMatrix.setTranslationMat(displacement.x, displacement.y);
        mPos = translationMatrix * mPos;
    }
Example #21
0
    public float findAngle(HVector2D vec)
    {
        float angle = Mathf.Acos(this.dotProduct(vec) / (this.magnitude() * vec.magnitude()) * (180 / Mathf.PI));

        return(angle);
    }
Example #22
0
 public HVector2D projection(HVector2D vec)
 {
     return(new HVector2D(((x * vec.x + y * vec.y) / (vec.x * vec.x * vec.y * vec.y)) * vec.x, ((x * vec.x + y * vec.y) / (vec.x * vec.x * vec.y * vec.y)) * vec.y));
 }
Example #23
0
 public float findAngle(HVector2D vec)
 {
 }
Example #24
0
 public HVector2D projection(HVector2D vec)
 {
 }
Example #25
0
    // Use this for initialization
    void Start()
    {
        mVel = new HVector2D(0, 0);

        mRadius = GlobalVariable.BALL_SIZE / 2;
    }
    public HVector2D projection(HVector2D vec)
    {
        float fraction = dotProduct(vec) / vec.dotProduct(vec);

        return(vec * fraction);
    }
Example #27
0
 public float dotProduct(HVector2D vec)
 {
     return(x * vec.x + y * vec.y);
 }
    public float findAngle(HVector2D vec)
    {
        float angle = Mathf.Rad2Deg * Mathf.Acos(dotProduct(vec) / (vec.magnitude() * magnitude()));

        return(angle);
    }
Example #29
0
 public float findAngle(HVector2D vec)
 {
     return(Mathf.Atan2(vec.y, vec.x) - Mathf.Atan2(this.y, this.x));
 }
Example #30
0
 public float dotProduct(HVector2D vec)
 {
 }