0
\$\begingroup\$

I'm trying to find a way to handle reflections for a breakout clone.

I would upload an image to the post instead of the following paragraph, however i have not yet gained the privilege of that yet.

If the ball intersects the left hand side i want it to bounce off to the left. if the ball intersects the right hand side i want it to bounce off to the right. if the ball intersects the middle section i want it to bounce up the way. i want to learn how to make it bounce in a varying direction dependant on what side of the left, right, or middle section was intersected

I would like to not use three separate rectangles for this, i want to learn how to do it with one.

I use a Vector2 for ball velocity, projVel.

It's position is projPos.

A rectangle for the paddle lightRect.

The reason I use proj.collRect for the beginning of the if is because I cannot use the intersect method with Vector2.

This is my makeshift collision handler at present, which does work but the speed changes to an extent which renders the game unplayable. The speed clamp i have only slightly slows it down i think. i have a variable for projSpeed i cannot clamp that or it will never be able to stop.

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));

Help me by showing me how I could do this, maybe in the Math. method, or even an alternative to .Intersects so I can use projPos instead of collRect.

I really am not sure where to start, if there is another way I could do it an example would be great.

\$\endgroup\$
1
\$\begingroup\$

I assume you mean the horizontal speed changes too much. If that is the case, there are two potential problems in this line of code:

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

First, I assume you're attempting to normalize the position of the projectile with respect to the distance between the endpoints of the paddle. If this is the case, you want to change this to

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

Second, you did not mention if the speed is too large or too small. If it is too large, perhaps the factor of 10 you are using is too large.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.