Sprite gets stuck when changing collision mask

D

Drago Supremo

Guest
Hi,
i'm working on a simple platform gaming but i'm having some problems handling collisions when i need to change collision masks.
Actually the code to handle collisions looks like this:
-----------------------------------------------------------------------------
//Horizontal collisions
i = 1;
while !(place_meeting(x + i * sign(hsp), y, obj_all_solids)) and (i < hsp * sign(hsp)) i += 1;
i = i * sign(hsp);

if (place_meeting(x + i, y, obj_all_solids)) //Checks for in-range collisions
{
while !(place_meeting(x + sign(hsp), y, obj_all_solids)) x += sign(hsp)
hsp = 0;
}

x += hsp;

//Vertical collisions
i = 1;
while !(place_meeting(x, y + i * sign(vsp), obj_all_solids)) and (i < vsp * sign(vsp)) i += 1;
i = i * sign(vsp);


if (place_meeting(x, y + i, obj_all_solids)) //Checks for in-range collisions
{
while !(place_meeting(x, y + sign(vsp), obj_all_solids)) y += sign(vsp)
vsp = 0;
}
y += vsp;
-------------------------------------------------------------------------
where:
vsp = vertical speed
hsp = horizontal speed

It works fine, but what if i change the collision mask for a smaller one, then i move it without colliding near another object and then in the next frame before the collision checking i change the collision mask to a bigger one for a bigger sprite making it colliding with the object, but not moving? The collision check won't work because at the moment of the check the sprite is already getting stuck into the object.

This is the problem: is there a way to fix it, maybe even completely modifying my collision-checking code?

I can't use the same collision mask for all sprites: they are too different.

I already thought about checking if the sprite is colliding in its position (x, y) and then moving it back until it doesn't collide, but by this way i'm not sure how to make it correctly.

I really hope i explained it sufficiently well and thank you in advance for your help.
 
Last edited by a moderator:

TheouAegis

Member
Don't let the sprite/mask change if it will result in a collision? Or move the instance out of the collision if changing masks puts it in one?

What are you doing that would require a mask with one width and another mask with a smaller width? Typically you'd make sure your masks don't have that issue.
 
D

Drago Supremo

Guest
Don't let the sprite/mask change if it will result in a collision? Or move the instance out of the collision if changing masks puts it in one?

What are you doing that would require a mask with one width and another mask with a smaller width? Typically you'd make sure your masks don't have that issue.
I'd like to create a roll-jump animation, but it's smaller than the idle animation that i use when the roll-jump animation hits the ground.
I'll try with your second tip, moving it back directly during sprite change.
 

TheouAegis

Member
The rolling sprite should be the same width as the jumping sprite. I mean, when someone does a somersault, they don't really get that much smaller. They become more square-like. Really, a person should have a wider bounding box when rolling, not narrower.
 
D

Drago Supremo

Guest
The rolling sprite should be the same width as the jumping sprite. I mean, when someone does a somersault, they don't really get that much smaller. They become more square-like. Really, a person should have a wider bounding box when rolling, not narrower.
Ok, but then what if i roll near a wall? Doesn't the roll-jump animation get stuck because it's wider than the jump animation?
Anyway the biggest problem is for the height, because the sprite has a much smaller height than the idle animation.
 
Last edited by a moderator:
T

TDSrock

Guest
Why are you altering the collision mask all the time.

I would simply just have maybe like 4 collision masks for a player unit and use that for collision. Then just draw a different sprite.
Code:
Sprite != collision mask
Also issue when changning the sprite can be checked.

Code:
if(crouching){
   if(place_meeting(x,y-20,obj_wall){
      lockCrouch = true;
   }
}
As an example(ofcourse you'll have to properly lock the crouching then too.)
 
D

Drago Supremo

Guest
Why are you altering the collision mask all the time.

I would simply just have maybe like 4 collision masks for a player unit and use that for collision. Then just draw a different sprite.
Code:
Sprite != collision mask
Also issue when changning the sprite can be checked.

Code:
if(crouching){
   if(place_meeting(x,y-20,obj_wall){
      lockCrouch = true;
   }
}
As an example(ofcourse you'll have to properly lock the crouching then too.)
Yes i'm actually using 2 collisione masks, 1 for the idle animation and another for roll-jump animation. But the problem is that the First one is taller than the second one and by this way when the roll-jump hits the ground it changes to the idle animation which is taller and by this way it gets stuck with the ground, and i don't understand how to fix this. I thought about a collision system that when an object is stucked with another it brings the object back until it's no more stucked, but i don't know how to make it properly.
 
Last edited by a moderator:
D

Drago Supremo

Guest
BTW, the word you want is 'stuck'.

A pile of something (or to pile something up neatly) is a 'stack'.
Ok i'll use these drawnings to explain myself because it's not really easy.

So i've an object "player" which is somersaulting and it's about to collide with the ground, where the black square is the collision mask being used (situation 1) .

The collision system detects that the "player" is about to collide and moves it on the ground, as it's supposed to do (Situation 2).

Now there come the problems: in fact in the next frame (since the player is on the ground and is no more moving) the collision mask will change to the one that is used for the idle-animation, as it's supposed to do, but since the new collision mask it's taller, it will get stuck in the ground (situation 3).

The last situation is the one that i don't know how to fix.

I actually use only two collision masks. I know that one for every situation would be better, but some sprites are so different that i need at least two masks.
If you need it i'll give you all the code used for my "player" object. I can't upload any sprite image because they are copyrighted, but they are from Samus Aran in Metroid Zero Mission.
 

Attachments

TheouAegis

Member
Make sure the sprite origins of the player and enemies and anything else that moves around like that has the origin of the sprite at the very bottom of the sprite.
 
D

Drago Supremo

Guest
Make sure the sprite origins of the player and enemies and anything else that moves around like that has the origin of the sprite at the very bottom of the sprite.
Thank you very much it works fine. I didn't think it was so easy to fix :)
 
Top