GameMaker How would Game Maker handle 2D instance collisions like Minecraft?

  • Thread starter Heavenisreal777
  • Start date
H

Heavenisreal777

Guest
Hello Game Makers!
I'd like to learn how to handle a top down game's collisions like minecraft does - "How to push instances of objects away from each other"

I've tried for several times to get it somewhat working, but to no avail..

If you could please some light about game maker collsion pushing, it would really help a lot. Thanks!
 

rytan451

Member
If the instances are overlapping, identify what direction the overlap is in. Then apply a force along that direction to push the instances apart. This latter part requires instances to have a velocity variable (or variables).
 
H

Heavenisreal777

Guest
If the instances are overlapping, identify what direction the overlap is in. Then apply a force along that direction to push the instances apart. This latter part requires instances to have a velocity variable (or variables).
Thanks for replying!
I've already basically done that, but the only question is how would I be able to make the pushing more smoother because MC's Collsions are smooth right?
I'm using GML's built-in pathfinding for all Enemy objects. For that reason, the pathfinding may or may not interfere in the collisions (but it shouldn't make any significant difference imo) - I mentioned this just in case.
Currently, what I've done is the following:

Enemy object/instance Create Event
Code:
/// Create paths
path = path_add();

/// Setup player as target
target_x = obj_player.x;
target_y = obj_player.y;

/// Setup speeds
spd = 18;
default_bounce_speed = 4;
bounce_speed = 0;
Enemy object/instance Step Event
Code:
/// Create grid (for pathfinding to work)
grid = global.grid;

/// Update target
target_x = obj_player.x;
target_y = obj_player.y;

/// Temporary variable for holding the obstacle objects
var obstacle = obj_all_obstacles;

/// How far will objects have to go inside to push?
r = 6;

/// Find out if is near an obstacle
near_obstacle = place_meeting(x,y,obstacle);

/// Push self away from others
if (collision_circle(x,y,r,obj_scp_049,1,1) and distance_true)
{
    path_end();
    if (bounce_speed == 0)
    {
        direction2 = irandom(360);
        x -= lengthdir_x(5, direction2);
        y -= lengthdir_y(5, direction2);
    }
    bounce_speed = default_bounce_speed;
}

/// Push self away from player
if (collision_circle(x,y,r,obj_player,1,0) and distance_true)
{
    path_end();
    bounce_speed = default_bounce_speed;
    direction += irandom_range(-5,5);
}

/// When touches any obstacle - reset bounce_speed
if (place_meeting(x,y,obstacle))
{
    bounce_speed = 0;
}

/// When bounce speed is different from 0, slowly decrease/increase it to 0
if (bounce_speed > 0) bounce_speed -= 0.2;
if (bounce_speed < 0) bounce_speed += 0.2;
/// Push initialise
x -= lengthdir_x(bounce_speed, direction);
y -= lengthdir_y(bounce_speed, direction);

/// Move if is not being pushed
if (bounce_speed == 0)
{
    if (mp_grid_path(grid, path, x, y, target_x, target_y, 1))
    {
        path_start(path, spd, path_action_stop, 0);
    }
    else
    {
        path_end();
    }
}
 
Last edited by a moderator:
H

Heavenisreal777

Guest
Right now they are just bouncing around each other when the hit other enemies.. :|
The problem is that when the bounce_speed is too low, then the enemies get stuck inside each other
How could I possibly fix this?
 
Last edited by a moderator:
Top