# Simple Colision Detection

## Recommended Posts

I need help with Collision Detection. I'm making a simple Bomberman game. Right now I want help with detecting the edges of the screen. When I go to the left side of the screen it works, but it won't let me hit the right button when I hit it. When I hit the right button, it doesn't detect the right side and it just keeps going. Here is the code I have so far:
  if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
if (theMan.GraphicsDevice.Viewport.X < screenpos.X)
{
screenpos.X += theMan.Width / 2;
}
}

if (Keyboard.GetState().IsKeyDown(Keys.Left))
{

if (theMan.GraphicsDevice.Viewport.X < screenpos.X)
{

screenpos.X -= theMan.Width / 2;
}

}

I also want help with other things, but I'll start with this :)

##### Share on other sites
If I'm not much mistaken, the problem would seem to be that you have a "less-than" sign in the position test for movement both left and right.

##### Share on other sites
Well I have no idea what language you are using, but I assume this is generally the same for all languages. The x/y position is at the upper left side of the image rectangle. So if you want to collide with the right side of the screen you need to get themans position, then add his width.

As for your other issue, it's possible that theman is getting stuck behind the right side of the screen and having his position reset because of it.

edit: ^^ that to. Didn't catch that.

##### Share on other sites
@Thaumaturge If I'm not much mistaken, the problem would seem to be that you have a "less-than" sign in the position test for movement both left and right.

Well I played with it... and the left one works fine, but the right one does not. It doesn't even detect the right hand side of the screen.

##### Share on other sites
Quote:
 Original post by stuffradio@Thaumaturge If I'm not much mistaken, the problem would seem to be that you have a "less-than" sign in the position test for movement both left and right.Well I played with it... and the left one works fine, but the right one does not. It doesn't even detect the right hand side of the screen.

If you are moving to the left, do you care about the left edge of the viewport, or the right edge? Why? Do you want to keep your position less than, or greater than that X position? Why?

If you are moving to the right, do you care about the left edge of the viewport, or the right edge? Why? Do you want to keep your position less than, or greater than that X position? Why?

Which edge of the viewport does "theMan.GraphicsDevice.Viewport.X" represent? How is "the right edge" of the viewport represented?

##### Share on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by stuffradio@Thaumaturge If I'm not much mistaken, the problem would seem to be that you have a "less-than" sign in the position test for movement both left and right.Well I played with it... and the left one works fine, but the right one does not. It doesn't even detect the right hand side of the screen.

If you are moving to the left, do you care about the left edge of the viewport, or the right edge? Why? Do you want to keep your position less than, or greater than that X position? Why?

If you are moving to the right, do you care about the left edge of the viewport, or the right edge? Why? Do you want to keep your position less than, or greater than that X position? Why?

Which edge of the viewport does "theMan.GraphicsDevice.Viewport.X" represent? How is "the right edge" of the viewport represented?

If you are moving to the left, do you care about the left edge of the viewport, or the right edge?
The left edge.
Why?
I don't want him to be able to go off the screen.

Do you want to keep your position less than, or greater than that X position?
I want to keep my position less than the position of the left side of the screen.
Why?
I don't want him to be able to go off the screen.

If you are moving to the right, do you care about the left edge of the viewport, or the right edge? Why? Do you want to keep your position less than, or greater than that X position? Why?
See above.

Which edge of the viewport does "theMan.GraphicsDevice.Viewport.X" represent? How is "the right edge" of the viewport represented?
I think left. Right edge I'm not sure.

##### Share on other sites
Quote:
 I want to keep my position less than the position of the left side of the screen.

At all times your position needs to be both greater than the left edge of the screen, and less than the right edge of the screen. When you are moving left, you want to check to make sure your position doesn't become less than the left edge of the screen, and when you are moving right you want to check to make sure your position doesn't become grater than the right edge of the screen.

##### Share on other sites
Heh, yes, that was silly of me - I was partly right, I believe, but Zahlman has given to you the rest.

Quote:
 Originally posted by stuffradioIf you are moving to the left, do you care about the left edge of the viewport, or the right edge?The left edge.

Quote:
 Originally posted by stuffradioIf you are moving to the right, do you care about the left edge of the viewport, or the right edge? Why? Do you want to keep your position less than, or greater than that X position? Why?See above.

From this I presume that you mean that if you are moving to the right, you care about the left edge. Are you sure about this? How does one hit the left edge when moving right?

And, if I may add one question to those that Zahlman provided:

I presume that you know the x- and y- coordinates of the screen (at least one of which you have been using already, it seems), as well as the width and height of the screen.

How far away from the left side is the right side of the screen?

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628362
• Total Posts
2982266

• 10
• 9
• 13
• 24
• 11