I am trying to create a game similar to (Pocket Run Pool) https://www.youtube.com/watch?v=ewlAE6NQnsI&t=215s a 2D pool game that is much simpler than a 3D one I need help with creating code for trajectory prediction something similar to this Unity - Manual: Multi-scene physics
How do I go about doing this?
1 Answer
1
To find the ball/wall that you’re going to hit, use the “CircleCast” method in Physics2D. Make sure the radius is the same as your white ball. For this to work, all your balls and cushion sides should have colliders on.
This will give you your collision point. The raycast hit that this returns will have a “normal” vector - this is the vector stating the direction perpendicular to the collider edge at the point where you hit it. So in your image, you can visualise the normal if you draw an arrow from the centre of the 8-ball to the centre of the dashed ball, this will point straight out from the 8-ball surface at the point where the collision occurs.
This normal will be the opposite to the direction in which you’ll move the ball you just hit, i.e. -normal will be the white arrow you circled, coming out the 8-ball.
You can find the next trajectory of the white ball by reflecting your initial white ball movement across this normal.
Vector2 bouncedWhiteBallDirection = Vector2.Reflect(initialWhiteBallDirection, hit.normal);
When writing behaviour with vectors, I strongly recommend using gizmos to show you the directions and positions you’re using visually in the scene view. In your monobehaviours you can implement the function OnDrawGizmos then inside that you have access to all the Gizmos methods. For example:
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawLine(whiteBallTransform.position, whiteBallTransform.position + initialWhiteBallDirection);
}
This would draw a red line in the direction of “initialWhiteBallDirection” out from the white ball position.
Let me know if you have any questions about this!
you need to use trigonometry and line renderer. Will have to do some research.
– logicandchaosI reccomend using an alternate physics scene. Its simpler than using math and can give you much more accurate results. This link should get you started. https://youtu.be/4VUmhuhkELk
– RealFolk