#StackBounty: #unity #c# #billards How can I Implement spin in billiard game?

Bounty: 100

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

_39103267_snooker_spin_2_298

for example difference between Top spin and Back spin is:

Top spin

Record_2019_03_12_00_35_03_630

Back spin

Record_2019_03_12_00_35_39_151


Spin behaviour

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

A


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

B

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

C


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);

Record_2019_03_12_00_47_45_936

Back Spin

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

Record_2019_03_12_00_45_23_544

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

enter image description here

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

enter image description here
enter image description here
enter image description here
enter image description here

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

enter image description here

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

enter image description here

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


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.