Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

This is the code on Game Maker. Make it so that the player collides with obstacles and…

  1. User

    This is the code on Game Maker. Make it so that the player collides with obstacles and cannot pass through them.

    /// @description

    var _hsp = keyboard_check(vk_right) - keyboard_check(vk_left); var _vsp = keyboard_check(vk_down) - keyboard_check(vk_up);

    x += _hsp * spd * blood; y += _vsp * spd * blood;

    // Restrict movement during day if (global.is_day) { // Vampire can only be in caves (e.g., obj_cave area) if (place_meeting(x, y, obj_cave) == false) { // Vampire is exposed to sunlight hp = 0; // Vampire dies } }

  2. Assistant

    To ensure that the player cannot pass through obstacles in your GameMaker project, you'll need to implement collision detection and response. A common and effective method is to handle horizontal and vertical movements separately, checking for collisions after each movement. This approach allows the player to slide along obstacles naturally.

    Below is the modified code with collision handling against an obstacle object (obj_obstacle). Make sure to replace obj_obstacle with the actual name of your obstacle object if it's different.

    /// @description Player Movement with Collision Handling
    
    // Input Handling
    var _hsp = keyboard_check(vk_right) - keyboard_check(vk_left);
    var _vsp = keyboard_check(vk_down) - keyboard_check(vk_up);
    
    // Calculate movement increments
    var move_x = _hsp * spd * blood;
    var move_y = _vsp * spd * blood;
    
    // --- Horizontal Movement ---
    // Attempt to move horizontally
    x += move_x;
    
    // Check for collision with obstacles
    if (place_meeting(x, y, obj_obstacle)) {
        // Collision detected, revert horizontal movement
        x -= move_x;
        
        // Optionally, you can add sliding or other responses here
        // For example, stop horizontal movement if colliding
        // _hsp = 0;
    }
    
    // --- Vertical Movement ---
    if (_vsp != 0) { // Only attempt vertical movement if there's input
        y += move_y;
        
        // Check for collision with obstacles
        if (place_meeting(x, y, obj_obstacle)) {
            // Collision detected, revert vertical movement
            y -= move_y;
            
            // Optionally, you can add sliding or other responses here
            // For example, stop vertical movement if colliding
            // _vsp = 0;
        }
    }
    
    // --- Daytime Restrictions ---
    if (global.is_day) {
        // Check if the player is within a cave
        if (!place_meeting(x, y, obj_cave)) {
            // Player is exposed to sunlight and dies
            hp = 0;
        }
    }
    
    // --- Optional: Additional Collision Responses ---
    // You can add more complex collision responses here, such as sliding along walls,
    // triggering events when hitting certain objects, etc.
    

    Explanation of the Changes

    1. Separate Horizontal and Vertical Movements:

      • Horizontal (x) Movement:

        • Move: The player attempts to move horizontally by updating the x position.
        • Check Collision: After moving, place_meeting checks if the new position collides with obj_obstacle.
        • Revert if Colliding: If a collision is detected, the movement is undone by subtracting move_x from x.
      • Vertical (y) Movement:

        • Move: Similarly, the player attempts to move vertically by updating the y position.
        • Check Collision: After moving, place_meeting checks for collisions.
        • Revert if Colliding: If a collision is detected, the movement is undone by subtracting move_y from y.
    2. Optimizations and Enhancements:

      • Conditional Vertical Movement: The vertical movement is only attempted if there is vertical input (_vsp != 0), which can optimize performance slightly.
      • Optional Sliding: You can enhance the collision response to allow the player to slide along obstacles by not fully reverting the movement or by adjusting velocity variables accordingly.
      • Collision with Different Objects: If you have multiple types of obstacles, you can modify the place_meeting function to check against different objects or use layers.
    3. Daytime Restrictions:

      • The existing logic to handle daytime restrictions remains unchanged. It ensures that during the day, the player (e.g., a vampire) must be within a cave (obj_cave). If not, the player’s health (hp) is set to 0, resulting in death.

    Additional Tips

    • Collision Masks: Ensure that your obstacle objects (obj_obstacle) have proper collision masks set up. The collision mask determines how place_meeting detects collisions.

    • Tile-Based Games: If your game uses tile maps for obstacles, consider using tilemap functions like tilemap_get_at_pixel or collision_line for more efficient collision detection.

    • Physics Engine: For more advanced movement and collision handling, you might explore using GameMaker’s built-in physics engine, which can handle more complex interactions automatically.

    • Debugging Collisions: Use draw_rectangle or draw_sprite in a debugging step to visualize collision areas and ensure that collision detection works as expected.

    By implementing the above collision handling, your player should now interact properly with obstacles, preventing them from passing through walls or other solid objects in your game.