• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[SOLVED] Collisions...Is there a way to filter the objects returned by collision funtions?

So I'm revisiting my collision code, and I'm having a problem. Here's the basic code to grab the (solid) object I'm colliding with:



The problem I'm having is that I check to see whether there's a solid object to the right of the player using place_free(). If there is, I grab the instance id from that location with instance_place() and use it for my collision checks. The problem with that is that even though place_free() discriminates between solid and non-solid instances, instance_place() doesn't, so if I have a blade of grass next to the wall I'm colliding with, half the time GM will return the id of the grass blade instead of the wall, and the collision code fails sporadically.

One way I could get around this is by having every wall object share a common parent, like obj_Solid. I tried that, and the code works reliably, but it's a pain in the ass, because certain objects I want to be solid already have parents, and because I'd like to be able to toggle objects from solid to non-solid on the fly, for things like gates or flame walls or whatever.

Is there a way I can check out all of the instances I'm colliding with at a certain position, and only return them if they have something like "solidity = "full"" in them or something?

I appreciate any advice!
 
S

SSJCoder

Guest
a simple way that I can think of is to simply iterate through all the objects (obviously if you have a lot of objects there may be performance implications).

example:
Code:
if ( !place_free( x+1, y ) ) {
    collisionA = noone;
    with ( all ) {
        if ( place_meeting( x-1, y, other ) ) { // added '-1' since that seems to be what you're trying to achieve
            // check object
            if ( object_index == obj_Wall || object_index == <other-objects, etc..> ) {
                // found a wall/solid/whatever
                collisionA = id;
                break;
            }
        }
    }
    // now our item/object is in 'collisionA' variable, but if no objects
    // are in range then it will reference the 'noone' keyword
}
// note that I'm unsure of any incompatibilities with current GMS versions as I'm mostly used to GM8
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
Another way would be using the collision list functions (unsure if your situation requires the instance's collision mask to be used, but if it doesn't...) such as collision_rectangle_list. After obtaining the list, iterate over it until you find a solid instance. Not that I recommend using solidity in the first place, but if that's what the circumstances require...
 
Heck yeah, thanks guys.

@SSJCoder: That's basically what I thought about doing at first, but I was a bit worried about the performance, too. It's a good idea though, and I appreciate the input!
@TsukaYuriko: This is basically what I was looking for, thank you! I won't be using solid...I'll be checking some other variable I make in each instance, so I can do something a little more granular and not have to worry about what GM is doing behind the scenes as much. This will work perfectly for me, thanks!

If anyone has any other thoughts or comments, or some even easier/more robust method for this kind of thing, I'm all ears, but I'm going to mark this thread solved, since Tsuka's solution seems very workable to me.
 
S

SSJCoder

Guest
No problem, idk about today's GM/YYC compiler, but I know for sure performance is an issue if you put too much code like that in GM8 ^.^' (while having hundreds/thousands of objects)
 

Xer0botXer0

Senpai
I've honestly stopped caring about bogging down the performance, at the end of the day I had find myself trying to improve the performance of a game that's no where near finished.
My new approach is, if it's not an endless loop, leave it.
 
Top