1
\$\begingroup\$

I'm working on a billiard game in Unity (2D), where I used 3D sphere visually and I'm trying to make the ball's rotation look natural as it moves across the table or hit another. I've got the ball moving using Rigidbody2D, but the rotation doesn't quite look right, especially when the ball changes direction or hits a cushion. Sometimes, the rotation seems off, and I want it to feel more natural.

If you could look up this video it just sometimes has weird rotaiton around it self and seems odd i just want to look more like realistic rotation of normal sphere.

public class BilliardBallRotation : MonoBehaviour
{
    private Transform ball3D;
    public float ballRadius = 0.5f;
    public float rotationSpeed = 1.0f;
    private float circumference;
    private Rigidbody2D rb;
    private float angle;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        circumference = 2 * Mathf.PI * ballRadius;
        sphere3D = transform;
    }

    void Update()
    {
        if (rb != null && rb.velocity != Vector2.zero)
        {
            float distanceMoved = rb.velocity.magnitude * Time.deltaTime;
            angle = (distanceMoved / circumference) * 360 * rotationSpeed;
            sphere3D.Rotate(Vector3.forward, -angle, Space.Self);
        }
    }
}
\$\endgroup\$
0

2 Answers 2

1
\$\begingroup\$

It seems like you tried to implement your own rotation here. But this shouldn't really be necessary when you are using 3d rigidbodies. Rigidbody3D physics are perfectly capable of handling the rotation of a sphere rolling across a surface after being moved with rigidbody.AddForce or by a collision with another rigidbody.

If the sphere doesn't seem to be rolling correctly on its own, then it might be sliding (something that does happen in real-world billard, by the way). But if the ball slides too much, then you might have to assign a physics materials to surface and sphere, and fiddle with the friction parameters until you got the right amount of friction between table and balls. Another factor that might result in the wrong amount of friction is too little gravity. The default gravity assumes that 1 unit = 1 meter in your game. If that's not true, then you might have to change the global gravity accordingly.

But if you really want to use 2D physics for a 3D physics game like pool billard and implement your own rotation, then you need to decouple the physics from the visuals. Looking at your code, it appears like you have the rigidbody2D and the sphere mesh on the same game object. When you rotate the sphere, you are rotating the 2d rigidbody as well. This leads to the weird physics glitches you are experiencing. To prevent this, make the object with the Rigidbody2D with its CircleCollider2D an invisible object without a MeshRenderer, and make the visualization with the MeshRenderer a child of said object without a collider or a rigidbody. Then apply the rotation to the transform.localRotation of the child. That way you can rotate the visualization without messing with the physics.

\$\endgroup\$
5
  • \$\begingroup\$ I am using a 2D Rigidbody on a 3D sphere, which I want to visually appear 3D while having 2D physics. \$\endgroup\$ Commented Oct 25 at 14:22
  • \$\begingroup\$ @TishkoSabir Why not use 3d physics? \$\endgroup\$
    – Philipp
    Commented Oct 25 at 14:24
  • \$\begingroup\$ I think 3D physics is more computationally expensive compared to 2D physics, I’m developing this game for mobile devices. \$\endgroup\$ Commented Oct 25 at 14:29
  • \$\begingroup\$ @TishkoSabir For pool billard, that's probably not much of an issue. But I updated the answer with another paragraph explaining how you could use 2D physics with a 3D visualization. \$\endgroup\$
    – Philipp
    Commented Oct 25 at 14:32
  • \$\begingroup\$ Thank you! it worked! \$\endgroup\$ Commented Oct 25 at 14:54
-1
\$\begingroup\$

Not sure about the ball but I have created a wheel which was inspired by this tool. I created it using physics constraint and giving mesh a random impulse to rotate.

It can also be done by just rotating your mesh on the event tick for a random amount of time. For a smooth slow down at the end, you can use interpolation.

When the mesh stops - based on its rotation, determine the answer. If the angle is from 0 to 60 - yes, if from 60 to 120 - no, etc. (remember that the rotation in unreal changes from -180 to 180).

I know it is not exactly what you need, but it may help you in some way.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This seems to be answering a very different question than the one that was asked. \$\endgroup\$
    – DMGregory
    Commented Oct 25 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .