Collision detection java

Started by guess, January 10, 2010, 12:53:59

Previous topic - Next topic

guess

hi,
i´ve got a problem with the Collision of the balls in my java game.you see the source code below.
i start the game and the balls do a collision (that´s great!) and roll in the opposite way (speedXY... *= -1;). picture here (1.jpg, see below):
http://www.imgimg.de/bild_15e99c02ejpg.jpg.html
so it should be.


but the balls also do this process when they do not hit the other balls, they do this on every crossover between the x- and y-axis, picture here(2.jpg, see below):
http://www.imgimg.de/bild_278dfbdcfjpg.jpg.html


//ballR= ball radius
//ball i clashes with ball  j from left
                    if (ballxy[i][0] + ballR >= ballxy[j][0] - ballR) {
                                                speedXY[i][0] *= -1;
                    }

// ball i clashes with ball  j from right
                    if (ballxy[i][0] + ballR <= ballxy[j][0] - ballR) {
                        speedXY[j][0] *= -1;
                    }

//ball i clashes with ball  j from top
                    if (ballxy[i][1] - ballR <= ballxy[j][1] + ballR) {
                        speedXY[i][1] *= -1;
                    }

//ball i clashes with ball  j from bottom
                    if (ballxy[i][1] - ballR >= ballxy[j][1] + ballR) {
                        speedXY[j][1] *= -1;
                    }


i hope anybody can help me (source code necessary ;)) so that the balls do only roll the opposite way when they really hit another.
many thanks. You really helped me!!

elias4444

Here, I'll go a step further. Here's the code from my Martians Vs. Robots game for asteroid against asteroid bouncing. You'll want to edit the elasticity to fit your own needs. This may be more than what you actually wanted though:

/**
* This method is embedded in my Asteroid object for simple circular collision detection
**/
public boolean isCollision(Asteroid s2, float errormargin) {
	float dx,dy,minDistance;

	dx = (float)((x) - (s2.x));
	dy = (float)((y) - (s2.y));
	minDistance = (((getWidth() + errormargin)/2f)) + ((s2.getWidth() + errormargin) / 2f);
	return (dx*dx + dy*dy < minDistance*minDistance);
}


/**
* sp.scalesize = size of asteroid (usually between 5 and 30)
* sp.dx = x directional velocity
* sp.dy = y directional velocity
* sp.x = asteroid location x coordinate
* sp.y = asteroid location y coordinate
**/
private void bounceasteroidoffasteroid(Asteroid sp1, Asteroid sp2) {
	double ed = 0.5f; // elasticity <= 1f (perfect elasticity = 1f)
	double mass1 = sp1.scalesize;
	double mass2 = sp2.scalesize;

	//// Vector for collision direction
	double dx = sp2.x-sp1.x, dy = sp2.y-sp1.y;
	double distance = Math.sqrt(dx*dx+dy*dy);
	double ax=dx/distance, ay=dy/distance;

	//// Projection of the velocities
	double va1=(sp1.dx*ax+sp1.dy*ay), vb1=(-sp1.dx*ay+sp1.dy*ax);
	double va2=(sp2.dx*ax+sp2.dy*ay); double vb2=(-sp2.dx*ay+sp2.dy*ax);

	//// New velocities (after collision)
	double vaP1=va1 + (1+ed)*(va2-va1)/(1+mass1/mass2);
	double vaP2=va2 + (1+ed)*(va1-va2)/(1+mass2/mass1);

	//// Undo the projections
	sp1.dx=(float)(vaP1*ax-vb1*ay); 
	sp1.dy=(float)(vaP1*ay+vb1*ax);
	sp2.dx=(float)(vaP2*ax-vb2*ay); 
	sp2.dy=(float)(vaP2*ay+vb2*ax);

}
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com