10
\$\begingroup\$

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?!

\$\endgroup\$
8
  • 6
    \$\begingroup\$ 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. \$\endgroup\$
    – Philipp
    Commented Mar 12, 2019 at 10:38
  • \$\begingroup\$ @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 \$\endgroup\$ Commented Apr 10, 2019 at 17:27
  • \$\begingroup\$ 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) \$\endgroup\$
    – Almo
    Commented Apr 10, 2019 at 18:37
  • \$\begingroup\$ @Almo why should I use Bullet physic? Is there a problem in unity built-in physic problem? \$\endgroup\$ Commented Apr 11, 2019 at 3:30
  • 1
    \$\begingroup\$ A quick google searched turned this up. rockpapershotgun.com/2015/09/02/rocket-league-making-of \$\endgroup\$
    – Almo
    Commented Apr 11, 2019 at 15:30

1 Answer 1

-1
\$\begingroup\$

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?

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Can you explain how this is different from the AddForceAtPosition(...) solution that OP already attempted? \$\endgroup\$
    – DMGregory
    Commented Apr 10, 2019 at 19:05
  • \$\begingroup\$ I did this before but I have problem I will update my answer and explain this problem youtu.be/DEgX-5sqMkU \$\endgroup\$ Commented Apr 11, 2019 at 2:55
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Apr 11, 2019 at 3:27
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Apr 11, 2019 at 3:33
  • \$\begingroup\$ @DMGregory I think problem is where I used SpinRoator.forward but apply force along SpinRoator.forward is correct!!! \$\endgroup\$ Commented Apr 11, 2019 at 3:45

You must log in to answer this question.

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