[英]Unity3D: Get Velocity after collision
I'm making a billiard game and I have two questions: 我正在打台球游戏,我有两个问题:
How do I find the velocity of two balls when they collide with each other and how do I apply it to both balls ? 当两个球相互碰撞时,如何找到它们的速度?如何将其应用于两个球?
I already know the angles that they're gonna move, I just need to find the velocity that they'll move in those directions. 我已经知道它们将要移动的角度,我只需要找到它们在这些方向上移动的速度即可。
I was never good at physics/physics programming so I would appreciate any help given! 我从来都不擅长物理/物理编程,因此,我将不胜感激! Btw, my game is in 3D.
顺便说一句,我的游戏是3D模式。
Thank you for your time! 感谢您的时间!
Edit: What I'm trying to do is make the movement direction match the direction that I'm calculating using this script: 编辑:我想做的是使移动方向与使用此脚本计算的方向匹配:
if (Physics.Raycast(ray, out hit, Mathf.Infinity, lmm))
{
location = new Vector3(hit.point.x, 1.64516f, hit.point.z);
}
if (Physics.SphereCast(transform.position, 0.77f, location - transform.position, out hitz, Mathf.Infinity, lmm2))
{
if (hitz.collider.tag == "Ball")
{
Vector3 start = hitz.point;
end = start + (-hitz.normal * 4);
lineRenderer2.SetPosition(1, end);
}
}
You can calculate the new velocities by applying an impulse to each ball. 您可以通过对每个球施加脉冲来计算新的速度。 We can apply Newton's Third law to do so.
我们可以应用牛顿第三定律。 PseudoCode:
伪代码:
RelativeVelocity = ball1.velocity - ball2.velocity;
Normal = ball1.position - ball2.position;
float dot = relativeVelocity*Normal;
dot*= ball1.mass + ball2.mass;
Normal*=dot;
ball1.velocity += Normal/ball1.mass;
ball2.velocity -= Normal/ball2.mass;
This however does not take into account friction between the two balls nor their angular momentum. 但是,这没有考虑两个球之间的摩擦力或它们的角动量。
If I understand correctly, what you are trying to do is to apply the speed of "Ball A" as a force to "Ball B", and vice versa as well. 如果我理解正确,那么您想做的就是将“ A球”的速度作为对“ B球”的作用力,反之亦然。 If that is the case I would suggest something like this:
如果是这种情况,我建议这样的事情:
Attach this script to all the balls: 将此脚本附加到所有球上:
public class BallCollisionScript : MonoBehaviour
{
public Rigidbody Rigid_Body;
void Start()
{
Rigid_Body = this.GetComponent<Rigidbody>();
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag.Equals("Ball"))
{
BallScript otherBall = collision.gameObject.GetComponent<BallScript>();
Rigid_Body.AddForce(otherBall.Rigid_Body.velocity);
}
}
}
Make sure that all balls are tagged with "Ball". 确保所有球都标记有“球”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.