Billiards game. Draw a targetting line

Hellow,

I’m unity3d starter.

I want to show the whiteline …

9655-bi.png

like this…

white line help the player …

so i want to show the white line…

how make this line… ?

I don’t know what kind of the function in unity3d…

help me plz…

4 Answers

4

To draw the guide line, you will need the ghost-ball (Refer to your previous question). The ghost-ball-gameObject and the target-ball-gameObject will be used to create a very important data: normal

You will need to combine the normal with the direction of the cue-ball to calculate the vector reflection. From that vector reflection, you will get the guideline you want (there is 2 line there, the original aiming line, and the reflected cue-ball bounce line)

Example script

This script was used to do the calculation of a bullet ricochet, but it shares the similarity with the cue-ball guideline as both of them involves calculating the vector-reflection. I am putting this script in to give you the idea what you will be doing

using UnityEngine;

public class DeflectBullet : MonoBehaviour {
	void Update () {
		Ray a = new Ray( transform.position, transform.forward );
		Ray b;
		RaycastHit hit;
		
		if( Deflect( a, out b, out hit )  ){
			Debug.DrawLine( a.origin, hit.point );
			Debug.DrawLine( b.origin, b.origin + 3 * b.direction );
		}
	}
	
	bool Deflect( Ray ray, out Ray deflected, out RaycastHit hit ) {
		
		if( Physics.Raycast(ray, out hit) ) {
			Vector3 normal = hit.normal;
			Vector3 deflect = Vector3.Reflect( ray.direction, normal );
			
			deflected = new Ray( hit.point, deflect );
			return true;
		}
		
		deflected = new Ray( Vector3.zero, Vector3.zero );
		return false;
	}
}

You can get the world position from the aim-Vector and reflected-Vector, and insert it into a [LineRenderer][1]. [LineRenderer.SetVertexCount][2] [LineRenderer.SetPosition][3] [1]: http://docs.unity3d.com/Documentation/ScriptReference/LineRenderer.html [2]: http://docs.unity3d.com/Documentation/ScriptReference/LineRenderer.SetVertexCount.html [3]: http://docs.unity3d.com/Documentation/ScriptReference/LineRenderer.SetPosition.html

thank you very much! your so genius !! :) but.. i don't look the whiteline in gameobject. just i can look whiteline in Scene Place ... how show the whiteline in gameobject? ..

The white line in the scene-view is from the Debug.DrawLine(), you can delete that code if you want to. To get the white line in game view, you will need to draw the line using LineRenderer or GL (accessible only in Pro, I think). You need 3 points to draw the guideline, you already have 2: ball.position & ghostball.position, the last Vector3 (where the ball will be heading after the hit) will be calculated using the reflection (look at my example)

Succeeded. Thanks to you !!! :) thank you very much!!

@Chronos-L I was checking the link where getting position of the ghost ball is answered but unfortunately it is deleted. Can you post the answer here again or should I make a new question?

To Draw Lines you can use like already suggested LineRenderer or the GL Class.

The Above Raycast can also be a CapsuleCast just set the radius to your Ball Radius.

like nuveria said use the hit Normal and the hit Point for Calculation.

Instanciate a Copy off your Ball with a transperent material and move it along the normal * radius.

thank you ! your link was helpful. :)

Check this out. It will do what you want

thx! your link :)

I don’t think you can calculate white ball’s reflection direction like reflecting from flat surface. See here: http://www.real-world-physics-problems.com/physics-of-billiards.html

27938-physics_billiards_1.png

As you see from the diagram V2a is not making a perfect reflection, instead its making a 90 degree with the collision normal (V2b). This is always 90 degree if there is no friction between balls.

Now the question is how to find V2a ? Since we can get the normal vector from collision data all we need to find the tangent vector to it. But we get two tangent vectors heading opposite directions. According to your hit direction decide which tangent to use.

there is an example to find the position of ghost ball: http://wangweiqiang.net/how-to-find-the-ghost-ball-position-in-billiard-game/

I looked the example but it only shows where the ghost ball on the moment of collusion. The discussion is which direction the ghostball will go after the collusion. Most people in the discussion above making the calculation wrong and using reflect. The link I sent explains why it is wrong.