0
\$\begingroup\$

I have made some collision if statements, but they didn't work.

            birdbox3.X += 5;
            birdbox3.Y -= 5;

            if (birdbox3.Intersects(Banner1)) {

                birdbox3.Y += 10;

            }
            else if (birdbox3.Intersects(Banner2)) { 

                birdbox3.Y = -birdbox3.Y; }

So if we take the first statement, the box is initially on the left down corner. According to my code, it would ideally be going right+up in my game, once it hits the banner on the extreme top, it should go down+right, thus both the X and the Y positions will increase. But what happens is that is starts bouncing really fast, after debugging I realised it was forever stuck in the first if, it's almost as if it collides 2 times, reverting Y to its initial movement, colliding a 3rd time, and doing the process over and over.

Which brings us to the question, how can I make the Update code run a tad bit slower, or after half a second or such passes, so when it runs, it doesn't misinterpret collisions?

\$\endgroup\$
  • \$\begingroup\$ A drawing of the situation would make things a lot clearer. \$\endgroup\$ – MichaelHouse Mar 2 '15 at 6:48
0
\$\begingroup\$

I have found the solution to this somewhere else. Adding a bigger if statement such as this :

private int elapsedTime = 0; //Declared at class level

public override void Update(GameTime gameTime)
{
    elapsedTime += gameTime.ElapsedGameTime.TotalMilliseconds;

if (elapsedTime > 500)
{
    elapsedTime = 0;
    birdbox3.X += 5;
    birdbox3.Y -= 5;

    if (birdbox3.Intersects(Banner1))
    {
        birdbox3.Y += 10;
    }
    else if (birdbox3.Intersects(Banner2))
    {
        birdbox3.Y = -birdbox3.Y;
    }
}
}

easily splits frames apart from each other, which helps with handling such issues, additionally this code sort of goes too far down the Y axis, so it goes further than the banner before it actually tests for it. I hope this helps anyone running into the same problem.

\$\endgroup\$
  • \$\begingroup\$ I'm not sure artificially delaying your frame time is a good solution in general, but if there's no visible problems from doing this, it might be good enough in your case. \$\endgroup\$ – Christian Mar 2 '15 at 8:58
  • \$\begingroup\$ a better way to do this is birdbox3.X += gameTime.ElapsedGameTime.TotalSeconds * 0.5f; instead. \$\endgroup\$ – dimitris93 Mar 5 '15 at 9:49

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.