// Use this for initialization void Start() { tempVec = new Vector2(); for (int i = 0; i < balls.Count; i++) { // Create one new ball GameObject b = balls[i]; Ball2D ball = b.GetComponent <Ball2D>(); // Make sure this ball spawns at an empty spot do { tempVec = b.transform.position; //tempVec.x = Random.Range(0, GlobalVariable.SCREEN_WIDTH - 2 * (GlobalVariable.SHOULDER_WIDTH + GlobalVariable.BALL_SIZE)) * (GlobalVariable.SCREEN_WIDTH - 2 * (GlobalVariable.SHOULDER_WIDTH + GlobalVariable.BALL_SIZE)) + GlobalVariable.SHOULDER_WIDTH + GlobalVariable.BALL_SIZE; //tempVec.y = Random.Range(0, 500) * (GlobalVariable.SCREEN_HEIGHT - 2 * (GlobalVariable.SHOULDER_WIDTH + GlobalVariable.BALL_SIZE)) + GlobalVariable.SHOULDER_WIDTH + GlobalVariable.BALL_SIZE; tempVec.x = Random.Range(ball.mRadius + GlobalVariable.SHOULDER_WIDTH, GlobalVariable.SCREEN_WIDTH - GlobalVariable.SHOULDER_WIDTH); tempVec.y = Random.Range(ball.mRadius + GlobalVariable.SHOULDER_WIDTH, GlobalVariable.SCREEN_HEIGHT - GlobalVariable.SHOULDER_WIDTH); b.transform.position = tempVec; ball.mPos.x = tempVec.x; ball.mPos.y = tempVec.y; } while (checkBallColli(ball)); } }
// Update is called once per frame void Update() { for (int i = 0; i < balls.Count; i++) { GameObject b1 = balls[i]; Ball2D ball = b1.GetComponent <Ball2D>(); ball.UpdatePhysics(); for (int j = 0; j < balls.Count; j++) { GameObject b2 = balls[j]; Ball2D ball2 = b2.GetComponent <Ball2D>(); if (ball != ball2) { if (ball.isCollidingWith(ball2)) { handleBallColli(ball, ball2, Time.deltaTime); } } } for (int k = 0; k < holes.Count; k++) { Hole2D hole = holes[k].GetComponent <Hole2D>(); if (ball.isInside(hole)) { b1.SetActive(false); break; } } } }
// 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; } }
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 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); }
bool checkBallColli(Ball2D toCheck) { for (int i = 0; i < balls.Count; i++) { // Create one new ball GameObject b = balls[i]; Ball2D ball = b.GetComponent <Ball2D>(); if (ball.isCollidingWith(toCheck) && toCheck != ball) { return(true); } } return(false); }
// Update is called once per frame void Update() { for (int i = 0; i < balls.Count; i++) { GameObject b1 = balls[i]; Ball2D ball = b1.GetComponent <Ball2D>(); ball.UpdatePhysics(); for (int j = 0; j < balls.Count; j++) { GameObject b2 = balls[j]; Ball2D ball2 = b2.GetComponent <Ball2D>(); if (ball != ball2) { if (ball.isCollidingWith(ball2)) { handleBallColli(ball, ball2, Time.deltaTime); AudioSource.PlayClipAtPoint(hitSound, transform.position); } } } for (int k = 0; k < holes.Count; k++) { Hole2D hole = holes[k].GetComponent <Hole2D>(); if (ball.isInside(hole)) { b1.SetActive(false); if (b1.gameObject.tag == "Player") { gameOver.SetActive(true); Time.timeScale = .25f; Invoke("Reset", 1.0f); } break; } } } }
void handleBallColli(Ball2D ball1, Ball2D ball2, float elapsed) { /* * HVector2D difference = ball1.mPos - ball2.mPos; * //float differenceInY = ball1.mPos.y - ball2.mPos.y; * * float distanceBetween2Balls = Mathf.Sqrt(Mathf.Pow(difference.x, 2) + Mathf.Pow(difference.y, 2)); * float radiusAddedTogether = ball1.mRadius + ball2.mRadius; * * //checks if any other balls have collided * if (distanceBetween2Balls <= radiusAddedTogether) * { * adjustBallsDist(ball1, ball2, ) * } * else * { * * } */ }
public bool isCollidingWith(Ball2D other) { //Finds the difference float differenceInX = this.mPos.x - other.mPos.x; float differenceInY = this.mPos.y - other.mPos.y; //Finds the distance and the combined radius of the two balls float distanceBetween2Balls = Mathf.Sqrt(Mathf.Pow(differenceInX, 2) + Mathf.Pow(differenceInY, 2)); float radiusAddedTogether = this.mRadius + other.mRadius; //Checks if any other balls have collided if (distanceBetween2Balls <= radiusAddedTogether) { return(true); } else { return(false); } }
public bool isCollidingWith(Ball2D other) { //if distance between the centre point of the 2 balls // <= to the radius added together //finds the difference, does the pythogoras theorem float differenceInX = this.mPos.x - other.mPos.x; float differenceInY = this.mPos.y - other.mPos.y; //math.pow does the power float distanceBetween2Balls = Mathf.Sqrt(Mathf.Pow(differenceInX, 2) + Mathf.Pow(differenceInY, 2)); float radiusAddedTogether = this.mRadius + other.mRadius; if (distanceBetween2Balls <= radiusAddedTogether) { return(true); } else { return(false); } }
// If 2 balls are overlapping by this time, move them back to when they just collided void adjustBallsDist(Ball2D ball1, Ball2D ball2, float overlapDistance) { }
void handleBallColli(Ball2D ball1, Ball2D ball2, float elapsed) { }
private void Start() { ball = drawnObject.GetComponent <Ball2D>(); }
private void Start() { ball = drawnObject.GetComponent <Ball2D>(); mRadius = GlobalVariable.BALL_SIZE / 2; }