- Home /
Predict Spherical Ball Collision Direction
Hi, I got a problem when doing on my project (kind of billiard game)
assume I have a two ball with different mass. I want the second Ball move on my Target direction using collision forces when collide with first ball. so I create a Target Point containing a vector data which the first ball move on. the target Point of course it has to slight off from the Second ball Center of Mass.
now this is my question.... How much it (the Target Point) is far from second ball's center of mass (assuming both of it has different radius and mass).
here's how I did it :
void Launch(Vector3 dir, Vector3 dir2){
//first ball is handling this script
//dir is Position of Second Ball
//dir2 is Position of Second Ball has to heading at during collision
//get Angles
float ang = Vec2Angle (new Vector2 (dir.x, dir.z), new Vector2 (transform.localPosition.x, transform.localPosition.z));
float ang2 = Vec2Angle (new Vector2 (dir.x, dir.z), new Vector2 (dir2.x, dir2.z));
//Get Angle direction of Second Ball heading at
float deltaAngle=Mathf.Abs (Mathf.DeltaAngle (ang, ang2))
//Because we need to target it into Opposite angle...
deltaAngle += 180;
//get relative position
Vector3 rPos= dir2 - dir;
//determine Target Position offset <- THE PROBLEM
float offset= Mathf.Sin(deltaAngle*Mathf.Deg2Rad);
rPos=rPos.normalized * Mathf.Lerp(0.3f,0.6f,offset); //0.3 is the second ball Radius
//Now add it into our Rigidbody Velocity
dir+=rPos;
GetComponent<Rigidbody>().velocity=(dir-transform.localPosition).normalized*40;
}
public static float Vec2Angle (Vector2 dir, Vector2 pivot)
{
Vector2 s = dir - pivot;
if (s.x > 0)
return Vector2.Angle (Vector2.up, s);
else
return 360 - Vector2.Angle (Vector2.up, s);
}
the current code isn't work properly (it slightly off from my targeted direction).
if you have questions about this, just ask. I'm really appreciate for your time here.
Answer by sparkzbarca · Aug 13, 2015 at 06:45 PM
Distance = ball1 radius + ball two Rafius
Create a Ray with origin ball two, direction - target direction so it's opposite direction.
Use Ray.getpoint(distance) to get the point to aim the moving ball one at.
Mass doesn't matter per say, like it effects where the balls end up after imPact but
not if they do unless you under shoot
well, indeed i didn't think about it. took me a while to realize it. previously already using it but everytime it goes miss (collision not detectable at extreme degree). taking per frame look and now I realize it goes right, setting my rigidbody as continous dynamic and now it work correctly. thanks for it....
That's not practical for a large number of balls, also remember to try to keep speeds realistic for good collisions well that look real. If your missing the collision I imagine they are going very fast if not you might be better cheating a bit still.
Do a simple distance check if distance to Aimpoint is less than say .005 shove the ball toward the other ball a bit to force the collision. Better than the penalty from continuous dynamic
I dont undestand this solution :S can you put a short lines of code to look a example?
Distance = between 2 balls position? after ray of ball number 2 position, direction-target direction? I dont undestand this above line
thanks :D