Sphere vs. line collision

Started by elias4444, June 03, 2006, 15:00:38

Previous topic - Next topic


Ok... I'm trying to optimize my game as much as possible. I have some instances where I need to detect a collision between a sphere and a line (the line can be any length and any angle, and can be approached from either side). I also need to calculate the "bounce" from off the wall.

I have the bouncing effect implemented as follows:

private void bounceshipoffwall(Player sp1, int[] whichwalls) {
		Wall whichwall = walls.get(whichwalls[0]);

		//define the matrix elements...
		float my = whichwall.getMY(whichwalls[1]);
		float mx = whichwall.getMX(whichwalls[1]);

		float x1 = (1-((my/mx)*(my/mx)))/(1+((my/mx)*(my/mx)));
		float x2 = 2*(my/mx)/(1+((my/mx)*(my/mx)));
		//then perform the matrix-vector multiplication:
		float ux = x1*sp1.dx + x2*sp1.dy;
		float uy = x2*sp1.dx - x1*sp1.dy;
		sp1.x = sp1.lastx;
		sp1.y = sp1.lasty;
		sp1.rotz = sp1.lastrotz;
		sp1.dx = ux;
		sp1.dy = uy;

However, with my rudimentary math skills, I can't figure out how to implement the above in a situation without perfect elasticity (how do you add elasticity variables?).

The bouncing isn't as important as the collision detection, but anyone who could help with either would be greatly appreciated.

Also, on the same note... is there a good (and recommended) book for 3D game mathematics? I could sure use one for optimizing things further.



shameless bump...  :oops:

Anyone? For right now, the only way I've found to detect the line vs. sphere collision is follow the equation for the line pixel-by-pixel and check to see if it's in the sphere. Obviously, that's quite intensive.

I'm guessing this can't be too uncommon, as I believe Cas does it with some of his games (think beams shooting at aliens, and the beam collides and knows where to stop).