# Character Gets Stuck in Ground Occassionally

4 replies to this topic

### #1 hydroxy

hydroxy

GMC Member

• GMC Member
• 280 posts
• Version:GM:Studio

Posted 21 September 2010 - 07:33 PM

Upon a high drop my character gets occassionally stuck in the ground by about 1 or 2 pixels, unable to move as he is colliding with solid objects in the floor. I was hoping someone could help me solve this problem so he doesn't get stuck anymore. Here is the basic physics code from my game. I'm pretty sure this happens because the vspeed is too high on the approach to the ground so the character is positioned inside the ground on the final step. I need a way to prevent this.

```//basic physics

if place_free(x,y+1) and place_free(x,y+vspeed) {gravity = 0.60 gravity_direction = 270}

if (hspeed >= 5) then hspeed = 5

if (hspeed <= -5) then hspeed = -5

if (vspeed >= 10) then vspeed = 10

if (vspeed <= -10) then vspeed = -10

if hspeed!=0 and (!place_free(x,y+1)) then {friction=0.4}

if vspeed > 0 and (collision_rectangle(bbox_left,bbox_bottom,bbox_right,bbox_bottom+1,obj_solid,1,1)) {move_contact_solid(270, 16) move_outside_solid(90,16) gravity = 0 vspeed = 0}

if vspeed < 0 and (collision_rectangle(bbox_left,bbox_top-1,bbox_right,bbox_top,obj_solid,1,1)) {move_contact_solid(90, 8) move_outside_solid(270,8) vspeed = 0}

if hspeed > 0 and (collision_rectangle(bbox_right,bbox_top,bbox_right+1+hspeed,bbox_bottom,obj_solid,1,1)) {move_contact_solid(0, 8) move_outside_solid(180,8) hspeed = 0}

if hspeed < 0 and (collision_rectangle(bbox_left-1+hspeed,bbox_top,bbox_left,bbox_bottom,obj_solid,1,1)) {move_contact_solid(180, 8) move_outside_solid(0,8) hspeed = 0}
```

Edited by hydroxy, 21 September 2010 - 07:36 PM.

• 0

### #2 Alex.Neumann12@gmail.com

Alex.Neumann12@gmail.com

GMC Member

• New Member
• 31 posts

Posted 21 September 2010 - 07:41 PM

Upon a high drop my character gets occassionally stuck in the ground by about 1 or 2 pixels, unable to move as he is colliding with solid objects in the floor. I was hoping someone could help me solve this problem so he doesn't get stuck anymore. Here is the basic physics code from my game. I'm pretty sure this happens because the vspeed is too high on the approach to the ground so the character is positioned inside the ground on the final step. I need a way to prevent this.

```//basic physics

if place_free(x,y+1) and place_free(x,y+vspeed) {gravity = 0.60 gravity_direction = 270}

if (hspeed >= 5) then hspeed = 5

if (hspeed <= -5) then hspeed = -5

if (vspeed >= 10) then vspeed = 10

if (vspeed <= -10) then vspeed = -10

if hspeed!=0 and (!place_free(x,y+1)) then {friction=0.4}

if vspeed > 0 and (collision_rectangle(bbox_left,bbox_bottom,bbox_right,bbox_bottom+1,obj_solid,1,1)) {move_contact_solid(270, 16) move_outside_solid(90,16) gravity = 0 vspeed = 0}

if vspeed < 0 and (collision_rectangle(bbox_left,bbox_top-1,bbox_right,bbox_top,obj_solid,1,1)) {move_contact_solid(90, 8) move_outside_solid(270,8) vspeed = 0}

if hspeed > 0 and (collision_rectangle(bbox_right,bbox_top,bbox_right+1+hspeed,bbox_bottom,obj_solid,1,1)) {move_contact_solid(0, 8) move_outside_solid(180,8) hspeed = 0}

if hspeed < 0 and (collision_rectangle(bbox_left-1+hspeed,bbox_top,bbox_left,bbox_bottom,obj_solid,1,1)) {move_contact_solid(180, 8) move_outside_solid(0,8) hspeed = 0}
```

Well it seems like you could use something along the lines of
`if place_meeting(x,y,obj_(your floor)) then {move_contact_solid/all(90, 5)`
Something along those lines.
• 0

### #3 superjoebob

superjoebob

YM2612

• New Member
• 1515 posts

Posted 21 September 2010 - 07:42 PM

A quick fix is to use something like

while( collision_point( x, y + 1, objFloor, true, true ) )
{
y -= 1;
}

Replace the +1 after y with whatever works for you. The only problem with that is you're using a while loop in gameplay code, which usually doesn't make much sense. The good thing about it, is that unless you counteract the movement within the loop, it should never get stuck in an infinite loop.

It's also worth mentioning that this is mostly for keeping the player above the ground on every frame, so you don't see the player slowly gliding up through the ground when they adjust their height. An if inplace of the while will work aswell, but you might see some jitter.

Edited by superjoebob, 21 September 2010 - 07:51 PM.

• 0

### #4 hydroxy

hydroxy

GMC Member

• GMC Member
• 280 posts
• Version:GM:Studio

Posted 21 September 2010 - 07:49 PM

Well it seems like you could use something along the lines of

`if place_meeting(x,y,obj_(your floor)) then {move_contact_solid/all(90, 5)`
Something along those lines.

I already have that in the code

A quick fix is to use something like

while( collision_point( x, y + 1, objFloor, true, true ) )
{
y -= 1;
}

Replace the +1 after y with whatever works for you. The only problem with that is you're using a while loop in gameplay code, which usually doesn't make much sense. The good thing about it, is that unless you counteract the movement within the loop, it should never get stuck in an infinite loop.

I would rather not have the character in the ground at all to be honest, is there a way that I can just keep him above the solid ground at all times, maybe move him to the ground level if he is going at high speeds and is about 10 pixels away from the ground?

• 0

### #5 billydoesitbest

billydoesitbest

GMC Member

• GMC Member
• 445 posts

Posted 23 September 2010 - 07:41 PM

A possible solution could be to use a collision line.

ex -

```var block;

block = collision_line(x, y, x, y + vspeed, obj_floor, precise, notme);

if (block > 0){
y = other.y - other.sprite_get_yoffset();
}```

should do the trick.
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users