XNA C# Rectangle Intersect Ball on a Square

Posted by user2436057 on Game Development See other posts from Game Development or by user2436057
Published on 2013-07-01T12:52:55Z Indexed on 2013/07/01 16:31 UTC
Read the original article Hit count: 302

Filed under:
|
|

I made a Game like Peggle Deluxe using C# and XNA for lerning.

I have 2 rectangles a ball and a square field. The ball gets shoot out with a cannon and if the Ball hits the Square the Square disapears and the Ball flys away.But the Ball doesent spring of realistically, it sometimes flys away in a different direction or gets stuck on the edge.

Thads my Code at the moment:

public void Update(Ball b, Deadline dl) 
{ 

    ArrayList listToDelete = new ArrayList(); 
    foreach (Field aField in allFields) 
    { 
        if (aField.square.Intersects(b.ballhere)) 
        { 
            listToDelete.Add(aField); 
            Punkte = Punkte + 100; 

            float distanceX = Math.Abs(b.ballhere.X - aField.square.X); 
            float distanceY = Math.Abs(b.ballhere.Y - aField.square.Y); 

            if (distanceX < distanceY) 
            { 
                b.myMovement.X = -b.myMovement.X; 
            } 
            else 
            { 
                b.myMovement.Y = -b.myMovement.Y; 
            } 

        } 
    }

It changes the X or Y axis depending on how the ball hits the Square but not everytimes.

What could cause the problem?

Thanks for your answer.

Greetings from Switzerland.

© Game Development or respective owner

Related posts about XNA

Related posts about c#