简体   繁体   中英

Get get collision location on object

I try to make a Pong like video game using C#.

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.

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

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. Then I got the value I wanted by subtract the ball Y position by the paddle 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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