# Problems with two circles collision detectioning

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

## Recommended Posts

Hello! Im using this tutorial below my post to build collision detection in my ball game. There is just so much new things about Vectors and so that its driving me crazy :) So here is my collision detection method. It should check if ball collided with other balls and then move ball near by other ball. So far it wont work, those balls are just jumping crazy if they collided.
public void testSpriteCollisionOnlyWithPlayers(Player player) {

Iterator p = map.getPlayers();
while(p.hasNext()) {

Player otherSprite = (Player)p.next();

if(player == otherSprite){
continue;
}

Vector2d movevec = new Vector2d(player.getVelocityX(), player.getVelocityY());

float dist = player.getDistance(otherSprite);
if(movevec.length() < dist){
continue;
}

// Normalize the moveve
Vector2d N = movevec;
N.normalize();
// Find C, the vector from the center of the moving
//circle A to the center of B
Vector2d C = new Vector2d(otherSprite.getCenterX()-player.getCenterX(), otherSprite.getCenterY()-player.getCenterY());

//D = N . C = ||C|| * cos(angle between N and C)
float D = N.getDotProduct(C);
// Another early escape: Make sure that A is moving
//towards B! If the dot product between the movevec and
//B.center - A.center is less that or equal to 0,
//A isn't moving towards B
if(D <= 0){
continue;
}
//Find the length of the vector C
float lengthC = C.length();
float F = (lengthC * lengthC) - (D * D);
// Escape test: if the closest that A will get to B
//is more than the sum of their radii, there's no
//way they are going collide
continue;
}
// We now have F and sumRadii, two sides of a right triangle.
//Use these to find the third side, sqrt(T)
float T = sumRadiiSquared - F;
//If there is no such right triangle with sides length of
//sumRadii and sqrt(f), T will probably be less than 0.
//Better to check now than perform a square root of a
//negative number.
if(T < 0){
continue;
}
//Therefore the distance the circle has to travel along
//movevec is D - sqrt(T)
float distance = D - (float)Math.sqrt(T);
//Get the magnitude of the movement vector
float mag = movevec.length();

//Finally, make sure that the distance A has to move
//to touch B is not greater than the magnitude of the
//movement vector.
if(mag < distance){
continue;
}

//Set the length of the movevec so that the circles will just
//touch
movevec.normalize();
movevec.times(distance);
player.setVelocityX(movevec.x);
player.setVelocityY(movevec.y);
continue;
}
}


Well here is my vector2d class also if the problem is there.
package util;

public class Vector2d {

public float x;
public float y;

/**
Creates a new Vector2d with the specified (x, y) values.
*/
public Vector2d(float x, float y) {
setTo(x, y);
}

/**
Sets this vector to the specified (x, y) values.
*/
public void setTo(float x, float y) {
this.x = x;
this.y = y;
}

/**
Divides this vector by the specified value. The new
length of this vector will be length()/s.
*/
public void divide(float s) {
x/=s;
y/=s;
}

/**
Returns the length of this vector as a float.
*/
public float length() {
return (float)Math.sqrt(x*x + y*y);
}

/**
Converts this Vector2d to a unit vector, or in other
words, a vector of length 1. Same as calling
v.divide(v.length()).
*/
public void normalize() {
divide(length());
}

/**
Returns the dot product of this vector and the specified
vector.
*/
public float getDotProduct(Vector2d v) {
return x*v.x + y*v.y;
}
/**

*/
public void times(float p){
this.x=this.x*p;
this.y= this.y*p;
}

}


http://www.gamasutra.com/features/20020118/vandenhuevel_01.htm [Edited by - arachidyl on November 27, 2005 1:32:51 PM]

##### Share on other sites
A good tip to just test ur collision detection before adding specific response is to Swap the Velocities of the 2 balls over

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 11
• 28
• 15
• 39
• ### Forum Statistics

• Total Topics
634835
• Total Posts
3019539
×