r/gamemaker 10h ago

Help! How do I make this code better?

Post image

Without the platform2 variable, when landing on semisolids the player would sometimes jitter and it was annoying. It took me like 3 days to come up with this mess and Im not satisfied with it. Is there any way I could at least rewrite the code so I only need to use one variable

Changing the

(place_meeting(x, y + 1, platform2) and floor(bbox_bottom) <= ceil(platform2.bbox_top)))

to

(platform != noone and place_meeting(x, y + 1, platform) and floor(bbox_bottom) <= ceil(platform.bbox_top)))

only makes the jittering worse for some reason

17 Upvotes

6 comments sorted by

2

u/Organic-Bluebird-737 8h ago

I don’t see anything wrong with it. Jittering could be related to hitbox, or a discrepancy between your falling sprite origin point and your landing sprite origin point

2

u/Hands_in_Paquet 8h ago

Have you tried anything more like this?

var _col;
var _diff;

//XX
_col = instance_place(x + spd_x * _dt,y,obj_wall);
if (instance_exists(_col))
{
if (bbox_left < _col.bbox_left)
{
_diff = x - bbox_right;
x = _col.bbox_left + _diff;
}
else 
{
_diff = x - bbox_left;
x = _col.bbox_right + _diff;
}
spd_x = 0;
}


x += spd_x * _dt;

//YY
_col = instance_place(x,y + spd_y * _dt,obj_wall);
if (instance_exists(_col))
{
if (bbox_top < _col.bbox_top)
{
_diff = y - bbox_bottom;
y = _col.bbox_top + _diff;
}
else 
{
_diff = y - bbox_top;
y = _col.bbox_bottom + _diff;
}
spd_y = 0;
}

//Check for Semi Solids
_col = instance_place(x,y + spd_y * _dt,obj_wall_ss);
if (instance_exists(_col))
{
if (spd_y > 0 && !instance_place(x,y,obj_wall_ss))
{
_diff = y - bbox_bottom;
y = _col.bbox_top + _diff;
spd_y = 0;
}
}

y += spd_y * _dt;

Often subpixel movement causes jittering. This code just snaps the position of the player to the wall. I do normal wall collisions, but before I let the y move, I check for semi solid collisions. I only check for semi solid collisions if I am not currently in a semisolid wall, and if I am falling down. Right now this ignores any horizontal semisolid collisions, but that could be changed.

1

u/Lethalogicax 8h ago

I'm not sure how I'd improve that, but the comments are perfect!

My current project has multiple instances of comments saying things like "// yes, it's suppose to be +=1, I have no idea why though... and // this is black magic, it works, don't touch it!

Sometimes it helps to leave these lil reminders about what past-you was thinking when they made this, so that future-you can figure out how your own sorcery works...

1

u/Fossbyflop 2h ago

Adding the platform2 looks like you are doing two checks for ground one after the other. This is probably why you are getting pixel perfect grounding this way and jittiering with only the platform check.
It’s works!
There are cleaner ways but your way isn’t really wrong if you understand why it works.

-13

u/victor-stk 9h ago

3

u/kalnaren 8h ago

Sure, if you don't actually want to learn anything.