Skip to main content
added 160 characters in body
Source Link
Shroeder
  • 323
  • 1
  • 13

Give your shiptank a Property that Tracks where its Bounds are. Give all other objects a Bounds as well. Check against the two rectangles to see if they are binding.

Rectangle rect1 = new Rectangle(x,y,width,height);
Rectangle rect2 = new Rectangle(x,y,width,height);
 

x and y would be your sprites location in the world (Vector2.X and Vector2.Y) Width and Height would be the width and height of the sprites rectangle

//Trap movement before you allow them to bind

GameTime gameTime = this.GameTime;

//Set a velocity based on gametime passed
Vector2 velocity = (150,0);
Vector2 velocityup = (0,150);

//lets say rect1 is your tank
//lets also assume for now that we are moving up x=x y--
//rect1.x += (float)(gameTime.EllapsedGameTime.MilleSeconds * velocity);
rect1.y -= (float)(gameTime.EllapsedGameTime.MilleSeconds * velocityup);

//rect1 now is the position at which our tank would be in a non collision scenario
//so we will check to see if that position would indeed collide with something

if (rect1.Intersects(rect2){

    //we collide
    //dont allow the tank to move in this direction

}

I think the problem you are having is that your measurement of movement is not fine enough, and that you allow the movement first, and then check to prevent it, whereas in this manner you first check, then move.

By using Vector 2's and gametime, we know exactly where our object are, and can trap for the finest of movements. Let me know if you still have some issues here, I know this explanation wasn't the best, but I am new as well so apologies ahead of time man.

Give your ship a Property that Tracks where its Bounds are. Give all other objects a Bounds as well. Check against the two rectangles to see if they are binding.

Rectangle rect1 = new Rectangle(x,y,width,height);
Rectangle rect2 = new Rectangle(x,y,width,height);
 
//Trap movement before you allow them to bind

GameTime gameTime = this.GameTime;

//Set a velocity based on gametime passed
Vector2 velocity = (150,0);
Vector2 velocityup = (0,150);

//lets say rect1 is your tank
//lets also assume for now that we are moving up x=x y--
//rect1.x += (float)(gameTime.EllapsedGameTime.MilleSeconds * velocity);
rect1.y -= (float)(gameTime.EllapsedGameTime.MilleSeconds * velocityup);

//rect1 now is the position at which our tank would be in a non collision scenario
//so we will check to see if that position would indeed collide with something

if (rect1.Intersects(rect2){

    //we collide
    //dont allow the tank to move in this direction

}

I think the problem you are having is that your measurement of movement is not fine enough, and that you allow the movement first, and then check to prevent it, whereas in this manner you first check, then move.

By using Vector 2's and gametime, we know exactly where our object are, and can trap for the finest of movements. Let me know if you still have some issues here, I know this explanation wasn't the best, but I am new as well so apologies ahead of time man.

Give your tank a Property that Tracks where its Bounds are. Give all other objects a Bounds as well. Check against the two rectangles to see if they are binding.

Rectangle rect1 = new Rectangle(x,y,width,height);
Rectangle rect2 = new Rectangle(x,y,width,height);

x and y would be your sprites location in the world (Vector2.X and Vector2.Y) Width and Height would be the width and height of the sprites rectangle

//Trap movement before you allow them to bind

GameTime gameTime = this.GameTime;

//Set a velocity based on gametime passed
Vector2 velocity = (150,0);
Vector2 velocityup = (0,150);

//lets say rect1 is your tank
//lets also assume for now that we are moving up x=x y--
//rect1.x += (float)(gameTime.EllapsedGameTime.MilleSeconds * velocity);
rect1.y -= (float)(gameTime.EllapsedGameTime.MilleSeconds * velocityup);

//rect1 now is the position at which our tank would be in a non collision scenario
//so we will check to see if that position would indeed collide with something

if (rect1.Intersects(rect2){

    //we collide
    //dont allow the tank to move in this direction

}

I think the problem you are having is that your measurement of movement is not fine enough, and that you allow the movement first, and then check to prevent it, whereas in this manner you first check, then move.

By using Vector 2's and gametime, we know exactly where our object are, and can trap for the finest of movements. Let me know if you still have some issues here, I know this explanation wasn't the best, but I am new as well so apologies ahead of time man.

Source Link
Shroeder
  • 323
  • 1
  • 13

Give your ship a Property that Tracks where its Bounds are. Give all other objects a Bounds as well. Check against the two rectangles to see if they are binding.

Rectangle rect1 = new Rectangle(x,y,width,height);
Rectangle rect2 = new Rectangle(x,y,width,height);

//Trap movement before you allow them to bind

GameTime gameTime = this.GameTime;

//Set a velocity based on gametime passed
Vector2 velocity = (150,0);
Vector2 velocityup = (0,150);

//lets say rect1 is your tank
//lets also assume for now that we are moving up x=x y--
//rect1.x += (float)(gameTime.EllapsedGameTime.MilleSeconds * velocity);
rect1.y -= (float)(gameTime.EllapsedGameTime.MilleSeconds * velocityup);

//rect1 now is the position at which our tank would be in a non collision scenario
//so we will check to see if that position would indeed collide with something

if (rect1.Intersects(rect2){

    //we collide
    //dont allow the tank to move in this direction

}

I think the problem you are having is that your measurement of movement is not fine enough, and that you allow the movement first, and then check to prevent it, whereas in this manner you first check, then move.

By using Vector 2's and gametime, we know exactly where our object are, and can trap for the finest of movements. Let me know if you still have some issues here, I know this explanation wasn't the best, but I am new as well so apologies ahead of time man.