Starturbo
Member
Hi, I'm making a platform game in GameMaker: Studio, and I'm wondering if I should even bother trying to use collision_rectangle. Someone I talk to, who is more experienced than I am, recommended me to use it for collisions. I know some other people are also starting to use collision_rectangle, however, almost all tutorials still use place_meeting. I'm kind of wondering which one I should use for the main ground collisions. I used to use place_meeting, actually, and got collisions to work pretty well (not perfect though). With collision_rectangle, however, I'm running into a lot of issues. The ground collision works well, besides a rare glitch, but I can't figure out how to get slopes working. Here's some of the ground collision code I have so far:
And here's the code for slopes. Keep in mind, the following code is unfinished:
I'm using a finite state machine, by the way. Any help is appreciated!
Code:
//Sets the Nearest Blocks to the Ones the Player is Colliding With
left_block = collision_rectangle(bbox_left + hsp, bbox_top + 1, bbox_left + hsp, bbox_bottom - 1, obj_par_block, false, true);
right_block = collision_rectangle(bbox_right + hsp, bbox_top + 1, bbox_right + hsp, bbox_bottom - 1, obj_par_block, false, true);
top_block = collision_rectangle(bbox_left + 1, bbox_top + vsp, bbox_right - 1, bbox_top + vsp, obj_par_block, false, true);
bottom_block = collision_rectangle(bbox_left + 1, bbox_bottom + vsp, bbox_right - 1, bbox_bottom + vsp, obj_par_block, false, true);
//Horizontal Collision
if left_block && image_xscale = -1
{
hsp = 0;
scr_position(left_block.bbox_right + 9, 0, self);
}
if left_block && (image_xscale = 1 || key_right) && !(key_left && key_right)
{
hsp = 0;
scr_position(left_block.bbox_right + 11, 0, self);
}
if right_block && image_xscale = 1
{
hsp = 0;
scr_position(right_block.bbox_left - 8, 0, self);
}
if right_block && (image_xscale = -1 || key_left) && !(key_left && key_right)
{
hsp = 0;
scr_position(right_block.bbox_left - 10, 0, self);
}
x += hsp;
//Vertical Collision
if top_block && !left_block && !right_block && vsp < 0
{
vsp = 0;
scr_position(0, top_block.bbox_bottom + (sprite_height / 2), self);
}
if bottom_block && vsp > 0
{
vsp = 0;
scr_position(0, bottom_block.bbox_top - (sprite_height / 2), self);
}
y += vsp;
Code:
//Sets the Nearest Slopes to the Ones the Player is Colliding With
near_left_slope = collision_rectangle(bbox_left + hsp, bbox_top + 1, bbox_left + hsp, bbox_bottom - 1, obj_par_slope, true, true);
near_right_slope = collision_rectangle(bbox_right + hsp, bbox_top + 1, bbox_right + hsp, bbox_bottom - 1, obj_par_slope, true, true);
near_top_slope = collision_rectangle(bbox_left + 1, bbox_top + vsp, bbox_right - 1, bbox_top + vsp, obj_par_slope, true, true);
near_bottom_slope = collision_rectangle(bbox_left + 1, bbox_bottom + vsp, bbox_right - 1, bbox_bottom + vsp, obj_par_slope, true, true);
left_slope = collision_rectangle(bbox_left - 1, bbox_top + 1, bbox_left - 1, bbox_bottom - 1, obj_par_slope, true, true);
right_slope = collision_rectangle(bbox_right + 1, bbox_top + 1, bbox_right + 1, bbox_bottom - 1, obj_par_slope, true, true);
top_slope = collision_rectangle(bbox_left + 1, bbox_top - 1, bbox_right - 1, bbox_top - 1, obj_par_slope, true, true);
bottom_slope = collision_rectangle(bbox_left + 1, bbox_bottom + 1, bbox_right - 1, bbox_bottom + 1, obj_par_slope, true, true);
//Up Slopes
if near_bottom_slope && vsp > 0
{
y += vsp;
vsp = 0;
}
while collision_rectangle(bbox_left + 1, bbox_bottom + 1, bbox_right - 1, bbox_bottom + 1, obj_par_slope, true, true)
{
y -= 1;
vsp = 0;
}
x += hsp;
y += vsp;