GM:S 1.4 Corner Collision Causing Incorrect X and Y Values?

Discussion in 'Programming' started by Storm1208, Jun 14, 2019 at 7:59 PM.

  1. Storm1208

    Storm1208 Member

    Joined:
    Dec 12, 2017
    Posts:
    9
    Hey All! I'm currently working on a recreation of the popular platformer "VVVVVV" and started using the view system in GameMaker to make small, 640x480 levels in my project. These levels are all contained within one room, and when the player walks far enough out of one view it switches to another to go to the next "level".

    Here's the code I typed up to test out this system:
    Code:
    if((obj_player.x = room_width/2))/*one level is 640x480 and room is 1280x960,
    so the player being at half the width of the room means it's time to "switch levels"*/
    {
           if(view_visible[0])
           {
                view_visible[0] = false;//turning off camera for first level
                view_visible[1] = true;//turning on camera for second level
                obj_titlecards.image_index = 1;//showing level name
           }
           else if(view_visible[1])//vice versa if going from level 1 to 0
           {
                view_visible[1] = false;
                view_visible[0] = true;
                obj_titlecards.image_index = 0;
           }
    }
    
    The system was working perfectly until I noticed a small bug. For some reason, if my player object encounters a corner (i.e. they are standing on the ground and also colliding with a "wall" in front of them) the view switching no longer works. I've been able to recreate the bug consistently and here's some pictures of my player encountering corners for reference:
    corner error.PNG corner error 2.PNG
    After the player collides with the floor and wall in a way like shown in the images, the views will no longer switch to show you which levels you're in. I concluded that this must have had something to do with how my collision code was affecting my player's x or y values but I can't seem to spot anything wrong with them.

    Horizontal Collision:
    Code:
    //Horizontal Collision
    if(place_meeting(x+hsp,y,obj_ground)) //if we're about to collide with a ground tile
    {
        while(!place_meeting(x+sign(hsp),y,obj_ground))//keep moving until...
        {
            x += sign(hsp);
        }
        hsp = 0; //BAM - stop moving
    }
    x += hsp;
    
    Vertical Collision:
    Code:
    //Vertical Collision
    if(place_meeting(x,y+vsp,obj_ground)) //if we're about to collide with a ground tile
    {
        while(!place_meeting(x,y+sign(vsp),obj_ground))//keep moving until...
        {
            y += sign(vsp);
        }
        vsp = 0; //BAM - stop moving
        flip = 1;
    }
    y += vsp;
    if(vsp != 0)
    {
        flip = 0;
    }
    
    I used the "show_debug_message()" code to track my player's x value and everything seems normal even after contacting the corner. I'm not entirely sure what's causing the bug but I appreciate any and all help anyone can give me!
     
  2. Bayesian

    Bayesian Member

    Joined:
    Sep 13, 2016
    Posts:
    402
    The issue is not in the code you posted. I must be in your view or camera settings. You should be using mod to trigger the camera stages as that will give you room and resolution independence although that specify doesn't matter for a re-make it would still be the best system
     
  3. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,351
    I think it actually is in the code that was posted.

    You are checking whether the player is exactly at a specific coordinate. If your player is even a fraction of a pixel away from that, this code is not going to run. I second restructuring your code to determine the room the player is in by using the mod operator, as this will not be bound to the player being at an exact coordinate at all. To check whether the player has entered a new room, keep a copy of the player's room coordinates at all time - if the current coordinates have different values than they did in the last step, you've just changed rooms.


    The original issue aside, I can see a second, not yet fully apparent issue unfolding soon. You're currently using the view system in a way where every room has its own view assigned to it. However, you are limited to having a maximum of 8 views at any given time, whether visible or not, so your current approach will work until you have exactly 8 rooms. You won't be able to add more than 8 rooms like this.

    I suggest to instead have just one view and change its coordinates accordingly, by manipulating the view_xview and view_yview variables to be in the top-left corner of the room the player is currently in. Since all rooms are the same size, the rest will take care of itself.
     

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