我目前正在Unity(C#)中复制一个粒子系统。我正在向每个粒子添加物理(用一个球体表示)以允许它从位于原点的平面反弹。颗粒反弹很多次,受重力影响。随着弹跳变小,球体开始穿过飞机,直至最终落下。重力将持续作用于粒子。什么是停止飞机顶部的球体的正确方法?为什么碰撞响应允许弹跳球最终穿过平面?
到目前为止,我已经包含了我的实现,但代码总是最终允许球体在飞机上下落。
检测:
public bool SpherePlaneCollisionDetection(Particle part)
{
Vector3 sphereCenter = part.position;
Vector3 planeCenter = Vector3.zero;
Vector3 normal = Vector3.up;
double radius = 0.5;
Vector3 dist1 = (sphereCenter - planeCenter);
float finalDist = Vector3.Dot (dist1, normal);
if (finalDist > radius) {
return false;
} else {
//Debug.Log ("COLLISION HAS OCCURED");
//Debug.Log ("POSITION: " + part.position);
//Debug.Log ("FINAL DIST: " + finalDist);
return true;
}
}
回应:
public void HandlePlaneCollision()
{
for (int i=0; i<Particles.Count; i++) {
//Particle p1temp = Particle[i];
Particle tempParticle = (Particle)Particles[i];
//Particle part = tempParticle.GetComponent<Particle>();
float dampning = 0.8f;
float bounce = -1f;
if(SpherePlaneCollisionDetection(tempParticle))
{
tempParticle.velocity.y *= (bounce*dampning);
tempParticle.velocity.x *= friction;
tempParticle.velocity.z *= friction;
Debug.Log(tempParticle.velocity.y);
Particles[i] = tempParticle;
}
}
}
任何帮助是极大的赞赏。
当碰撞发生三角洲,尝试将y位置设置为飞机的y位置。 – Imapler 2014-10-07 06:31:38
并且如果'| speed |
Spektre
2014-10-07 06:47:23
你没有使用Unity内置的对撞机吗? – anothershrubery 2014-10-09 13:53:10