简体   繁体   English

获取 object 上的碰撞位置

[英]Get get collision location on object

I try to make a Pong like video game using C#.我尝试使用 C# 制作类似 Pong 的视频游戏。

The basic game works well for me, but I want to divide the paddles into multiple zones.基本游戏对我来说效果很好,但我想将桨分成多个区域。 Each zone should move the ball individual.每个区域都应该单独移动球。

The problem is, that I don't really know how to get the correct zone of the paddle.问题是,我真的不知道如何获得桨的正确区域。 For example:例如:

if (collision >= 50px){move ball in direction A}
else if (collision <= 100px && collision > 50px){move ball to direction B}

I tried using the Y value of Bounds, but it doesn't worked.我尝试使用 Bounds 的 Y 值,但它不起作用。

The next step I try is to get the ball location and the paddle location to calculate the zone.我尝试的下一步是获取球位置和桨位置以计算区域。

Thanks for your help.谢谢你的帮助。

Part of the problem is that you are not taking care of all scenarios, for example (collision >= 50px) can be true and (collision <= 100px && collision > 50px) can also be true when collision == 75部分问题是您没有处理所有情况,例如 (collision >= 50px) 可能为真,当碰撞 == 75 时 (collision <= 100px && collision > 50px) 也可能为真

If you want to strictly determinate in which zone is your ball, you must use conditions more like this (I assume your variable collision is an integer)如果你想严格确定你的球在哪个区域,你必须使用更像这样的条件(我假设你的变量碰撞是一个整数)

if (collision >= int.MinValue && collision <= 50 ) // Move A
else if (collision > 50 && collision <= 100 ) // Move B
else if (collision > 100 && collision <= 150 ) // Move C
// Next areas...
else if (collision > 250 && collision <= int.MaxValue) // Move G

Just try to get only one condition with true at each moment, and you will get the correct area.只需尝试在每个时刻仅获得一个条件为真,您将获得正确的区域。 I hope it helps.我希望它有所帮助。

I was able to solve it on my own.我能够自己解决它。

I got the Y position of the paddle and the Y position of the ball.我得到了桨的 Y position 和球的 Y position。 Then I got the value I wanted by subtract the ball Y position by the paddle Y position.然后我通过桨 Y position 减去球 Y position 得到我想要的值。

Here is a snippet of the real source code:下面是一段真实的源代码:

//contact with right paddle
        else if (ball.Bounds.IntersectsWith(paddle2.Bounds))
        {
            double paddle2_locationvar = paddle2.Top;
            paddle2_Bounds_text.Text = paddle2_locationvar.ToString() + " " + ball.Top.ToString();

            double ball_locationvar = ball.Top;

            double zonevar = ball_locationvar - paddle2_locationvar;

            paddle2_Bounds_text.Text = zonevar.ToString();

            timer5.Stop();

            //zones
            if (zonevar <= 12.571)
            {
                ballx = ballx * -1;
                bally = 6;
                timer5.Start();
            }
            else if (zonevar > 12.571 && zonevar <= 25.142)
            {
                ballx = ballx * -1;
                bally = 4;
                timer5.Start();
            }
            else if (zonevar > 25.142 && zonevar <= 37.713)
            {
                ballx = ballx * -1;
                bally = 2;
                timer5.Start();
            }
            else if (zonevar > 37.713 && zonevar <= 50.284)
            {
                ballx = ballx * -1;
                bally = 0;
                timer5.Start();
            }
            else if (zonevar > 50.284 && zonevar <= 62.855)
            {
                ballx = ballx * -1;
                bally = -2;
                timer5.Start();
            }
            else if (zonevar > 62.855 && zonevar <= 75.426)
            {
                ballx = ballx * -1;
                bally = -4;
                timer5.Start();
            }
            else if (zonevar > 75.426 && zonevar <= 88)
            {
                ballx = ballx * -1;
                bally = -6;
                timer5.Start();
            }
            else
                ballx = ballx * -1;

Thank you very much for your effort.非常感谢您的努力。

Have a nice day祝你今天过得愉快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM