Search Unity

Bricks Arcade Collisiondetection Problem

Discussion in '2D' started by felixpk, Mar 5, 2015.

  1. felixpk

    felixpk

    Joined:
    Nov 7, 2013
    Posts:
    17
    Hello!
    I am making a little Bricks Game just like the one above!

    I am checking the Balls direction and the blocks normal, to calculate with Vector3.Reflect() the reflection Vector.
    All fine but there is one problem I am not able to solve.
    If the Ball hits exactly in the middle of two blocks (what happens quite often) the ball goes just through both Blocks. I figured out what happens exactly:
    • The ball Collides with the right block
    • Calculates the reflection so the balls direction is now dir(0,-1)
    • now he checks the collision with the second block
    • And again calculates the reflection wich is: dir(0,-1) and normal(0,-1) = reflection(0,1)
    The problem is that I can't find a solution to fix this, that works 100%.

    That's an image of the game.

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D other) {
    2.         if (other.gameObject.tag == "Brick") {
    3.             Vector2 tmpNormal = other.contacts[0].normal;
    4.             tmpNormal.x = Mathf.RoundToInt(tmpNormal.x);
    5.             tmpNormal.y = Mathf.RoundToInt(tmpNormal.y);
    6.  
    7.             //if ((dir.y < 0 && tmpNormal.y < 0) || (dir.y > 0 && tmpNormal.y > 0)) {
    8.             //    return;
    9.             //}
    10.  
    11.             dir = Vector3.Reflect(dir, tmpNormal).normalized;
    12.             Destroy(other.gameObject);
    13.  
    14.         }

    If you have any questions, I'll answer as soon as I can!
    Thanks in advance!
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Can't you save the contact normal and and do the reflection just before your movement code? That way all collisions should be finished and you'll only change the direction once.