# Collision Detection doesnt work because some things move more than 1px at a time

I'm writing a 2d scrolling game in javascript using canvas and since I'm limited to setInterval running at minimum once every ms, I need to have some objects moving at greater than 1 pixel a second. I have this for collision detection between objects:

// ** oversee collisions
collisionsInterval = setInterval(function() {

// outter object
for(i = 0; i < objectsToCollision.length; i++) {

// inner object
for(j = 0; j < objectsToCollision.length; j++) {

// dont compare an object to itself
if(i==j) continue;

// check if they touch on x axis
if(Math.abs(objectsToCollision[i].centerX - objectsToCollision[j].centerX) <= (objectsToCollision[i].halfWidth + objectsToCollision[j].halfWidth)) {

// check if they touch on y axis
if(Math.abs(objectsToCollision[i].centerY - objectsToCollision[j].centerY) <= (objectsToCollision[i].halfHeight + objectsToCollision[j].halfHeight)) {

// a collision was detected, do something useful

}

}

}

}
}, 1);


The problem is, since it wont alert() when two things are about to touch because my objects move at more than 1px at a time. The problem is, some objects might move 3px at a time, some might move at 8px at a time, and I also have gravity occurring so in the Y direction especially the velocity of my objects will really vary.

In my code above objectsToCollision is an array which contains all the objects I'm checking for collisions with. I've modelled what I've done so far after this https://gamedev.stackexchange.com/a/587 but it doesn't discuss how to solve the problem I'm experiencing!

• I'm going to venture a guess and say that your objects are really small compared to their relative velocities? If so, the issue is called "Tunneling". That might help you search for more relevant results. Here's one related link I've seen a few times: gamedev.net/topic/345705-predictive-collision-detection – John McDonald Mar 9 '12 at 20:56

This looks like you're comparing the objects at a single point - the center-point of the rectangle is not important - only the bounds:

object1 = ship
object2 = bullet

if (object2.y2 > object1.y1) AND (object2.y1 < object1.y2):
if(object2.x2 > object1.x1) AND (object2.x1 < object1.x2):
print ('a hit!')

• The method I'm using to calculating collisions already works when it comes to determining when a collision occurs. However, I do prefer your method of detecting when two objects overlap because it makes use of 2 y coordinates and 2 x coordinates to an object (or in other words, vertices), and it would be much more beneficial for me to track an objects' vertices instead of center because I'll need them for drawing the objects in webGL. It might also be faster than how I'm doing it because it doesn't use Math.abs. This answer didn't necessarily resolve my problem but it did help, thanks! – David Zorychta Mar 12 '12 at 16:42

This is by no means a complete answer, but instead of describing objects as points describe them as a line or set of lines, each with a start point and time and end point and time. Now you can easily calculate the position of something at any given nanosecond. This is great if you are shooting at them with a laser over short distances. Figuring a collision between two such objects is harder and would require either some fancy mathematics or else a series of successive approximations. If the approximations start to diverge, you missed. If they converge, you'll get to the collision point soon enough.

At any rate, you'll quickly get the objects into a position where you can do the hard work of checking for a collision, answering questions like "would object A pass through the hole object B or smack a projecting fin?" at time t + 0.4398224, despite the fact that they were miles apart at both times t and time t + 1.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8388315,"math_prob":0.784943,"size":1539,"snap":"2019-43-2019-47","text_gpt3_token_len":371,"char_repetition_ratio":0.18957655,"word_repetition_ratio":0.01746725,"special_character_ratio":0.24431449,"punctuation_ratio":0.12820514,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.957946,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-17T07:55:59Z\",\"WARC-Record-ID\":\"<urn:uuid:fe044893-c68f-4f70-9f06-bb4cdc6bb450>\",\"Content-Length\":\"146860\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7ae19dc2-6dc0-4288-b1cd-b77fa956ba80>\",\"WARC-Concurrent-To\":\"<urn:uuid:d147c6cf-b646-43e8-a062-b302c1adf6ab>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://gamedev.stackexchange.com/questions/25276/collision-detection-doesnt-work-because-some-things-move-more-than-1px-at-a-time/25409\",\"WARC-Payload-Digest\":\"sha1:L6NC3QVZT6RGTUFIUL4ODIMLIZ6CGGZQ\",\"WARC-Block-Digest\":\"sha1:JUX67F2DV452NZDBZQB3PEIO2FM5NIAA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496668896.47_warc_CC-MAIN-20191117064703-20191117092703-00080.warc.gz\"}"}