So I'm trying to create a pong game using monogame and im having some issue with my hit detection. I want the ball to switch directions when it hits the left padddle but for some reason the collision only gets detected on the top, not the sides so the ball just goes through the sides of the paddle but bounces when it hits the top can anyone see what am i doing wrong? Heres my code
Part of The Bat Class
public class Bat : DrawableGameComponent
{
private SpriteBatch spriteBatch;
private Texture2D tex;
private Vector2 speed;
private Vector2 position;
private Vector2 stage;
public Rectangle Rectangle
{
get
{
return new Rectangle((int)position.X, (int)position.Y, tex.Width, tex.Height);
}
}
public Bat(Game game,
SpriteBatch spriteBatch,
Texture2D tex,
Vector2 position,
Vector2 speed,
Vector2 stage): base(game)
{
this.spriteBatch = spriteBatch;
this.tex = tex;
this.position = position;
this.speed = speed;
this.stage = stage;
}
The Collision classes update method
public override void Update(GameTime gameTime)
{
if (ball.Rectangle.Intersects(bat.Rectangle))
{
ball.speed.Y = -Math.Abs(ball.speed.Y);
hitSound.Play();
}
base.Update(gameTime);
}
And the game class that adds the bat and collision detection
Texture2D batTex = this.Content.Load<Texture2D>("Images/BatLeft");
Vector2 batPos = new Vector2(100,100);
Vector2 batSpeed = new Vector2(4, 0);
bat = new Bat(this, spriteBatch, batTex, batPos, batSpeed, stage);
this.Components.Add(bat);
SoundEffect dingSound = this.Content.Load<SoundEffect>("Music/ding");
CollisionDetection cd = new CollisionDetection(this, ball, bat, dingSound);
this.Components.Add(cd);
Rectangleclass instead of an unique variable), try using the rectangle as a standard variable without the get method. and call it from the public variable right away.