locked
Collision Detection RRS feed

  • Question

  • Ok, i know this has already been posted on but after going through that i still couldn't get it to work.  So here's my code and plz tell me what's wrong.

    Texture2D _paddle1;
    Texture2D _paddle2;
    Texture2D _ball;
    Vector2 _paddle1pos;
    Vector2 _paddle2pos;
    Vector2 _ballpos;
    Vector3 _p1box;
    Vector3 _p1box2;
    Vector3 _p2box;
    Vector3 _p2box2;
    Vector3 _bbox;
    Vector3 _bbox2;
    BoundingBox bb1 = new BoundingBox();
    BoundingBox bb2 = new BoundingBox();
    BoundingBox bb3 = new BoundingBox();
    SpriteBatch spriteBatch;

    _paddle1pos = new Vector2(50, 300);
    _paddle2pos =
    new Vector2(1000, 300);
    _ballpos =
    new Vector2(400, 300);
    _p1box =
    new Vector3(_paddle1pos.X, _paddle1pos.Y,0);
    _p1box2 =
    new Vector3(_paddle1pos.X + 14, _paddle1pos.Y + 67,0);
    _p2box =
    new Vector3(_paddle2pos.X, _paddle2pos.Y,0);
    _p2box2 =
    new Vector3(_paddle2pos.X + 14, _paddle2pos.Y + 67,0);
    _bbox =
    new Vector3(_ballpos.X, _ballpos.Y,0);
    _bbox2 =
    new Vector3(_ballpos.X + 33, _ballpos.Y + 32,0);

    bb1.Min = _p1box; bb3.Min = _p2box; bb2.Min = _bbox;
    bb1.Max = _p1box2; bb3.Max = _p2box2; bb2.Max = _bbox2;

    if (bb3.Intersects(bb1))
    {
    _ballHspeed *= -2;
    _paddle1Hspeed *= -2;
    _paddle2Hspeed *= -2;
    }
    if (bb3.Intersects(bb2))
    {
    _ballHspeed *= -2;
    _paddle1Hspeed *= -2;
    _paddle2Hspeed *= -2;
    }
    if (bb1.Intersects(bb2))
    {
    _ballHspeed *= -2;
    _paddle1Hspeed *= -2;
    _paddle2Hspeed *= -2;
    }

    Thanks,
    DragonC#

    Sunday, November 19, 2006 7:31 AM

Answers

  • You're also doing this:

    _paddle1pos = new Vector2(50, 300);
    _paddle2pos =
    new Vector2(1000, 300);
    _ballpos =
    new Vector2(400, 300);

    which means you're never checking the paddle or ball against an updated position, it's always a hard-coded position. Where is this code in your project?

    Sunday, November 19, 2006 6:48 PM

All replies

  • Maybe if you tell us what's happening we can better help.
    Sunday, November 19, 2006 1:05 PM
  • Sorry, it was late 2:00 in the morning to be exact.  Ok above you can see i decleared  my boxes, but when the program runs the ball still goes right through them, no collision.  That is where my problem is and thanks for replying.
    Sunday, November 19, 2006 6:20 PM
  • Your code does not actually call any intersection routines.

    For each time step, you have to check each object against any other object it may intersect, and if you find that they're moving into each other, handle the collision by affecting the position, velocity, or forces on your simulated objects. XNA is not a simulation engine, and does nothing of that for you.

    Sunday, November 19, 2006 6:45 PM
  • You're also doing this:

    _paddle1pos = new Vector2(50, 300);
    _paddle2pos =
    new Vector2(1000, 300);
    _ballpos =
    new Vector2(400, 300);

    which means you're never checking the paddle or ball against an updated position, it's always a hard-coded position. Where is this code in your project?

    Sunday, November 19, 2006 6:48 PM
  • Ok heres the code that moves the ball

    _ballpos.X += _ballHspeed;
    _ballpos.Y += _ballVspeed;

    int BallMaxX = Window.ClientWidth - _ball.Width;
    int BallMinX = 0;
    int BallMaxY = Window.ClientHeight - _ball.Height;
    int BallMinY = 0;

    if (_ballpos.X > BallMaxX)
    {
    _ballHspeed *= -1;
    _ballpos.X = BallMaxX;
    }
    else if (_ballpos.X < BallMinX)
    {
    _ballHspeed *= -1;
    _ballpos.X = BallMinX;
    }
    if (_ballpos.Y > BallMaxY)
    {
    _ballVspeed *= -1;
    _ballpos.Y = BallMaxY;
    }
    else if (_ballpos.Y < BallMinY)
    {
    _ballVspeed *= -1;
    _ballpos.Y = BallMinY;
    }

    And the paddles are the same for the collision testing.

    Sunday, November 19, 2006 8:10 PM
  • After i read your post about ten times i realized what you where trying to say thanks

     

    Sunday, November 19, 2006 9:37 PM