Jump to content


Photo
- - - - -

Smooth Platform


  • Please log in to reply
1 reply to this topic

#1 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 14 October 2011 - 08:30 AM

  • Title: Smooth Platform
  • Description: (Almost) Perfect simple platform collision system
  • GM Version: :GM7:
  • Registered: insert yes if it requires Pro, otherwise, no
  • File Type: .gmk :GM7:
  • File Size: 4k
  • File Link: http://www.host-a.ne...napplatform.gmk
Summary
Shows when you setup your tile size, position and player properly you can achieve smooth platform collision detection with very simple code.

Thanks to the new demo in html5 for platform movement and this topic (Read It):
http://gmc.yoyogames...1

I have implemented a Windows GML version of the movement described there using methods you may be more familiar with. No Boolean logic or map array involved like the very informative topic talks about. Only the alignment method described is used here

I strongly recommend you use the method discussed in that topic for HTML5 implementation as that method is most efficient for that platform.

However, if you have trouble with understanding how that works, the method here may help grasp the concept.

The method here used in this Windows version is designed to use GM's standard collision function calls and move_snap for it's implementation.

In any case, this very simple code will help you grasp the concept and likely solve all your platform problems for non moving walls.

Note: The black block in the demo is made to show improper setup behavior

///////////////////////
//could be in the create
//gravity, we dont change it live
gravity = .5;
//snap width and height
snapw = 32;
snaph = 32;
//movement speed
lrspeed = 4; //must be less than your snapw;
//jump vel
jumps = -9; //must be less than your snaph;
//falling cap (must be less than your snaph to prevent penetration)
fallcap = 12;

///////////////////////
//This code belongs on the END STEP

//get left right, could be x+= instead of hspeed = 
hspeed = (keyboard_check(vk_right) - keyboard_check(vk_left))*lrspeed; //gives possibility (-1, 0, 1) * lrspeed

//the edge to allow falling when not completely over the edge
edge = 4;
//up and down detect, line with edge works best
upi = collision_line(bbox_left+edge, bbox_top, bbox_right-edge, bbox_top, wallObj, 0, 1);
//upi = collision_point((bbox_left+bbox_right)/2, bbox_top, wallObj, 0, 1); 
//upi = instance_place(x, y-1, wallObj);
downi = collision_line(bbox_left+edge, bbox_bottom, bbox_right-edge, bbox_bottom, wallObj, 0, 1);
//downi = collision_point((bbox_left+bbox_right)/2, bbox_bottom, wallObj, 0, 1); 
//downi = instance_place(x, y+1, wallObj);

//handle floor and ceiling
if(upi != noone or downi != noone)
{
//basically, this matches your player height, and the snap combined with the tile position and size
//and your player position and origins will snap right outside the collision
//If your player snaps in a wall, it's because the snap is off or the wall is miss aligned
//or your are moving too fast for your setting like that black wall in the demo shows
    move_snap(0, snaph);
    //stop the movement
    vspeed = 0;
    //check for jumping, we are on floor, no ceiling above and hitting jump
    if(downi != noone and upi == noone and keyboard_check_pressed(vk_up)) 
        vspeed = jumps;
}


//left and right detect, line with edge works best
lefti = collision_line(bbox_left, bbox_top+edge, bbox_left, bbox_bottom-edge, wallObj, 0, 1);
//lefti  = collision_point(bbox_left, (bbox_top+bbox_bottom)/2, wallObj, 0, 1);
//lefti = instance_place(x-1, y, wallObj);
righti = collision_line(bbox_right, bbox_top+edge, bbox_right, bbox_bottom-edge, wallObj, 0, 1);
//righti = collision_point(bbox_right, (bbox_top+bbox_bottom)/2, wallObj, 0, 1); 
//righti = instance_place(x+1, y, wallObj);

if(lefti != noone or righti != noone)
{
//basically, this matches your player width, and the snap combined with the tile position and size
//and your player position and origins will snap right outside the collision
//If your player snaps in a wall, it's because the snap is off or the wall is miss aligned
//or your are moving too fast for your setting like that black wall in the demo shows
    move_snap(snapw, 0)
    //stop the movement if going towards the wall
    if(hspeed <0 and lefti) 
        hspeed = 0;
    else if(hspeed >0 and righti) 
        hspeed = 0;
}


//clamp the falling speed
vspeed = min(vspeed, fallcap);

//done
///////////////////////


  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#2 Trinnexx

Trinnexx

    GMC Member

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

Posted 01 April 2012 - 04:43 AM

Interesting stuff, and all in the one script too! Collision seem very stable.
The use of move_snap() and nothing else is interesting.
  • 0