CollideTypesInto creating infinite loop


(Skelyton) #1

When the jet collides with the bird, it should move down its own height.

Here is the broken code

Link to the game

But, I don’t think its populating the collideArray with the new collisions on the second iteration of the loop. It should be populating the collideArray with the second bird entity which I’ve assigned the type “doge” to. However my trace code always returns “bird”, meaning its getting stuck on the first collision.


(JP Mortiboys) #2

The problem with your code, as-is, is that you’re not clearing the array before you call collideTypesInto, so it still has the old entries in there as well - you need to do:

y = minHeight + height;
collideArray.length = 0;
collideTypesInto(["bird", "doge"], x, y, collideArray);

Also, this seems quite complicated - is there any reason this wouldn’t work?

var e:Entity;
while (e = collideTypes(["bird", "doge"], x, y)) {
  y += height;
}

(This is literally “moving down by its own height” whenever it hits something)


(Glamador) #3

I helped to write this code.

You say “has the old entries in there as well”, but the trace code only returns the same 3 values.

1 Bird 154

Over and over again. That means that it isn’t adding to the array nor is it replacing it. It’s just reusing the same data over and over. The CollideTypesInto function only works on empty arrays, does it? That’s an annoying way of going about it, but I suppose it would take the same 1 line of code to clear the array whether it’s done by the engine or the user, so meh.

As to your solution, that seems like it would work well enough. But the idea was to only move it just enough to clear the impeding object, and it grabs as many objects as there are colliding to try and find the lowest (or, eventually, highest) y-position and move it slightly above or below that.

I’m not sure why I wanted it to work like that, since it it seems a lot of headache for a minor quirk of spawn placement, but I get tunnel-visioned sometimes. Really, I just wanted to know why this code wasn’t working before scrapping it for being stupid.

The final goal is just to avoid two objects spawning already colliding. But on a second thought, this code doesn’t even work for that because if it only checks on spawn it wouldn’t catch when a jet (which moves faster than a bird) spawns just after a bird and catches up to it before the player can react…it’s a pickle.


(Zachary Lewis) #4

Try collide doge? So scare. Why this happen?

Concern.


(JP Mortiboys) #5

If it’s returning the same objects over and over again, my initial guess would be that you’re not moving it enough out of the way, so it’s still colliding with the same object.

With regards to your jet problem, just determine a reasonable reaction time (1 second?), multiply that by the jet’s speed (in pixels/second), and extend its hitbox rightwards by that many pixels for the placement loop only.

There are several ways of planning object placement that would make this easier - for example you could divide the screen (or spawn area) into equal-sized boxes at least large enough to fit an object in (ideally larger), then for each object you want to pick, select a random unused box, and then place it at a random position inside that box. For many objects this would make quite a difference in speed as it’s O(n) instead of O(n2).


(Skelyton) #6

This is a great idea. Thanks so much. I’ll try this out.