0

我正在尝试找到一种方法来处理突破克隆的反射。

我会将图片上传到帖子而不是下一段,但是我还没有获得这个特权。

如果球与左侧相交,我希望它反弹到左侧。如果球与右手边相交,我希望它反弹到右侧。如果球与中间部分相交,我希望它向上反弹。我想学习如何根据左侧、右侧或中间部分的哪一侧相交使其在不同的方向上反弹

我不想为此使用三个单独的矩形,我想学习如何使用一个。

我使用 Vector2 作为球速,projVel.

它的位置是projPos.

桨的矩形lightRect.

我使用proj.collRectif 开头的原因是因为我不能将 intersect 方法与 Vector2 一起使用。

这是我目前的临时碰撞处理程序,它确实有效,但速度变化到导致游戏无法玩的程度。我认为我的速度钳只会稍微减慢它。我有一个变量,因为projSpeed我无法钳制它,否则它将永远无法停止。

    if (proj.collRect.Intersects(lightSaber.lightRect))
    {
                proj.projPos.Y = lightSaber.lightRect.Y - proj.projTxr.Height;
                proj.projVel.Y *= -1;

        proj.projVel.X = 10 * (proj.projPos.X - lightSaber.lightRect.Center.X) / (lightSaber.lightRect.Center.X);
    }

            proj.projVel.X = Math.Max(-4, Math.Min(proj.projVel.X, 4));
            proj.projVel.Y = Math.Max(-4, Math.Min(proj.projVel.Y, 4));

通过向我展示如何做到这一点来帮助我,也许在数学中。方法,甚至是 .Intersects 的替代方法,因此我可以使用 projPos 而不是 collRect。

我真的不知道从哪里开始,如果有另一种方法可以做到这一点,一个例子会很棒。

4

1 回答 1

0

我建议您根据位置计算反射角,然后从角度和撞击前的速度推导出速度,而不是单独操纵 X 和 Y 速度。

例子:

// NOTE: this code assumes that positive Y is down
if (proj.collRect.Intersects(lightSaber.lightRect) && projPos.projVel.Y > 0.0f) // only bounce if projectile is moving downward
{
    // remember current speed for when we calculate new velocity
    var projSpeed = projVel.Length(); 

    // make sure the projectile no longer intersects the bar
    proj.projPos = lightRect.Y - proj.projTxr.Height;

    // interpolate reflection angle
    var t = (proj.projPos.X - lightSaber.lightRect.X) / lightSaber.lightRect.Width;
    var reflectDegrees = 150.0f - t * 120f; // straight up +/- 60 degrees
    var reflectRadians = reflectDegrees * (float)Math.PI / 180.0f;

    // final velocity determined by angle and original projectile speed
    proj.projVel = new Vector2((float)Math.Cos(reflectRadians) * projSpeed, -(float)Math.Sin(reflectRadians) * projSpeed);
}
于 2017-01-17T18:03:33.037 回答