我目前正在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