Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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){
	        Vector2d movevec = new Vector2d(player.getVelocityX(), player.getVelocityY());
	        float dist = player.getDistance(otherSprite);        
	        float sumRadii = (player.getRadius() + otherSprite.getRadius());
	        dist -= sumRadii;
            if(movevec.length() < dist){
            // Normalize the moveve
            Vector2d N = movevec;
            // 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){
    		//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
    	    float sumRadiiSquared = sumRadii * sumRadii;
    	    if(F >= sumRadiiSquared){
    	    // 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){
    	     //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){
    	    //Set the length of the movevec so that the circles will just

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) {

	  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
	public void normalize() {
    Returns the dot product of this vector and the specified
	public float getDotProduct(Vector2d v) {
	    return x*v.x + y*v.y;
	public void times(float 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 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!