Jump to content
  • Advertisement
Sign in to follow this  
TheDodo

Bullet Collision Help

This topic is 2588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have my bullet collision working properly in every situation except for one. This is a 2D shooter in C#, when the player sprite is pushing against a wall either going to left to right or bottom to top the bullet does not detect any collision, but if it is from right to left or top to bottom it detects the collision. The bullet spawns at the edge of the sprite with the center being on the edge.

Here is the collision code I am using. I am comparing the current position and next position of the bullet against the position of the wall to determine if the bullet will collide with the wall on the next frame. Then I move the bullet to the position of the wall, and use the rectangles to detect the collision.


private void CollideWall()
{
foreach (Vertical aVert in level.Vertical)
{
if (collisionRect.Intersects(aVert.outerRect))
{
if (position.X >= aVert.collisionRect.Right && nextPos.X <= aVert.collisionRect.Right)
{
position.X = aVert.collisionRect.Right;
}
if (position.X <= aVert.collisionRect.Left && nextPos.X >= aVert.collisionRect.Left)
{
position.X = aVert.collisionRect.Left;
}
if (collisionRect.Intersects(aVert.collisionRect))
{
damage = 0;
visible = false;
}
}
}
foreach (Horizontal aHoriz in level.Horizontal)
{
if (collisionRect.Intersects(aHoriz.outerRect))
{
if (position.Y >= aHoriz.collisionRect.Bottom && nextPos.Y <= aHoriz.collisionRect.Bottom)
{
position.Y = aHoriz.collisionRect.Bottom;
}
if (position.Y <= aHoriz.collisionRect.Top && nextPos.Y >= aHoriz.collisionRect.Top)
{
position.Y = aHoriz.collisionRect.Top;
}
if (collisionRect2.Intersects(aHoriz.collisionRect))
{
damage = 0;
visible = false;
}
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!