Legacy GM [Solved] Clamp() Alternatives?

B

BubbleMage123

Guest
Hello! I've been working with a glitch in my game where the characters get stuck in each other, which looks like this:



After some testing, I found that it related to the clamp code I have:
Code:
x=clamp(x, 0, room_width);
y=clamp(y, 0, room_height);
So my question is: Is there an alternative to clamping the room or manually outlining the room in solid blocks?
I looked online and didn't find much help :( Any help is appreciated, even if there's a reference to somewhere else. Thanks!
 
Last edited by a moderator:

TheouAegis

Member
Should prob be clamp(x, sprite_xoffset, room_width-sprite_width+sprite_xoffset).

Anyway, you could also use median(), same as clamp().

Or use a set of if/else checks that do the same thing.
 
B

BubbleMage123

Guest
@flyingsaucerinvasion I'm sorry, I was talking about how the characters get stuck in the wall. The "screen shake" is what causes the jitteriness and the lines that appear in the tiles come from the screen shake, which I'm going to try to fix later.
@TheouAegis The median code did do the same thing as clamp, but unfortunately it caused the same glitch. The clamp(x, sprite_xoffset, room_width-sprite_width+sprite_xoffset) caused my character to glitch under the screen in a really weird way xD
 
Code:
var dx=clamp(x, 0, room_width)-x;
var dy=clamp(y, 0, room_height)-y;
with(obj_player){ //both player instances
   x+=dx;
   y+=dy;
}
You simply need to move both instances by the clamp amount so they don't move into each other. Probably better, to not run into bugs, you might want to have invisible blocks around the edges so its taken care of in the movement code since it looks like your blocks work right.
Code:
var w = 32; //block width (change value of needed)
var ins = instance_create(0,-w,obj_block);
ins.image_xscale = room_width/w;
ins = instance_create(0,room_height,obj_block);
ins.image_xscale = room_width/w;
ins = instance_create(-w,-w,obj_block);
ins.image_yscale = (room_height+2*w)/w;
ins = instance_create(w+room_width,-w,obj_block);
ins.image_yscale = (room_height+2*w)/w;
 
B

BubbleMage123

Guest
@Strawbry_jam Thank you! I liked the second patch of code. Works perfectly ^_^ seems like it creates instances of the solid to outline the room, so I moved it out of the step event.
 

Joe Ellis

Member
x=clamp(x, 0, room_width);
y=clamp(y, 0, room_height);
This code is pretty pointless cus the majority of the time they will never be outside the room, it'd be better to not have this code and just have blocks blocking the way to outside the level, which strawberry jam said lol, but im just pointing out this code is completely pointless if theyre in the room, which 99.9% of the time they will be, its just cluttering up your code, you could simply delete this and it wouldnt make any difference cus the blocks sort it out
 
Top