Jump to content


Photo
* * * * - 3 votes

Simplest Platform Movement/Collisions


  • Please log in to reply
26 replies to this topic

#1 Canite

Canite

    Canigget

  • GMC Member
  • 1358 posts
  • Version:GM8

Posted 22 March 2011 - 10:20 PM

  • Title: Simplest Platform Movement and Collisions
  • Description: This may be simple, but it is very efficient and completely glitch free platform movement/collisions. The collsions are pixel perfect and uses no solids and no collision events.
  • GM Version: GM 8
  • Registered: No
  • File Type: .gmk
  • File Size: .01 mb
  • File Link: Simplest Platform Movement.gmk
Additional Info
Technically, I could have made the code even shorter, but I lengthened it out to keep it organized :P Also every line is commented and it should be very easy to follow. The perfect engine for any begginner who wants glitch free platforming without all the extra complicated add-ons that other tutorials have.
  • 4

#2 Grix

Grix

    GMC Member

  • New Member
  • 78 posts

Posted 23 March 2011 - 05:11 PM

Looking good!

Would be best to have some friction in the horizontal movement though, so it looks smoother.
  • 0

#3 golden_cow2

golden_cow2

    GMC Member

  • New Member
  • 178 posts

Posted 23 March 2011 - 07:25 PM

You can hover if you repeatedly release the up button.

Needs to be if (keyboard_check_released(vk_up) && vspd < 0)
  • 0

#4 lasttea999

lasttea999

    GMC Member

  • GMC Member
  • 290 posts

Posted 23 March 2011 - 11:33 PM

Simple, solid, nice. You might want to add more features--- for example, any speed between 0 and 1 would fail to move the player, right? --- but I guess more features would make it less and less simple. Consider taking a look at this engine by Matt Thorson: Click here

Edited by lasttea999, 23 March 2011 - 11:36 PM.

  • 1

#5 Jeesie

Jeesie

    GMC Member

  • New Member
  • 18 posts

Posted 21 April 2012 - 05:54 PM

Very nice! I am using your script for my new game, but I can't figure out how to make a jumpthrough platform with this code.

I have: obj_player, obj_block and obj_block_jump (the jumptrough platform). This is what I made (isn't working):
repeat(abs(vspd)) // we want to check for a collision every pixel, so we use a repeat() function to check every pixel while falling
{
    if !place_meeting(x, y+sign(vspd), obj_block)  // vspd can be negative or positive, so we check 1 pixel above or below, depending on which direction we are going
    {
        y += sign(vspd); // add to our y value 1 pixel at a time, letting use check for a collision every pixel
    }
    else if place_meeting(x, y+sign(vspd), obj_block_jump)
    {
        y += sign(vspd);   
    }
    else // else if we hit a block
    {
        vspd = 0; // stop moving vertically
    }
}

Can you help me out?
  • 0

#6 Canite

Canite

    Canigget

  • GMC Member
  • 1358 posts
  • Version:GM8

Posted 22 April 2012 - 04:48 PM

Here's an updated version that includes the jumpthrough platforms :happy:
Simplest Platform Movement + Jumpthrough
If anyone has feature requests, I can attempt to revise for them, but my first post will stay as the original, to keep it simple.
  • 0

#7 Jeesie

Jeesie

    GMC Member

  • New Member
  • 18 posts

Posted 22 April 2012 - 07:51 PM

Wow, it works great! Thank you very much.

I also made trampolines in my game by adding this to your script:
if !place_meeting(x, y+1, obj_block)  && (!place_meeting(x,y+1,obj_block_through) or place_meeting(x,y,obj_block_through))
{
    grav = 2;
    vspd += grav;
}
else if place_meeting(x, y+1, obj_block_jump)
{
    vspd = -30;
}
else
{
    grav = 0;
    vspd = 0;
    if keyboard_check_pressed(vk_up)
    {
        vspd = -22;
    }
}

And also give obj_block_jump a parent: obj_block.

Could you also explain moving platforms?

Edit
I also added another future to the jumpthrough platform:
if !place_meeting(x, y+1, obj_block)  && (!place_meeting(x,y+1,obj_block_through) or place_meeting(x,y,obj_block_through))  // if we are in the air
{
    grav = 2; // set the gravity to .5
    vspd += grav; // add .5 to our vspd once every step
}
else if place_meeting(x, y+1, obj_block_jump)
{
    vspd = -30;
}
else if place_meeting(x, y+1, obj_block_through) && keyboard_check_pressed(vk_down)
{
   self.y = (other.y+1)
}
else // else if we are on the ground
{
    grav = 0; // set the gravity to 0
    vspd = 0; // set vspd to 0 to stop moving
    if keyboard_check_pressed(vk_up) // since we are on the ground, we can handle jumping here, so check if we pressed up
    {
        vspd = -22; // set the vspd to -8, which will make us jump
    }
}

This will make the obj_player go through the jumpthrough platform (fall down) when you press the down arrow while standing on a jumpthrough platform.
But I know this isn't a good way because the player could get stuck, what do you suggest to do here? This is the part of the code I have added:

else if place_meeting(x, y+1, obj_block_through) && keyboard_check_pressed(vk_down)
{
   self.y = (other.y+1)
}

Kind regards,

Jeesie

Edited by Jeesie, 22 April 2012 - 08:06 PM.

  • 1

#8 oobanoob

oobanoob

    GMC Member

  • GMC Member
  • 145 posts
  • Version:Unknown

Posted 29 April 2012 - 08:56 PM

Loving this code. It's taught me a lot.

Edited by oobanoob, 01 May 2012 - 10:29 AM.

  • 0

#9 NeoTalon27

NeoTalon27

    GMC Member

  • GMC Member
  • 220 posts
  • Version:GM:HTML5

Posted 04 May 2012 - 03:56 PM

This is fantastic, I feel a very deep affection towards you for creating this 'Awesome' Tutorial. Platforming games are the only games I ever had trouble developing, thanks a ton! :biggrin:
  • 0

#10 Hypersense

Hypersense

    GMC Member

  • GMC Member
  • 5 posts
  • Version:GM8

Posted 08 May 2012 - 03:27 PM

I absolutely love this tutorial. Thank you!

I also have a question: What if I want to use another object that acts the same as obj_block? Imagine I can turn an enemy into a block of stone, how would I have to edit the script to also work for obj_stone?

EDIT:

Nevermind, I found it thanks to the posts above! Just made the codes that looked like this:
if !place_meeting(x, y+1, obj_block)

Like this:
if !place_meeting(x, y+1, obj_block)  && !place_meeting(x, y+1, obj_stone)

Edited by Hypersense, 08 May 2012 - 04:16 PM.

  • 0

#11 nanoviro

nanoviro

    GMC Member

  • GMC Member
  • 62 posts
  • Version:GM8

Posted 09 May 2012 - 05:28 AM

Server Lagged and caused me to post 3 times....
Mod please delete this post and the one 2 below if possible.

Edited by nanoviro, 09 May 2012 - 05:32 AM.

  • 0

#12 nanoviro

nanoviro

    GMC Member

  • GMC Member
  • 62 posts
  • Version:GM8

Posted 09 May 2012 - 05:28 AM

So I have been trying to implement this into one of my platform games. //colllide with non-solids

hspeed = (keyboard_check(vk_right)-keyboard_check(vk_left))*player_speed; // a nice little trick that will set our hspd to +4 or -4 depending what button we are holding
repeat(abs(hspeed)) // same as the vspd, we want to check for a collision at every pixel we move
{
if !place_meeting(x+sign(hspeed),y,Obj_Solid) // if there is no block to our left or right
{
x += sign(hspeed); // add to our x value depending on which way we are going
}
}

//if place_meeting(x+hspeed,y,Obj_Solid)
//{
// hspeed=0
//}
if place_meeting(x,y+vspeed,Obj_Solid)
{
vspeed=0
gravity=0
move_contact_all(270,0)
}



That is the code that I put in. I was sure to change everything to work with the changes. The problem is my hspeed ignores the wall and simply goes right through it. While inside of the walls it has no friction so I thought it may be working a bit right there. One issue is when I go off a small jump and land while holding the left or right arrow key the player goes into the block a little ways. Wasn't sure why this was happening or how to fix it. Any help would be greatly appreciated.
  • 0

#13 nanoviro

nanoviro

    GMC Member

  • GMC Member
  • 62 posts
  • Version:GM8

Posted 09 May 2012 - 05:30 AM

Server Lagged and caused me to post 3 times....
Mod please delete this post and the one 2 Above if possible.

Edited by nanoviro, 09 May 2012 - 05:32 AM.

  • 0

#14 Roadrager

Roadrager

    GMC Member

  • New Member
  • 28 posts
  • Version:GM8

Posted 11 May 2012 - 01:16 PM

I am using your script and it is great however when i put more than 1 object it doesnt work for the other objects. I am new to this and so please make exceptions if i have made a stupid mistake. This is what i have done, what can i do to make it work?

if !place_meeting(x,y+1,obj_block or obj_block1 or obj_wood)
{
grav = .5;
vspd += grav;
}
else
{
grav = 0;
vspd = 0;
if keyboard_check_pressed(vk_up)
{
vspd = -8;
}
}
if keyboard_check_released(vk_up)
{
vspd *= .5;
}
if vspd > 8
{
vspd = 8;
}
repeat(abs(vspd))
{
if !place_meeting(x,y+sign(vspd),obj_block or obj_block1 or obj_wood)
{
y += sign(vspd);
}
else
{
vspd = 0;
}
}
hspd = (keyboard_check(vk_right)-keyboard_check(vk_left))*4;
repeat(abs(hspd))
{
if !place_meeting(x+sign(hspd),y,obj_block or obj_wood or obj_block1)
{
x += sign(hspd);
}
}

Think im sorted now

Edited by Roadrager, 11 May 2012 - 01:35 PM.

  • 0

#15 Freeload

Freeload

    GMC Member

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

Posted 03 August 2012 - 01:35 PM

@OP

This is really cool.

Is there a simple way to use this for a character that is automatically moving left or right, who will change direction when they hit a block from the side and otherwise fall off as per normal?

Kinda like it would work in a Lemmings or Mario & Wario style game for example. If the character could do this on slightly uneven terrain it would be even better (if the height is too high they wouldn't be able to go up it and would change direction instead).

Any help would be much appreciated.

Thanks,

Kirk

Edited by Freeload, 03 August 2012 - 01:50 PM.

  • 0

#16 Bladderbloat

Bladderbloat

    GMC Member

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

Posted 23 August 2012 - 02:24 PM

This is great! Would it be possible to add some kind of code for ladders in there? I tried using whatever tiny skills I had but the player just kept sliding down slowly from the ladder. Could it be the smooth variable jumping when vk_up is released?

Learned much from this!

Thanks!
  • 0

#17 Ruub

Ruub

    Finn The Human

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

Posted 01 February 2013 - 04:13 PM

I love how simplistic the code is and at the same time it works so freakishly good.

Quote that.

I would also wonder how you would go for moving platforms and top down :)

Edited by Ruub, 01 February 2013 - 04:28 PM.

  • 0

#18 joe331

joe331

    GMC Member

  • New Member
  • 1 posts
  • Version:GM8.1

Posted 22 March 2013 - 04:49 AM

This is superb! Thanks a lot man!

 

I was learning to put together a mechanism that allows movements like in megaman games. At first I tried without coding to learn the logic behind the mechanic. I manage untill I met a frustating glitch when my jumping animation fail to show up when I press right + up, while the left + up is perfect. Left and Right is exactly  identical, I had no idea why it was like that.

 

Then I met this code example/tutorial, which showed me the power of coding. Simple few lines of code can really does a lot of stuff. I guess coding is the true way to go then. My coding background was only html, css, and a bit of php. But I manage to learn a lot from this example, plus the help section. I followed the logic and pattern and looked up some commands in the help section. 

 

I have successfully remake the movement coding to my practice game (step by step so I can understand the mechanism better). And then I also use coding for sprite control (for the movements). After a bit of trial and error, and rewriting for efficiency. I successfully attach sprites to the movements. Works perfectly, very satisfying :D


  • 0

#19 Spacemoose

Spacemoose

    GMC Member

  • New Member
  • 11 posts
  • Version:GM8.1

Posted 20 May 2013 - 02:34 PM

How do i fix the button-bash hovering? nice code so far


  • 0

#20 proon

proon

    GMC Member

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

Posted 21 May 2013 - 12:52 PM

Add slopes and it will be perfect! :P


  • 0

#21 chestertagat

chestertagat

    GMC Member

  • Banned Users
  • 17 posts
  • Version:GM:Studio

Posted 29 May 2013 - 08:28 AM

no glitches ,,

great work..



#22 chestertagat

chestertagat

    GMC Member

  • Banned Users
  • 17 posts
  • Version:GM:Studio

Posted 29 May 2013 - 08:29 AM

would be better if you put nicer sprite with animation..



#23 Wiiboy4ever

Wiiboy4ever

    GMC Member

  • GMC Member
  • 117 posts

Posted 28 February 2014 - 10:08 AM

Really sorry for the monstrous bump, but can anyone fix the double jump throughplatform glitch? Whenever jmpthrough platforms overlap, the player just falls straight through both of them.... this is very unfortunate considering this looked like a great example..... :(


  • 0

#24 Thorgi

Thorgi

    GMC Member

  • New Member
  • 11 posts
  • Version:GM:Studio

Posted 03 March 2014 - 08:23 PM

I imported this into GM:S and it works perfectly. Better than other out-of-the-box platform "engines" I have tried. I'll try to adapt this to my game and add more layers to it to help me learn.

 

Am I right in thinking that..

hspd = (keyboard_check(vk_right)-keyboard_check(vk_left))*4;

.. works by translating the keypresses into boolean and multiplying them by a set speed? If so, that's awesome.

 

Really sorry for the monstrous bump, but can anyone fix the double jump throughplatform glitch? Whenever jmpthrough platforms overlap, the player just falls straight through both of them.... this is very unfortunate considering this looked like a great example..... :(

 

I'm having trouble understanding what you mean. I tried replicating your issue by stacking instanced os 'obj_jumpthrough' onto itself, I tried layering them, etc. Never fell through them and I could always jump to the topmost 'obj_jumpthrough' platform.


  • 0

#25 stevenblanc

stevenblanc

    GMC Member

  • New Member
  • 4 posts
  • Version:GM:Studio

Posted 05 March 2014 - 02:24 PM

Really sorry for the monstrous bump, but can anyone fix the double jump throughplatform glitch? Whenever jmpthrough platforms overlap, the player just falls straight through both of them.... this is very unfortunate considering this looked like a great example..... :(

 

The easiest way to do that is just to adjust your through platform sprite. In reality you only need a pixel width for the sprite, so unless you need two platforms one pixel apart they would never touch.

 

Just make the sprite one pixel and align with the top of the platform.


  • 0

#26 Elrien

Elrien

    GMC Member

  • GMC Member
  • 34 posts
  • Version:Unknown

Posted 16 June 2014 - 10:03 PM

hi, i've found out now about you're script and reading the comments it sounds great, and i've tried to download it, but i can't see it. is there a way you could put the code in here so that i could use it too? i really don't know how to solve this collision problem... thank you anyway :thumbsup:


  • 0

#27 cyborgpope

cyborgpope

    GMC Member

  • New Member
  • 1 posts
  • Version:GM:Studio

Posted 26 September 2014 - 10:20 PM

So if I would want to add a "run"  button what would the code look like?

 

I tried:

if keyboard_check(vk_space);
 {
 hspd =+7;
 }
 
But I have no idea what I'm doing and it didn't work.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users