Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Algorithm: If passed between other two

Discussion in 'Scripting' started by Corpse0327, Feb 10, 2015.

  1. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    Hi guys

    Imagine a billiards like game. There are 3 balls. Even though game is 3D we are working only on x and z axis.

    What i want to know is "how can i know, if a player hit a ball and that ball passed between the other two balls".

    I have tried 5-6 algorithms. Took my many days and still failed.

    I can save all three balls positions at each update, or at each time a collision happens for calculation purposes.
    I can know if hitted ball passed other side of the arena(split from an imaginery line by other two balls), dynamically
    I can know if two line segments collide(given four positions)
    I can know the collided point of two line segments

    I have tried algorithms that uses different combinations of these(mentioned above) to find a solution. But most causes mistakes at some extreme situations. The biggest problem i have encountered is, when a ball hits another ball. That ball moves at the same time with hitted ball, which causes many problems for different algorithms.

    I couldn't come up with a good algorithm, can you help?

    Thanks in advance.
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
  3. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    Is it safe? What if the frame drops and ball just passes without not being able to be detected?

    Just because of this fear i did not used that.
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Raycasts should happen in FixedUpdate, so frame drops wouldn't be an issue.

    Even if they were, a single raycast every frame is pretty cheap.
     
  5. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    Thanks for the reply.

    How could i forgot FixedUpdate. Something is not right with me. I should get proper sleeps :)

    I will go make some tests with it.
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Even in fixed, a fast moving ball can still jump past a ray.
    If it were me, I'd do one of 2 options
    • Put a collider inbetween the gate balls. Its on a layer that the balls dont collide with. cast ray from the moving ball, the distance that it moves in the frame to detect this collider.
    • 2d line intersection maths. gate balls form a line segment, moving ball distance travelled in a frame makes the other line segment.
     
  7. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    I have made some tests and what you said is true.

    Since this is a crucially important feature of the game. I have decided to use line intersections and some other geometric formulas. I just finished a new algorithm and was thinking of how to implement it. This new algorithm i made seems good enough. I will make some tests after i finished implementing it.

    Thanks for your answers. It really did helped me a lot.
     
    Last edited: Feb 11, 2015