# How can I Implement spin in billiard game?

I'm not sure that you played billiard or snooker before.if you played you should know about spin. but anyway if you don't know about it you can see it in this video

I want to Implement spin like this game

for example difference between Top spin and Back spin is:

Top spin

Back spin

Spin behaviour

if you aim behind the ball you can shoot the ball in all direction

if you aim closest to right you can just aim the right

if you aim closest to left you can just aim the left

I found a same Issue that used Torque in stackoverflow but I don't know why didn't work!

Top Spin

GetComponent<Rigidbody>().AddTorque(Vector3.back * cueStrength );
GetComponent<Rigidbody>().AddForceAtPosition(cueStick.forward * cueStrength,transform.position,ForceMode.Acceleration);


Back Spin

GetComponent<Rigidbody>().AddTorque(Vector3.forward * cueStrength );
GetComponent<Rigidbody>().AddForceAtPosition(cueStick.forward * cueStrength,transform.position,ForceMode.Acceleration);


I noticed that flyordie's snooker didn't use AddTorque.so I don't need answer that Implemented with AddTorque.

# Update

What I tried

I add joystick to select spin angle

then I apply spin force ( It seems my spin works but sometimes didn't work that I attached video in end of my question).

this is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainBall : MonoBehaviour {

private Vector3 FirstPos; // store first position as reset position
public Transform SpinRoator; // spin rotator cylinder that show spin angle
private Rigidbody rigidbody;
public bool SpinUsed; // spin should use once
private float v2;

private void Start() {
rigidbody = GetComponent<Rigidbody>();
FirstPos = transform.position;
}

//After goal reset ball position
public void ResetPos(){
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
transform.position = FirstPos;

}

void SpinForce(){
SpinUsed = true;
float F = ComebackForce(); // calculate comeback force
StopBall(); // stop ball
rigidbody.AddForce(SpinRoator.forward*F);// apply spin force
}

// calculating return force after hitting f = m.a
float ComebackForce(){
var m = rigidbody.mass;
var v1 = rigidbody.velocity.magnitude;

var a = (v1 - v2) / Time.deltaTime;
var F = m * a;
v2 = rigidbody.velocity.magnitude;
return F;
}

//stop ball
public void StopBall(){
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
}

private void OnCollisionEnter(Collision other) {
if(other.collider.tag == "Ball" || other.collider.tag == "ColorfulBall"){
if(!SpinUsed){
SpinForce();
}
}
}
}


# but What is Problem?!

It seems works correctly but My Problem is sometimes Spin don't work correctly you can see this video that demonstrate this problem.my ball goes wrong direction of spin!!!

1.Ball goes Wrong sometimes

as you can see in above gif ball goes wrong direction!!! ball should go along yellow cylinder but I don't know what is problem of my code!!!

1.Ball goes slow sometimes

another problem is comeback force is slow sometimes!!! I calculate comeback force F = m.a but why is it slow?!

• The Unity physics engine is good enough for most games, but if you want to develop a game where it is extremely important to get the physics exactly right, like a billard simulation for example, you might have to write your own physics. – Philipp Mar 12 at 10:38
• @Philipp I don't know how can I Implement custom physic.but I think it's possible to recreate Spins with default physic.but anyway I hope someone help me to Implement custom physic for billiard – Seyed Morteza Kamali Apr 10 at 17:27
• This question is too broad. We could help if you had a system already working and needed assistance with specific issues. Rocket League, for example, used Bullet physics: en.wikipedia.org/wiki/Bullet_(software) – Almo Apr 10 at 18:37
• @Almo why should I use Bullet physic? Is there a problem in unity built-in physic problem? – Seyed Morteza Kamali Apr 11 at 3:30
• I'm not going to do research for you. A quick google searched turned this up. rockpapershotgun.com/2015/09/02/rocket-league-making-of – Almo Apr 11 at 15:30

## 1 Answer

I don't know if my idea will be good enough for you, but you can try it - maybe while aiming, you can store the info on what did the player do (a simple way would be to store a vector... "Player aimed at center horizontally and +5% vertically) and then, when you hit a ball, stop it completely for a frame and add a force that would be calculated with the spin included?

• Can you explain how this is different from the AddForceAtPosition(...) solution that OP already attempted? – DMGregory Apr 10 at 19:05
• I did this before but I have problem I will update my answer and explain this problem youtu.be/DEgX-5sqMkU – Seyed Morteza Kamali Apr 11 at 2:55
• @DMGregory my first solution was realistic way that used Torque but It didn't work.apply force after collision solution is fake way to apply spin force. – Seyed Morteza Kamali Apr 11 at 3:27
• @DMGregory I updated my question and add this fake way but I don't know why I have problem sometimes.I tested it several times but I don't know why ball goes wrong. – Seyed Morteza Kamali Apr 11 at 3:33
• @DMGregory I think problem is where I used SpinRoator.forward but apply force along SpinRoator.forward is correct!!! – Seyed Morteza Kamali Apr 11 at 3:45