• 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!

(P3DC) Apply collisions for each instance of an object

R

Rootmanko123

Guest
Hey there,

I managed to set up a 3D collision system using P3DC library, where I have one player and multiple table model objects (.gmmod) which are placed in the map as obj_table objects.

Now, the player object's collisions are set up simply like this:

collision_player = p3dc_begin_model();
p3dc_add_block(-8, -8, 0, 8, 8, 32);
p3dc_end_model();

Also, the table collisions are set up similarly:
collision_table = p3dc_begin_model();
p3dc_add_model("tablemodel.gmmod", x, y, 0);
p3dc_end_model();

Collision code is set up in the step event of the player like this:
if (p3dc_check(collision_player, x + hsp, y, 0, obj_table.collision_table, 0, 0, 0) == false) {
x += hsp;
}
if (p3dc_check(collision_player, x, y + vsp, 0, obj_table.collision_table, 0, 0, 0) == false) {
y += vsp;
}

Now, the player - table collisions work fine, however just with the first table instance that was placed in the room. When I place multiple instances of the table in the room, collisions work with only the first one that was placed, as I mentioned before.
I know there is a workaround, and that is to have a "map" object that loads all the model objects into one game maker object and then the player collides with the whole "map" object. That works fine, however, I want to be able to use the room editor to create my map and place different 3D model objects inside the editor. This way, I would have to model the whole map in a 3D model editor, which is not what I want.

Is there any way that I could make this collision code apply for every instance of the table object?

Any tips are appreciated, thank you for your time guys ;)
Have a nice day.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
You'd want to use a with-loop
Code:
var blocked = false;
with (obj_table) {
    if (p3dc_check(other.collision_player, other.x + other.hsp, other.y, 0, collision_table, 0, 0, 0)) {
        blocked = true;
        break;
    }
}
if (!blocked) x += hsp;
// ...same for Y
cleaner but requires further understanding of how with works::
Code:
var blocked = false;
with (obj_table) with (other) {
    if (p3dc_check(collision_player, x + hsp, y, 0, other.collision_table, 0, 0, 0)) {
        blocked = true;
        break;
    }
}
if (!blocked) x += hsp;
// ...same for Y
 
R

Rootmanko123

Guest
Thank you so much!

This works and helped me a lot to understand the mechanics. Before knowing the solution, I tried to implement a similar code using with into the step event of the table object, but instead of using with(table) I used with(player) and I noticed every time I collided with the table object, something happened, which was that the player slowed down a bit. Also the speed of the player was faster than before. I realized that since I've got multiple table objects in the room, running their own step events containing the with(player) code, where I used other.x += other.hsp and so on, the code for applying movement was running as many times as the number of tables in the room, thus speeding up the player (3 tables = step event run 3 times = 3 times the original player speed).
This however works like magic ;)

Thanks a lot, it's been my pleasure. Have a nice rest of the day.
 
Top