Jump to content
  • Advertisement
Sign in to follow this  
rubsnick

Trying to do ball to ball collicions.... and it's not working.

This topic is 2485 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've made the paddle and everything and it works but when they touch it they don't recognize one another so I tried to implement this collision but it doesn't seem to work properly. I"m assuming I might need more variables but I"m not entirely sure. :/ I know the code might be a bit messy I"ve been taking from everywhere to learn this. So any tips?

This chunk controls the overall movement of the balls

public partial class Game1
{
public void Ball1mover()
{
if (movingUp)
{
ball1Position.Y -= 3;
}
else
{
ball1Position.Y += 3;
}
if (movingLeft)
{
ball1Position.X -= 3;
}
else
{
ball1Position.X += 3;
}
if (ball1Position.X <= 0 && movingLeft) movingLeft = false;
if (ball1Position.Y <= 0 && movingUp) movingUp = false;
if (ball1Position.X >= (graphics.GraphicsDevice.Viewport.Width - ball1.Width) && !movingLeft) movingLeft = true;
if (DetectPaddleBallCollision())
{
movingUp = true;
}
if (DetectPaddleBallCollision())
{
movingUp = true;
}
}
public void Ball2mover()
{
if (movingUp2)
{
ball2Position.Y -= 3;
}
else
{
ball2Position.Y += 3;
}
if (movingLeft2)
{
ball2Position.X -= 3;
}
else
{
ball2Position.X += 3;
}
if (ball2Position.X <= 0 && movingLeft2) movingLeft2 = false;
if (ball2Position.Y <= 0 && movingUp2) movingUp2 = false;
if (ball2Position.X >= (graphics.GraphicsDevice.Viewport.Width - ball1.Width) && !movingLeft2) movingLeft2 = true;
if (DetectPaddleBallCollision2())
{
movingUp2 = true;
}
}


This code works I just think it'll help with the understanding the movement. The detectPaddleBallcollision methods return true or false of the boolean Movingup and moving left.

So I tried to do somethign that came to mind which is this

public void BalltoBallCollision()
{
//Controlls ball 1 movement
{
if (ball1Position.Y == ball2Position.Y )
movingUp = false;
if (ball1Position.X == ball2Position.X)
movingLeft = false;}
if (ball1Position.Y == ball2Position.Y )
movingUp2 = true;
if (ball1Position.X == ball2Position.X)
movingLeft2 = true;
}


When I implement that the balls do collide but I have a problem with the paddle, it'll somehow control both of them... so I"m not entirely sure how to "fix" it... I'm thinking of adding several other boolean variable so they don't interfear with the general movement. But I don't know how I would implement them.

Anyway So any idea on what I could do here? If need be I"ll uplaod the entire project if it'll make things easier.

Share this post


Link to post
Share on other sites
Advertisement
give the ball a radius (from center of ball to the edge). then, add together the two ball's radius', and if the distance between the centers of the two balls is less than the two radius's added together, they are colliding. something like this:


object ball1, ball2, ball3;

ball1.radius = 5;
ball1.pos = (0, 0);

ball2.radius = 5;
ball2.pos = (0, 11);

ball3.radius = 5;
ball3.pos = (0, 9);

bool checkCollision(object obj1, object obj2)
{
float collisionDistance = obj1.radius + obj2.radius;
float2 centerToCenterVector = obj1.pos - obj2.pos; // This is actually a vector. You can use this vector to add to the balls position to make it "bounce" off the other ball

// Maybe your not using the xna math library, but you can find another function that gets the length of a vector
float objDistance = XMVectorGetX(XMVector2Length(centerToCenterVector)); // XMVector2Length return a vector will all elements equal to the length of the centerToCenterVector. we only need one of them, so we just get X.

if(objDistance >= collisionDistance)
{
return true; // They Collide
}

return false; // If we have not already returned true, then they are not colliding, and we return false
}

// Now check for the collisions
checkCollision(ball1, ball2); // Distance between ball1 and ball2 is 11. radius's added together is only 10. No collision. return false

checkCollision(ball1, ball3); // Distance between ball1 and ball3 is 9. radius's added together is 10. There was a collision. return true


Maybe this could be helpful:
http://braynzarsoft....php?p=D3D11BVCD

Share this post


Link to post
Share on other sites
Just a small note, you've got a typo there, the condition should be
if(objDistance < collisionDistance)

Share this post


Link to post
Share on other sites
thanks tom, haha, i basically just did that off the top of my head, so it was bound to have something like that at least ;)

Share this post


Link to post
Share on other sites
Hey thanks everyone... I'm still having difficulty. I"m understanding the theory behind it no problem. I just don't know how to update it... or Don't understand why it's not working. :/ I'm going to post my entire code everything works and I'm just kinda tired after trying to quite some time... Anyone have any ideas? Here is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Breakout
{
/// <summary>
/// This is the main type for your game
/// </summary>
public partial class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D paddle;
Vector2 paddlePosition = Vector2.Zero;
Texture2D ball1;
Vector2 ball1Position = Vector2.Zero;
Texture2D ball2;
Vector2 ball2Position = Vector2.Zero;
Boolean movingUp, movingLeft, movingUp2,movingLeft2;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{

base.Initialize();
}
protected override void LoadContent()
{
movingUp = true;
movingLeft = true;
movingUp2 = true;
movingLeft2 = true;

spriteBatch = new SpriteBatch(GraphicsDevice);
paddle = Content.Load<Texture2D>("Paddle");
paddlePosition= new Vector2((graphics.GraphicsDevice.Viewport.Width/2)-(paddle.Width/2), (graphics.GraphicsDevice.Viewport.Height-80));

ball1 = Content.Load<Texture2D>("ball");
ball1Position=new Vector2((graphics.GraphicsDevice.Viewport.Width/2)-(ball1.Width/2), graphics.GraphicsDevice.Viewport.Height-115);

ball2 = Content.Load<Texture2D>("ball");
ball2Position = new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) - ((ball2.Width / 2)+250), graphics.GraphicsDevice.Viewport.Height - 115);
}

protected override void UnloadContent()
{

}

protected override void Update(GameTime gameTime)
{

PaddleMover();
Ball1mover();
Ball2mover();
BalltoBallCollision();

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(paddle, paddlePosition, Color.White);
spriteBatch.Draw(ball1, ball1Position, Color.White);
spriteBatch.Draw(ball2, ball2Position, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}


}
}


Here is where all of the important stuff happends (movement etc) it's in the other partial class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Breakout
{
public partial class Game1
{
public void PaddleMover()
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && paddlePosition.X >= 0)
{
paddlePosition.X -= 15;
}
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && paddlePosition.X <= (graphics.GraphicsDevice.Viewport.Width - 150))
{
paddlePosition.X += 15;
}
}
public void Ball1mover()
{
if (movingUp)
{
ball1Position.Y -= 3;
}
else
{
ball1Position.Y += 3;
}
if (movingLeft)
{
ball1Position.X -= 3;
}
else
{
ball1Position.X += 3;
}
if (ball1Position.X <= 0 && movingLeft) movingLeft = false;
if (ball1Position.Y <= 0 && movingUp) movingUp = false;
if (ball1Position.X >= (graphics.GraphicsDevice.Viewport.Width - ball1.Width) && !movingLeft) movingLeft = true;
if (DetectPaddleBallCollision())
{
movingUp = true;
}
}
public void Ball2mover()
{
if (movingUp2)
{
ball2Position.Y -= 3;
}
else
{
ball2Position.Y += 3;
}
if (movingLeft2)
{
ball2Position.X -= 3;
}
else
{
ball2Position.X += 3;
}

if (ball2Position.X <= 0 && movingLeft2) movingLeft2 = false;
if (ball2Position.Y <= 0 && movingUp2) movingUp2 = false;
if (ball2Position.X >= (graphics.GraphicsDevice.Viewport.Width - ball1.Width) && !movingLeft2) movingLeft2 = true;
if (DetectPaddleBallCollision2())
{
movingUp2 = true;
}
}
public Boolean DetectPaddleBallCollision()
{
if ((ball1Position.Y + ball1.Height) >= paddlePosition.Y &&
(ball1Position.Y + ball1.Height) < (paddlePosition.Y + 4) &&
(ball1Position.X + ball1.Width) > paddlePosition.X &&
ball1Position.X < (paddlePosition.X + paddle.Width))
{
return true;
}
else
{
return false;
}
}
public Boolean DetectPaddleBallCollision2()
{
if ((ball2Position.Y + ball2.Height) >= paddlePosition.Y &&
(ball2Position.Y + ball2.Height) < (paddlePosition.Y + 4) &&
(ball2Position.X + ball2.Width) > paddlePosition.X &&
ball2Position.X < (paddlePosition.X + paddle.Width))
{
return true;
}
else
{
return false;
}
}
public void BalltoBallCollision()
{
//Controlls ball 1 movement
if (ball1Position.Y == ball2Position.Y && ball1Position.X == ball2Position.X)
{
ball2Position.Y = -ball2Position.Y;
ball1Position.Y = -ball1Position.Y;

}


}
}
}


Any help would be appreciated.... I obviousl have no idea what I"m doing. :(

Share this post


Link to post
Share on other sites
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!