Friday, April 3, 2015

[Progress] Collision detection with boulder pushing - part 1

I finally got it to work! I needed to comment out a part of my obj_grid code that was deleting all controller_collision child instances (to save memory). I might just make all of the instances invisible for the time being.

After a lot of frustration and doing my to understand my mistakes, I realized that I was going about the checks incorrectly. Rather than creating a separate place_meeting() event for checking my collision object obj_wall_marker which inherits the properties from it's parent controller_collision to determine if the space it was pushing into was not occupied by a collision object. Instead, it should be performing that check simultaneously with respect to the position to where the player is. Another mistake I made was having x = 0; for the collision check, as it would set the x-position go 0 rather than "do nothing". And finally, it is important to remember that the image mask starts at (0,0) rather than the center, and these images are all 16x16 which means I was checking as far as an entire grid over, causing preemptive collision with the player. Anyways, here's the old code in it's messy non-functional glory.

if place_meeting(x-8,y,obj_player) && keyboard_check(ord('D')) {
    x += 10 ;
    }
if place_meeting(x+8,y,obj_player) && keyboard_check(ord('A')) {
    x -= 10 ;
    }
if place_meeting(x,y-8,obj_player) && keyboard_check(ord('S')) {
    y += 10 ;
    }
if place_meeting(x,y+8,obj_player) && keyboard_check(ord('W')) {
    y -= 10;
    }


if place_meeting(x-16,y,obj_wall_marker) {
    x = 0 ;
    }
if place_meeting(x+16,y,obj_wall_marker) {
    x = 0 ;
    }
if place_meeting(x,y-16,obj_wall_marker) {
    y = 0 ;
    }
if place_meeting(x,y+16,obj_wall_marker) {
    y = 0 ;
    }

move_snap(16,16);

The best part is the new collision code takes up even less space than this

if place_meeting(x,y+1,obj_player) && !place_meeting(x,y-1,controller_collision) && keyboard_check(ord('W')) {
    y -= 10;
    // Check South, push North
    }
if place_meeting(x+17,y,obj_player) && !place_meeting(x-1,y,controller_collision) && keyboard_check(ord('A')) {
    x -= 10 ;
    // Check East, push West
    }
if place_meeting(x,y-1,obj_player) && !place_meeting(x,y+1,controller_collision) && keyboard_check(ord('S')) {
    y += 10 ;
    // Check North, push South
    }
if place_meeting(x-2,y,obj_player) && !place_meeting(x+1,y,controller_collision) && keyboard_check(ord('D')) {
    x += 10 ;
    // Check West, push East
    }
//show_debug_message("PUSHED")
move_snap(16,16);

It's still cluttered, but it'll suffice until I start working on a Finite State Machine.

No comments:

Post a Comment