I'm working on a pong game and am stuck with my collision between my "ball" and my "paddle2" objects. It seems my paddle position wont move with my paddle object, so instead of the ball bouncing off my paddle it bounces off an invisible circle where the paddle started. also instead of rebounding off the paddle start pos it goes to 0, 0 and comes down from the top left corner. my collision code is surounded in ////////////////////////////////// if that helps lol
Any help would be much appreciated!
ps. I know my code will look like crap to everyone but this is my first ever project and i am still learning the basics.
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D backgroundTexture;
Rectangle background = new Rectangle(0, 0, 850, 510);
Texture2D paddle1Texture;
Rectangle paddle1 = new Rectangle(115, 230, 60, 60);
Texture2D paddle2Texture;
Rectangle paddle2 = new Rectangle(675, 230, 60, 60);
Vector2 paddle2Pos;
Texture2D ballTexture;
Rectangle ball = new Rectangle(410, 245, 30, 30);
Vector2 ballPos;
Vector2 ballDir;
float ballSpeed = 6;
//p1score
public Game1()
: base()
{
graphics = new GraphicsDeviceManager(this);
graphics.IsFullScreen = false;
graphics.PreferredBackBufferHeight = 510;
graphics.PreferredBackBufferWidth = 850;
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
ballTexture = Content.Load<Texture2D>("ball");
ballPos = new Vector2(410, 245);
ballDir = new Vector2(1, 1);
ballDir.Normalize();
paddle1Texture = Content.Load<Texture2D>("paddle1");
paddle2Texture = Content.Load<Texture2D>("paddle2");
paddle2Pos = new Vector2(675, 230);
backgroundTexture = Content.Load<Texture2D>("background");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
//right paddle
if (Keyboard.GetState().IsKeyDown(Keys.Up))
paddle2.Y -= 7;
if (Keyboard.GetState().IsKeyDown(Keys.Down))
paddle2.Y += 7;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
paddle2.X -= 7;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
paddle2.X += 7;
if (paddle2.Y + 60 > 505)
paddle2.Y = 505 - 60;
if (paddle2.Y < 5)
paddle2.Y = 5;
if (paddle2.X < 427)
paddle2.X = 427;
if (paddle2.X + 60 > 845)
paddle2.X = 845 - 60;
//left paddle
if (Keyboard.GetState().IsKeyDown(Keys.W))
paddle1.Y -= 7;
if (Keyboard.GetState().IsKeyDown(Keys.S))
paddle1.Y += 7;
if (Keyboard.GetState().IsKeyDown(Keys.A))
paddle1.X -= 7;
if (Keyboard.GetState().IsKeyDown(Keys.D))
paddle1.X += 7;
if (paddle1.Y + 60 > 505)
paddle1.Y = 505 - 60;
if (paddle1.Y < 5)
paddle1.Y = 5;
if (paddle1.X + 60 > 423)
paddle1.X = 423 - 60;
if (paddle1.X < 5)
paddle1.X = 5;
//ball movement and boundary
//if (paddle2.Intersects(ball))
// ballSpeed = 6;
//if (paddle1.Intersects(ball))
// ballSpeed = 6;
{
}
{
ballPos += ballDir * ballSpeed;
ball = new Rectangle((int)ballPos.X, (int)ballPos.Y, 30, 30);
}
ballPos += ballDir * ballSpeed;
if (ballPos.Y + 35 > graphics.PreferredBackBufferHeight)
{
ballPos.Y = graphics.PreferredBackBufferHeight - 35;
ballDir.Y = -ballDir.Y;
}
if (ballPos.Y < 5)
{
ballPos.Y = 5;
ballDir.Y = -ballDir.Y;
}
if (ballPos.X + 35 > graphics.PreferredBackBufferWidth)
{
ballPos.X = graphics.PreferredBackBufferWidth - 35;
ballDir.X = -ballDir.X;
}
if (ballPos.X < 5)
{
ballPos.X = 5;
ballDir.X = -ballDir.X;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float distance = (ballPos.X - paddle2Pos.X) * (ballPos.X - paddle2Pos.X) + (ballPos.Y - paddle2Pos.Y) * (ballPos.Y - paddle2Pos.Y);
float radius = (15 + 30) * (15 + 30);
if (distance <= radius)
{
Vector2 dir = (ballPos = -paddle2Pos);
dir.Normalize();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(backgroundTexture, background, Color.White);
spriteBatch.Draw(paddle1Texture, paddle1, Color.White);
spriteBatch.Draw(paddle2Texture, paddle2, Color.White);
spriteBatch.Draw(ballTexture, ball, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}