Jump to content


Photo

Trouble with Collision Detecting in Platformer


  • Please log in to reply
2 replies to this topic

#1 kirbyfreak

kirbyfreak

    GMC Member

  • GMC Member
  • 15 posts
  • Version:GM7

Posted 05 November 2011 - 03:42 PM

Hello! I'm currently trying to learn GML for a side-scrolling gun game where you can run around on the ground, and I'm trying to use collision detecting to make it so my character can land on the ground, and stop when he runs into walls and stops and falls when he jumps and hits something. Basic collision detecting. But I'm utterly failing. I kinda have it so when he lands he lands correctly, but if he tries to fall off a ledge onto the ground he just snaps to the ground, when he runs into the walls he either goes into them or magically teleports into oblivion, and when he jumps and hits his head, he slowly sinks out of the block... I've tried setting up 4 collision boxes around the character without precise collision, but... It's not working. Here's my code, if anyone's interested (part of it's for shooting his gun, but most of it is for moving). Any suggestions? Thanks for the help!
//left-check
if collision_rectangle(x,y,x+1,y+27,oSpaceWall,false,true)
 {gravityX = 0
  speedX = 0
  x = xprevious
  move_contact_solid(0,speedX)}
//right-check
if collision_rectangle(x+23,y+1,x+24,y+26,oSpaceWall,false,true)
 {gravityX = 0
  speedX = 0
  x = xprevious
  move_contact_solid(180,speedX)}
//up-check
if collision_rectangle(x+1,y,x+24,y+1,oSpaceWall,false,true)
 {gravityY = 0
  speedY = 0
  y = yprevious
  move_contact_solid(90,speedY)}
//down-check
if collision_rectangle(x+1,y+31,x+24,y+32,oSpaceWall,false,true)
 {gravityY = 0
  speedY = 0
  inAir = false
  y = yprevious
  move_contact_solid(270,speedY)}
if inAir = true
 {gravityY = 1}
//gravity adding to speed
speedX = speedX + gravityX
speedY = speedY + gravityY
//keyboard input
if keyboard_check(ord('W')) and inAir = false
 {speedY = -13
  inAir = true}
if keyboard_check(ord('A'))
 {speedX = -2
  sprite_index = sPlayer1l}
if keyboard_check(ord('D'))
 {speedX = 2
  sprite_index = sPlayer1r}
if keyboard_check(ord(false))
 {speedX = 0}
if keyboard_check(ord('S'))
 {if sprite_index = sPlayer1r
   {instance_create(x+12,y,oLaserShot)}
  if sprite_index = sPlayer1l
   {instance_create(x-12,y,oLaserShot)}}
//movement
x = x + speedX
y = y + speedY

Edited by kirbyfreak, 05 November 2011 - 06:18 PM.

  • 0

#2 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4689 posts
  • Version:GM8

Posted 06 November 2011 - 03:37 AM

Make the movement a result of the collision check. Also, use collision_line, it's better suited for what you're doing. Furthermore, this will bug out if speedX and speedY are greater than 1 or less than -1.

Good job making your own movement engine. Now you just need to find out how custom engines work. Check out The Granny Engine (google it). You can use his code if you want (I do), or edit yours to more closely match his. I'll tell you what it does, though. It stores the speeds in separate variables, subtracting 1 from them each step, then it moves 1 pixel until the backup variable is cleared out. Every time it tries to move the object 1 pixel, it checks for a collision first. If there isn't one, it moves it.
  • 0

#3 kirbyfreak

kirbyfreak

    GMC Member

  • GMC Member
  • 15 posts
  • Version:GM7

Posted 28 April 2012 - 01:39 AM

Thank you so much! :D My game's really progressed now, but now I'm stuck on online play. >.< Stupid 39dylib...
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users