Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / C#

The SpaceShoot server

Rate me:
Please Sign up or sign in to vote.
4.97/5 (32 votes)
27 Mar 2012CPOL28 min read 52.1K   1.9K   56  
Providing a stable and powerful server for the JavaScript / HTML5 browser game SpaceShoot with some gimmicks.
var clamp = function(value, min, max) {
	if(value > max)
		return max;
	else if(value < min)
		return min;
	return value;
};

var collisionQ2Q = function(sx, sy, sw, sh, dx, dy, dw, dh) {//Method to detect collisions for rectangle with rectangle
	var sleft = sx - sw / 2,
		dright = dx + dw / 2;
	if(sleft > dright)
		return false;
	var sright = sx + sw / 2,
		dleft = dx - dw / 2;
	if(sright < dleft)
		return false;
	var sup = sy - sh / 2,
		dbottom = dy + dh / 2;
	if(sup > dbottom)
		return false;
	var sbottom = sy + sh / 2,
		dup = dy - dh / 2;
	if(sbottom < dup)
		return false;
	return true;
};

var collisionC2Q = function(sx, sy, sd, dx, dy, dw, dh) {//Method to detect collisions for circle with rectangle
	var sradius = sd / 2;
	var closestX = clamp(sx, dx - dw / 2, dx + dw / 2),
		closestY = clamp(sy, dy - dh / 2, dy + dh / 2);
	var distanceX = sx - closestX,
		distanceY = sy - closestY;
	var distanceSquared = (distanceX * distanceX) + (distanceY * distanceY);
	return distanceSquared < (sradius * sradius);
};

var collisionC2C = function(sx, sy, sd, dx, dy, dd) {//Method to detect collisions for circle with circle
	var sradius = sd / 2,
		dradius = dd / 2;
	var length = sradius + dradius,
		distanceX = sx - dx,
		distanceY = sy - dy;
	var distanceSquared = (distanceX * distanceX) + (distanceY * distanceY);
	return distanceSquared < (length * length);
};

var pbc = function(obj) {
	var s = obj.size / 2;
	if(obj.y + s < 0)
		obj.y = c.canvas.height + s;
	else if(obj.y - s > c.canvas.height)
		obj.y = -s;
	
	if(obj.x + s < 0)
		obj.x = c.canvas.width + s;
	else if(obj.x - s > c.canvas.width)
		obj.x = -s;
};

var d2g = function(angle) {		//Helper to calculate degree -> rad
	return Math.PI / 180 * angle;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Germany Germany
Florian lives in Munich, Germany. He started his programming career with Perl. After programming C/C++ for some years he discovered his favorite programming language C#. He did work at Siemens as a programmer until he decided to study Physics.

During his studies he worked as an IT consultant for various companies. After graduating with a PhD in theoretical particle Physics he is working as a senior technical consultant in the field of home automation and IoT.

Florian has been giving lectures in C#, HTML5 with CSS3 and JavaScript, software design, and other topics. He is regularly giving talks at user groups, conferences, and companies. He is actively contributing to open-source projects. Florian is the maintainer of AngleSharp, a completely managed browser engine.

Comments and Discussions