C# 为什么碰撞响应允许弹跳球体最终通过平面?
我目前正在复制Unity(C#)中的一个粒子系统。我将物理添加到每个粒子(由球体表示)以允许它从位于原点的平面反弹。受重力影响,粒子会反弹多次。当反弹变小时,球体开始穿过平面,直到最终落下。重力将持续作用在粒子上。在平面顶部停止球体的正确方法是什么 到目前为止,我已经包含了我的实现,但是代码总是最终允许球体穿过平面 检测:C# 为什么碰撞响应允许弹跳球体最终通过平面?,c#,unity3d,physics,collision,particle-system,C#,Unity3d,Physics,Collision,Particle System,我目前正在复制Unity(C#)中的一个粒子系统。我将物理添加到每个粒子(由球体表示)以允许它从位于原点的平面反弹。受重力影响,粒子会反弹多次。当反弹变小时,球体开始穿过平面,直到最终落下。重力将持续作用在粒子上。在平面顶部停止球体的正确方法是什么 到目前为止,我已经包含了我的实现,但是代码总是最终允许球体穿过平面 检测: public bool SpherePlaneCollisionDetection(Particle part) { Vector3 sphereCe
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;
}
}
}
响应:
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;
}
}
}
public void HandlePlaneCollision()
{
对于(int i=0;i我不使用Unity,但之前我对物理模拟进行了编码,这通常是因为:
阻尼+浮动舍入错误
是的,您正在进行阻尼,因此如果您的对象正在下落,那么它将再次反弹(高度h0
)再次下落,然后再次反弹(高度h1发生碰撞时,尝试将y位置设置为飞机的y位置。如果| speed |您没有使用Unity的内置碰撞器吗?我选择在不使用碰撞器的情况下实施碰撞检测/反应。