Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Ball vs Ball game, Flash, Game development and Users contributions.

This is a prototype sent me by Rory Kamminga.

It’s an unfinished game where you control a blue ball with arrow keys. Your aim is to kick a red ball out of the stage.

Obviously the red ball will try to do the same with you.

The interesting thing is that Rory took the studies of Raman Pfaff’s Physics of an Elastic Collision and its derivated tutorials such as Managing ball vs ball collision with Flash by myself and Elastic collisions by Marijn van Aerle and come out with a good prototype where the red ball is moved by AI.

That’s what Rory says:

Hi Emanuele, I’m a huge fan of your site, and I thought I’d make a contribution in the form of a concept game that’s still in its early stages.
Basically, you control the blue ball with the arrow keys and you have to knock the red ball off the edge. Clicking the mouse resets it.

and

I’m thinking of having several stages, including walls.
I want to have multiple AI balls, but I don’t know how.

So, if you have any clue about multiple AI balls, just give feedback.

The source code is well commented, for your understanding:

//functions
function findAnAngle(xthing, ything) {
	//very basic angle finder..returns value in degrees  
	term = Math.PI/180;
	//degree->radian
	if (xthing<0) {
		t = 180+Math.atan(ything/xthing)/term;
	} else if (xthing>0 && ything>=0) {
		t = Math.atan(ything/xthing)/term;
	} else if (xthing>0 && ything<0) {
		t = 360+Math.atan(ything/xthing)/term;
	} else if (xthing == 0 && ything == 0) {
		t = 0;
	} else if (xthing == 0 && ything>=0) {
		t = 90;
	} else {
		t = 270;
	}
	return t;
}
function doCollision(ball, ball2) {
	dx = ball._x-ball2._x;
	dy = ball._y-ball2._y;
	phi = Math.atan(dy/dx);
	//find the collision angle
	term = Math.PI/180;
	//degree->radian
	//find out the magnitude of the velocities of the two balls
	v1i = Math.sqrt(ball.vx*ball.vx+ball.vy*ball.vy);
	v2i = Math.sqrt(ball2.vx*ball2.vx+ball2.vy*ball2.vy);
	//find out the angle of the velocity vector
	ang1 = findAnAngle(ball.vx, ball.vy)*term;
	ang2 = findAnAngle(ball2.vx, ball2.vy)*term;
	//find the velocities in the new coordinate system
	v1xr = v1i*Math.cos(ang1-phi);
	v1yr = v1i*Math.sin(ang1-phi);
	v2xr = v2i*Math.cos(ang2-phi);
	v2yr = v2i*Math.sin(ang2-phi);
	//find the final velocities in the normal reference frame
	//the x velocities will obey the rules for a 1-D collision
	v1fxr = ((ball.mass-ball2.mass)*v1xr+(ball2.mass+ball2.mass)*v2xr)/(ball.mass+ball2.mass);
	v2fxr = ((ball.mass+ball.mass)*v1xr+(ball2.mass-ball.mass)*v2xr)/(ball.mass+ball2.mass);
	//the y velocities will not be changed
	v1fyr = v1yr;
	v2fyr = v2yr;
	//convert back to the standard x,y coordinates
	_root.ball.vx = Math.cos(phi)*v1fxr+Math.cos(phi+Math.PI/2)*v1fyr;
	_root.ball.vy = Math.sin(phi)*v1fxr+Math.sin(phi+Math.PI/2)*v1fyr;
	_root.ball2.vx = Math.cos(phi)*v2fxr+Math.cos(phi+Math.PI/2)*v2fyr;
	_root.ball2.vy = Math.sin(phi)*v2fxr+Math.sin(phi+Math.PI/2)*v2fyr;
}
//move ball
MovieClip.prototype.move = function() {
	this.onEnterFrame = function() {
		//check for collision with walls, done pretty quick
		//the balls might get stuck in the walls if they move too fast
		if ((ball._x<45) or (ball._x>579)) {
			bdead = true;
		}
		if ((ball._y<40) or (ball._y>424)) {
			bdead = true;
		}
		if ((ball2._x<45) or (ball2._x>579)) {
			b2dead = true;
		}
		if ((ball2._y<40) or (ball2._y>424)) {
			b2dead = true;
		}
		//move 
		this._x += this.vx;
		this._y += this.vy;
	};
};
_root.onLoad = function() {
	ball.mass = 20;
	ball.vx = 0;
	ball.vy = 0;
	ball.move();
	ball2.mass = 20;
	ball2.vx = 0;
	ball2.vy = 0;
	ball2.move();
	coll = 0;
	friction = 0.1;
	gws = 0;
	ba = 0.8;
	nmea = 0.8;
	radius = 15;
	bdead = false;
	b2dead = false;
};
_root.onEnterFrame = function() {
	//we check for collision using the distance between the two centre points of the balls
	//if that distance is smaller than two times the radius of the balls we have collision
	//the radius is hardcoded
	distance_x = Math.abs(ball._x-ball2._x);
	distance_y = Math.abs(ball._y-ball2._y);
	distance = Math.sqrt(distance_x*distance_x+distance_y*distance_y);
	//make sure the balls don't get stuck into eachother
	if (distance<=30 && coll == 0) {
		coll = 1;
		doCollision(ball, ball2);
	} else if (distance>30) {
		coll = 0;
	}
	//Player One controls 
	if (Key.isDown(Key.RIGHT) && ball.vx<10) {
		ball.vx += ba;
	}
	if (Key.isDown(Key.LEFT) && ball.vx>-10) {
		ball.vx -= ba;
	}
	if (Key.isDown(Key.UP) && ball.vy>-10) {
		ball.vy -= ba;
	}
	if (Key.isDown(Key.DOWN) && ball.vy<10) {
		ball.vy += ba;
	}
	//Friction 
	if (ball.vx>0) {
		ball.vx -= friction;
	} else if (ball.vx<0) {
		ball.vx += friction;
	}
	if (ball.vy>0) {
		ball.vy -= friction;
	} else if (ball.vy<0) {
		ball.vy += friction;
	}
	if (ball2.vx>0) {
		ball2.vx -= friction;
	} else if (ball2.vx<0) {
		ball2.vx += friction;
	}
	if (ball2.vy>0) {
		ball2.vy -= friction;
	} else if (ball2.vy<0) {
		ball2.vy += friction;
	}
	//Gravity well! 
	if (ball._x>320) {
		ball.vx -= gws;
	} else if (ball._x<320) {
		ball.vx += gws;
	}
	if (ball._y>240) {
		ball.vy -= gws;
	} else if (ball._y<240) {
		ball.vy += gws;
	}
	if (ball2._x>320) {
		ball2.vx -= gws;
	} else if (ball2._x<320) {
		ball2.vx += gws;
	}
	if (ball2._y>240) {
		ball2.vy -= gws;
	} else if (ball2._y<240) {
		ball2.vy += gws;
	}
	//Tracking AI 
	if ((ball2._xball._x) && ball2.vx>-10) {
		ball2.vx -= nmea;
	}
	if ((ball2._y>ball._y) && ball2.vy>-10) {
		ball2.vy -= nmea;
	}
	if ((ball2._y0) {
			ball._xscale -= 2;
			ball._yscale -= 2;
			ball._alpha -= 1;
		}
	}
	if (b2dead == true) {
		ball.vx = 0;
		ball.vy = 0;
		ball2.vx = ball2.vx/3;
		ball2.vy = ball2.vy/3;
		if (ball2._xscale>0) {
			ball2._xscale -= 2;
			ball2._yscale -= 2;
			ball2._alpha -= 1;
		}
	}
};
_root.onMouseDown = function() {
	ball.vx = 0;
	ball.vy = 0;
	ball2.vx = 0;
	ball2.vy = 0;
	ball._x = 245;
	ball._y = 170;
	ball2._x = 165;
	ball2._y = 222;
	bdead = false;
	b2dead = false;
	ball._xscale = 100;
	ball._yscale = 100;
	ball._alpha = 100;
	ball2._xscale = 100;
	ball2._yscale = 100;
	ball2._alpha = 100;
};

And this is the result:

Take a look, download the source code and let me know if you can turn this prototype into a blockbuster

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.