# The logic behind jump and collisions

3 replies to this topic

### #1 Tartine

Tartine

GMC Member

• New Member
• 23 posts
• Version:GM8

Posted 12 May 2012 - 08:21 PM

Hello !

So, now that the right and left navigation of my character are working with GML, I'm trying to implement the jump and collisions system and obviously it didn't work and once again I need your help to figure it out.

what I want : I just want my character to jump if you press up arrow key. To fall if there is nothing underneath him and to collide with solid blocks.

What happens : The jump is working but when the character is landing, he doesn't stop on the top of the block but somewhere in the middle of the solid block. My character can go through block on his left and right but if I stop him at the same place as a block, he get stuck on it. The landing is working but have the same problem as the jump for the landing.

So, this is my code. Let me know if you don't understand something. As always, I don't want you to make the code for me, I want to understand what happens and how to make it work.
Thank you

create event :
```//Variables
hspeed = 0
vspeed = 0
gravity = 0
onGround = true // false = Character jumping or falling ; true = Character on the ground
```
step event :
```//**Collisions**
if !place_free(x,y+1) // check if the character is on the ground
{
onGround = true;
}

if place_free(x,y+1)    // check if the character isn't on the ground
{
onGround = false;
}

if onGround = true  // if the character is on the ground, all right, let him where he is
{
gravity = 0;
vspeed = 0;
}
if onGround = false //if there character isn't on the ground, bring him down
{
gravity += 0.2;
move_contact_solid(270,1); // move down until you collide with something solid
}

//**Jump**
if keyboard_check(vk_up) and onGround = true
{
vspeed -= 6;
sprite_index = MainGuy_Jump;
onGround = false;
}

//**Run to the right**
if keyboard_check(vk_right) and place_free(x+1,y) // move to the right only if there is free space at x+1 and you press right arrow
{
hspeed += 1
sprite_index = MainGuy_Run_Right;
image_speed = 0.5 ;
}
if hspeed > 5
{
hspeed = 5;
}
if (keyboard_check(vk_nokey))
{
hspeed = 0;
sprite_index = MainGuy_Stand ;
image_speed = 0.3;
}

//**Run to the left**
if keyboard_check(vk_left) and place_free(x-1,y) // move to the left only if there is free space at x-1 and you press left arrow
{
hspeed -= 1
sprite_index = MainGuy_Run_Left ;
image_speed = 0.5 ;
}
if hspeed < -5
{
hspeed = -5;
}
if (keyboard_check(vk_nokey))
{
hspeed = 0;
sprite_index = MainGuy_Stand ;
image_speed = 0.3;
}```

Edited by Tartine, 12 May 2012 - 08:27 PM.

• 0

### #2 CobraA1

CobraA1

Logic Fanatic

• New Member
• 542 posts

Posted 12 May 2012 - 08:44 PM

I suggest taking a look at the "platform games" tutorial.

Also, check out the "else" statement. You're checking some things twice that only need to be checked once.

Are you limiting the maximum vertical speed?
• 0

### #3 Tartine

Tartine

GMC Member

• New Member
• 23 posts
• Version:GM8

Posted 12 May 2012 - 09:27 PM

Are you limiting the maximum vertical speed?

No, should I ? In what purpose ?

I saw that the tutorial was made with the Drag & Drop system, but I want to do it with the GML.

Edited by Tartine, 12 May 2012 - 09:59 PM.

• 0

### #4 CobraA1

CobraA1

Logic Fanatic

• New Member
• 542 posts

Posted 12 May 2012 - 11:08 PM

Are you limiting the maximum vertical speed?

No, should I ? In what purpose ?

I saw that the tutorial was made with the Drag & Drop system, but I want to do it with the GML.

Beyond a certain speed, you are moving more than 270 pixels per step, at which your move_contact_solid becomes insufficient. Also, it basically outstrips what is reasonable for gameplay. A maximum vertical speed keeps gameplay reasonable and avoids some bugs associated with extreme speeds.

And oh: I noticed you are doing something with your gravity.

```gravity += 0.2;
```

You are increasing gravity with every step! Which is unlikely to be what you want: For most games gravity is constant, it's the speed that changes.

You can still look at the tutorial, as it may still be useful. If you want to use GML, that's fine, you should be able to find GML equivalents of everything drag and drop.

Dunno if any of that will fix your problems, but maybe you can get some ideas from the tutorial.
• 0

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

0 members, 0 guests, 0 anonymous users