Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#XNA使对象保持在边界内_C#_Xna - Fatal编程技术网

C#XNA使对象保持在边界内

C#XNA使对象保持在边界内,c#,xna,C#,Xna,我目前有一个方法可以检查我是否从上/下/侧面越界。物体本身是一个球,我有一个问题,关于如何让球正确地从边缘反弹?我该怎么办 //这种行为不是我想要的 if ( InsideOfBounds ) { Vector3 mCenter = Ball.getCenter(); Vector3 normalizeV = tempCenter; normalizeV.Normalize(); mHeroBall.setVelocity(-testSpeed * nor

我目前有一个方法可以检查我是否从上/下/侧面越界。物体本身是一个球,我有一个问题,关于如何让球正确地从边缘反弹?我该怎么办

//这种行为不是我想要的

if ( InsideOfBounds )
{
     Vector3 mCenter = Ball.getCenter();
     Vector3 normalizeV = tempCenter;
     normalizeV.Normalize();
     mHeroBall.setVelocity(-testSpeed * normalizeV);
}

我可以为您提供一个使用XNA编写的突破克隆的示例:


基本上,你翻转速度的右分量来做一个“完美”的反弹。如果需要,可以通过乘以系数(如0.95或1.1)来增加摩擦力或弹性,从而改变球的速度。

当您更新对象(球)的位置时,您需要检查新值是否超出范围(以及顶部\底部还是左侧\右侧)。如果实际超出边界,则翻转速度向量中的正确元素

示例:如果球已通过左边界,则BallSpeed.X=-BallSpeed.X

在这一点上,不要忘记用新的速度而不是旧的速度更新球的位置,否则它将从屏幕上飞出当前帧(除非这不是问题)