Remote collision detection (for games)

Hello all, so I have been pretty active on the “create games” front but not so much for blogging, sorry about that… So I come to you now with a Pen that razed over 1500+ views on codepen and 14 hearts so far…

It’s my implementation of a remote collision detection system that uses degrees instead of bounding boxes. This works well for games where you want to calculate the position of the shoot but the “cannon/lazer/fazer/tower” is not near the target, so instead of creating long bounding-boxes. We just calculate the degrees between the source (cannon) and the target(enemy). And when the cannon is rotated on those degrees it can fire.

Here I will show how a tank can “detect” the enemy. Here’s a quick example and the code will follow:

See the Pen Remote collision detection by Michael Dobekidis (@netgfx) on CodePen.

First of all let us create our HTML

<div id="tank">
  <div id="cannon"></div>

quite minimal right?

Now onwards for some css




This creates the tank object and the background. Notice that cannon and tank are two separate objects.

Now the javascript part

First we declare some images as variables for ease of use:

var tankImg = "";

var cannonImg = "";

var targetImg = "";

var bulletImg = "";

You can find these nice textures and tank images here: Textures pack

Now we create all of our entities as Images

  var tank = new Image(); = "0px";  = "0px";
  tank.onload = function(){
  tank.src = tankImg;
  var cannon = new Image(); = "10px";  = "13px"; = "absolute"; = "100%";    = "visible";
  cannon.onload = function(){
  cannon.src = cannonImg;
  var target = new Image(); = getRandom($(document).width()-50,50)+"px";  = getRandom(300,50)+"px"; = "absolute"; = "target";
  target.onload = function(){
  target.src = targetImg;

* getRandom is a typical random function

function getRandom(max, min) {
    return Math.floor(Math.random() * (1 + max - min) + min);

Here we loaded all our entities and appended them to DOM, as you see when the target(enemy) is created we call a function scan()
Let’s see that and what it does:

function scan() {
var angle =  checkAngle();"#cannon>img",5,{rotation:360,transformOrigin:9+"px "+7+'px',onUpdate:function(){
      var element =;
      var _angle = Math.round(element[0]._gsTransform.rotation);
      if(_angle === angle || (angle<=_angle+5 && angle>=_angle-5) ){

What we do here is to rotate the cannon and on every frame we check the angle of the cannon upon the angle of the target, and if the angles match (+-5pixels) then we pause the rotation and the target has been detected.
The last part is the checkAngle() function which looks like this:

function checkAngle(){

var targetX = $("#target").offset().left+25;
var targetY = $("#target").offset().top+25;
var targetW = $("#target").width();
var targetH = $("#target").height();
  var angleY = (targetY-(targetH/2)) - $("#tank").offset().top;
  var angleX = (targetX-(targetW/2)) - $("#tank").offset().left;
  var angle = Math.round(Math.atan2(angleY, angleX) * 180 / Math.PI) + 90;// in degrees
  angle = angle - 90; // minus the tank angle, otherwise the cannon aligns to the front
  if(angle < 0){
    angle = 360 - Math.abs(angle); // converting negative angle to its counterpart
  return angle;

Here we use simple math to calculate the angle between the target(enemy) and the source(tank)

I hope you enjoyed this little tutorial. For any questions or remarks please leave me a comment.


Tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *