1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

(P3DC) Apply collisions for each instance of an object

Discussion in 'Programming' started by Rootmanko123, Aug 12, 2019.

  1. Rootmanko123

    Rootmanko123 Member

    Joined:
    Aug 1, 2019
    Posts:
    4
    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.
     
  2. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,434
    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
    
     
  3. Rootmanko123

    Rootmanko123 Member

    Joined:
    Aug 1, 2019
    Posts:
    4
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice