• Create Account

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

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1rubsnick  Members   -  Reputation: 106

Like
0Likes
Like

Posted 15 February 2012 - 07:27 AM

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;
{
movingUp = true;
}
{
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;
{
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.

### #2iedoc  Members   -  Reputation: 496

Like
1Likes
Like

Posted 15 February 2012 - 09:44 AM

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.pos = (0, 0);

ball2.pos = (0, 11);

ball3.pos = (0, 9);

bool checkCollision(object obj1, object obj2)
{
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

http://braynzarsoft....php?p=D3D11BVCD
Braynzar Soft - DirectX Lessons & Game Programming Resources!

### #3Tom KQT  Members   -  Reputation: 1291

Like
0Likes
Like

Posted 16 February 2012 - 07:48 AM

Just a small note, you've got a typo there, the condition should be
if(objDistance < collisionDistance)

### #4iedoc  Members   -  Reputation: 496

Like
0Likes
Like

Posted 16 February 2012 - 10:05 AM

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 ;)
Braynzar Soft - DirectX Lessons & Game Programming Resources!

### #5blackbook  Members   -  Reputation: 110

Like
0Likes
Like

Posted 16 February 2012 - 01:45 PM

You can do Bounding Sphere collision detection for the balls

and bounding box to bounding sphere for the ball to paddle

if you dont know what they are type them in the IDE and highlight and hit F1.

Here's a link for Bounding volumes collision.

http://msdn.microsoft.com/en-us/library/bb313876.aspx

### #6rubsnick  Members   -  Reputation: 106

Like
0Likes
Like

Posted 21 February 2012 - 12:31 PM

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 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();
}
{
movingUp = true;
movingLeft = true;
movingUp2 = true;
movingLeft2 = true;

spriteBatch = new SpriteBatch(GraphicsDevice);

ball1Position=new Vector2((graphics.GraphicsDevice.Viewport.Width/2)-(ball1.Width/2), graphics.GraphicsDevice.Viewport.Height-115);

ball2Position = new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) - ((ball2.Width / 2)+250), graphics.GraphicsDevice.Viewport.Height - 115);
}

{

}

protected override void Update(GameTime gameTime)
{

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

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
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
{
{
this.Exit();
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && paddlePosition.X >= 0)
{
}
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && paddlePosition.X <= (graphics.GraphicsDevice.Viewport.Width - 150))
{
}
}
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;
{
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;
{
movingUp2 = true;
}
}
{
if ((ball1Position.Y + ball1.Height) >= paddlePosition.Y &&
(ball1Position.Y + ball1.Height) < (paddlePosition.Y + 4) &&
(ball1Position.X + ball1.Width) > paddlePosition.X &&
{
return true;
}
else
{
return false;
}
}
{
if ((ball2Position.Y + ball2.Height) >= paddlePosition.Y &&
(ball2Position.Y + ball2.Height) < (paddlePosition.Y + 4) &&
(ball2Position.X + ball2.Width) > paddlePosition.X &&
{
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.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS