Game Maker's Garage Forum

Game Maker's Garage => Trash Talk => Topic started by: Connors on August 24, 2012, 01:18:00 AM

Title: Platforming and Collision Code
Post by: Connors on August 24, 2012, 01:18:00 AM
I've re-updated an older version of an unfinished tile engine beta. And if you can believe that...

I'm attaching the file as it is so far. Use with SC 2.0b3.1. The collision is near perfect except at absurd speeds. I'm going to have to tweak it if/when I add enemies but the collision and "physics" code should work with many objects!

Always save incrementally gentlemen. It could save you hours of work.

The biggest difference is I changed the collision code to output what tiles each corner is hitting for x and y, for a total of 8 variables in the array called cp$(). The array helps me check more than one with ease.

Oh and i scribbled a new logo on top of the old one because i got bored.
Title: Re: Platforming and Collision Code
Post by: Connors on August 24, 2012, 02:07:45 AM
I've got a peculiar bug to pin down in my collision code where he reaches the edge of a block and snaps to the edge of the next one over. This is because somewhere it returns the wrong tile to stop at the edge of.

It is most easily recreated by running to the left starting from a standstill over the tops of the single blocks in a row.

My only leads are that it seems to only occur if x and/or y speed is negative, and if you are hitting the corner of a tile. It is the same bug that on rare occasion makes you warp upwards onto a ledge if you hit a lower right corner. Which could be a total game breaker.
Title: Re: Platforming and Collision Code
Post by: Gan on August 24, 2012, 09:45:05 AM
Biggest issue I've found is the Command key is a bad jump key. Cause when I move and jump, it flips my screen into Dash Board.

I'm still trying to recreate the bug you experienced.

Edit: I can see it. Hmm. I'm going to have to do a screen recording and slow it down to take a closer look.
Video: http://cl.ly/2G3Y07080R3B (http://cl.ly/2G3Y07080R3B)

So strange.If you move too fast the bug doesn't happen. If you move just slow enough.... he skips to the left. Interesting.

Edit 2: I've pinpointed the bug. It is in this chunk:
Code: [Select]
IF xvel(obj) < 0 THEN
   //left
   LET nextx = ( xpos(obj)+xvel(obj) )
   FOR n = 0 to 1
      LET nexty = ( ypos(obj) + (sizey(obj)*n) ) -(1*n)
     
      LET cp$(5+n) = maptile$(mapx(nextx),mapy(nexty))
     
      IF cp$(5+n) = "1" THEN  //
         LET xvel(obj) = 0
         LET xpos(obj) = mapx(xpos(obj))*32-32
      END IF                                                                    //
   NEXT
END IF
Now if I comment these two lines, the game appears to play the same, but without that jumping:
Code: [Select]
IF xvel(obj) < 0 THEN
   //left
   LET nextx = ( xpos(obj)+xvel(obj) )
   FOR n = 0 to 1
      LET nexty = ( ypos(obj) + (sizey(obj)*n) ) -(1*n)
     
      LET cp$(5+n) = maptile$(mapx(nextx),mapy(nexty))
     
      IF cp$(5+n) = "1" THEN  //
         //LET xvel(obj) = 0
         //LET xpos(obj) = mapx(xpos(obj))*32-32
      END IF                                                                    //
   NEXT
END IF
Title: Re: Platforming and Collision Code
Post by: Connors on August 24, 2012, 11:43:56 AM
Yes Gan you have effectively disabled him stopping when he detects a wall on the left.
I'm almost certain it has to do with my maptile$() method. All collision does is if his next position, based on velocity, would be inside a wall, he instead stops at the edge of that tile. I think I have some more things I can try now actually.
Title: Re: Platforming and Collision Code
Post by: Gan on August 24, 2012, 12:05:39 PM
Ah hahah.  ;D  So that's what that code does....

Hypothesis:
That code makes him stop from going into a wall on the left side.
The movement skipping only happens when he's moving left at half max speed.
Half max speed means he'll still go over the tile, but he drops ever slightly mid-gap.
Lets say he is moving left at half speed. He drops a few pixels into the gap but he's still moving fast enough to the left. Then collision happens, but another piece of code sees that he's not low enough so he can still go on top. Therefore the other piece of code hoists him up back on top, while the left collision block code still activated, saying he should stop at the block edge. But cause his position was hoisted up, it snaps him to the opposite edge.

That's my hypothesis why the snapping doesn't occur when you disable the left blocking code.
Title: Re: Platforming and Collision Code
Post by: Connors on August 24, 2012, 12:30:10 PM
Well Gan you're right about the cause, but it was a side effect of me using a weird position for mapx(). Mapx() and mapy() return the tile map coordinate of an x or y position, respectively. I have code that snaps him to the edge of the tile he is on so he hits the wall. I checked the map position with the very edge of his sprite not knowing that it could overlap and return the coordinate of the next tile. I have fixed the bug by giving mapx/y() a coordinate toward the center of the tile.
Title: Re: Platforming and Collision Code
Post by: Gan on August 24, 2012, 01:14:18 PM
Epic sauce!

I suppose the next step would be to fix tunneling.
If you fall in an endless loop, you eventually move fast enough to go straight through a block.
Therefore I propose to make the logic steps into a for loop that runs steps in small increments depending on velocity.
For example, hardly moving and it does 1 logic step per timer tick.
Moving fast then chops the frame into 10 logic steps per timer tick.
So each logic step is pretty much the same amount of (tiny/incremental) movement so block dude doesn't skip over any blocks.
Title: Re: Platforming and Collision Code
Post by: Connors on August 24, 2012, 02:27:08 PM
Actually I'm just gonna put a cap on velocity that's less than the size of tiles. The speed required to warp through a tile is far greater than a reasonable limit.
Title: Re: Platforming and Collision Code
Post by: Connors on August 24, 2012, 03:52:08 PM
I'm trying to make the game decide which tile to use based on what tiles are nearby, so that they fit together. If nothing's above it, it's grassy, if nothing's below it, it's got a rough bottom, if nothing's above or below it has both, otherwise it has neither. That's only four sprites. And guess what Gan? If I try to do checks above and below it draws everything in the wrong place. It's impressively glitchy considering it should only draw a sprite if the FOR loop's on a solid tile.
Title: Re: Platforming and Collision Code
Post by: Connors on August 24, 2012, 04:02:20 PM
I'd like you to run the program, and then comment out the following in the method " drawmap " and tell me why this makes it draw all tiles 1 square beneath.

Code: [Select]
         IF maptile$(x,y-1) = "1" then
            LET sprite$ = "above"
         END IF
Title: Re: Platforming and Collision Code
Post by: Gan on August 24, 2012, 04:32:30 PM
Fixed!

Turned out to be a problem involving how SilverCreator handles global variables.

This is your code:
Code: [Select]
LET sprite = 1
LET windowTXT$ = "Start"
FOR x = 1 to 25
   FOR y = 1 to 18
      LET pic$ = maptile$(x, y)
      IF pic$ = "1" then

         LET sprite$ = "neither"
         IF maptile$(x,y-1) = "1" then
            LET sprite$ = "above"
         END IF
         
         CREATESPRITE sprite, sprite$
         MOVESPRITE sprite, x*32-32, y*32-20, TRUE
         LET sprite = sprite + 1
      END IF
   NEXT
NEXT
Notice how you call maptile$(x, y-1)?
Now look at the maptile method code:
Code: [Select]
IF x < 26 then
   IF x > 0 then
      IF y < 19 then
         IF y > 0 then
            LET tile$ = NTHFIELD$(map$(y), ",", x)
            RETURN tile$
         END IF
      END IF
   END IF
END IF

The problem is that all variables in Sc are global. And cause in Sc you have X and Y as parameters for this method, when you call maptile$(x, y-1), you are setting x = x and y = y - 1.
That is why there is that offset!

Easy fix, just change the method to this:
(http://cl.ly/image/092i0s2M120Z/Screen%20Shot%202012-08-24%20at%204.31.41%20PM.png)
Use different non-used variables for the parameters. x1 instead of x, and y1 instead of y.
Title: Re: Platforming and Collision Code
Post by: Connors on August 24, 2012, 04:39:23 PM
Isn't this a bug?
I've NEVER had variables change when I used them for parameters. Plus if you toy with the original file one more time you'll see that using (x,y+1) doesn't mess it up!
Title: Re: Platforming and Collision Code
Post by: Gan on August 24, 2012, 04:44:16 PM
This is certainly and interesting predicament.
Though it appears to be on the safe side, it is best to use different variables for parameters.

Anywho, can you change the jump key? It is a tad annoying.
Title: Re: Platforming and Collision Code
Post by: Connors on August 24, 2012, 04:51:14 PM
I think it's still affecting my FOR loop if I do checks within that since I had array out of bounds errors.
EDIT: Fixed, thanks to Gan. Don't know how I got outside the field of play but NOW IT'S OK.
I'm finishing the magic tile alignment thing.
EDIT: IM POSTING THIS B*TCH.
CAPS = ENTHUSIASM.
Title: Re: Platforming and Collision Code
Post by: Zoo on August 24, 2012, 05:58:03 PM
add a goal, make a level editor. I will help.
Title: Re: Platforming and Collision Code
Post by: Zoo on August 24, 2012, 06:05:10 PM
Oh, and make the sides loop too, unless you're gonna add scrolling
Title: Re: Platforming and Collision Code
Post by: Connors on August 24, 2012, 07:47:28 PM
Chances are falling off the bottom will kill you in the final product! However the game mechanics are not yet in place, people. I need to do a little planning. I don't even know if this is going to be like Meat Boy, or be a puzzle game, or if it will have enemies to fight!
Chances are it will be some combination. Perhaps this is where I need some assistance - we can do it mspaintadventure style! You guys suggest what will happen, and I pick and choose which suggestions to follow and otherwise do what I want to regardless. (http://gamemakersgarage.com/forum/chat/Smilies/XDFace.gif)
Title: Re: Platforming and Collision Code
Post by: Connors on August 24, 2012, 10:22:20 PM
Oh god he can die now. And making that animation took longer than it logically should have. The lag is part of the game damnit!

PS: I was just having fun. %99 chance I'm gonna revert to #6 and program death better.
Title: Re: Platforming and Collision Code
Post by: Zoo on August 25, 2012, 11:00:57 AM
Love the death animation, needs a respawn button and a goal.
Title: Re: Platforming and Collision Code
Post by: Connors on September 03, 2012, 11:52:29 PM
This one is a proof of concept that shows the game can reset and switch levels no problem, if I get the level editor to behave than it's gonna be smooth sailing from here on out!

There's three levels, the last one's impossible.

The goal is green. Also, arrows move and option is jump.
Title: Re: Platforming and Collision Code
Post by: Zoo on September 04, 2012, 04:28:32 PM
SPACE OR UP TO JUMP!!! SERIOUSLY!!!

1. I like how he now respawns.
2. Flip the spikes that kill you from the bottom, and make normal spikes not kill you from below.
3. I like the character, he controls well, bouncy and fast, but still responsive and handles well.
4. He controls like super meat boy... Just sayin', I should be able to wall jump.
5. up to jump/space
6. Difficulty curve. You shouldn't die on the first level. It should be REALLY easy. The levels get gradually harder, teaching you how to play as you go along.
7. WHAT HAPPENED TO THE AWESOME DEATH ANIMATION?!?
Title: Re: Platforming and Collision Code
Post by: Connors on September 05, 2012, 04:18:28 PM
Here's it is people! This is the big one...
Included here is the latest version, plus a rudimentary LEVEL EDITOR!

...Warning: Level editor currently will not load levels and may never load levels unless we make a better way to do it! ;)

EDIT: I forgot to mention that I can still make changes to the controls, although this would mean different level designs. Possibilities include Wall-Jumping, holding jump to go higher and/or general changes to the physics. What do you all think?
Title: Re: Platforming and Collision Code
Post by: Zoo on September 05, 2012, 04:38:55 PM
Add wall jumping and low/high jumping. That's a good idea. I like the editor. Very... original idea...



Made you a level, editor works great, even though it still tells you the "enemy limit" and so on.

LET map$(1)  =  "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(2)  =  "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,G,"
LET map$(3)  =  "0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,"
LET map$(4)  =  "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(5)  =  "0,0,0,1,1,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(6)  =  "0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(7)  =  "0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(8)  =  "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(9)  =  "0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(10)  =  "0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(11)  =  "0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(12)  =  "0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(13)  =  "0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(14)  =  "0,0,p,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,"
LET map$(15)  =  "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,"
LET map$(16)  =  "2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
LET map$(17)  =  "2,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,"
LET map$(18)  =  "1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,"
Title: Re: Platforming and Collision Code
Post by: Zoo on September 05, 2012, 08:13:00 PM
found a glitch, you can fall through the side to the bottom
Title: Re: Platforming and Collision Code
Post by: Connors on September 05, 2012, 08:28:59 PM
Can you be more specific Zoo?
Title: Re: Platforming and Collision Code
Post by: Zoo on September 05, 2012, 08:29:46 PM
No wall on left edge, walk over edge so you just fall through. come out again under platform
Title: Re: Platforming and Collision Code
Post by: Connors on September 05, 2012, 08:40:50 PM
Perhaps I should have him die if he leaves the screen?
Title: Re: Platforming and Collision Code
Post by: Gan on September 05, 2012, 09:23:45 PM
Maybe just block off the sides?
Title: Re: Platforming and Collision Code
Post by: Circuit on September 05, 2012, 10:42:14 PM
Haha, I love what you did with the level editor  ;D
This game is really starting to look good.  The controls are nice and responsive.  The long jump at the end of level 1 makes it feel like the physics are tuned just right to make the game challenging.  And I like how the hat is a symbol of Square Dude's coolness, staying on his head while he's alive but flying off when he dies.
Title: Re: Platforming and Collision Code
Post by: Connors on September 06, 2012, 08:01:26 AM
Maybe just block off the sides?
First, I might add scrolling later. Second, that's pretty much up to the level designer.

Title: Re: Platforming and Collision Code
Post by: Connors on September 07, 2012, 10:03:28 PM
Guys I think I can add scrolling without a total rewrite of the sprite code for the game! I'll keep you posted.
EDIT: There are so many problems with this idea that I don't really wanna mess with it tonight.
Title: Re: Platforming and Collision Code
Post by: Gan on September 07, 2012, 11:23:45 PM
Usually adding scrolling only requires changing your drawing code to have a moveable "camera" that can follow the player.
Title: Re: Platforming and Collision Code
Post by: Zoo on September 08, 2012, 11:43:37 AM
Scrolling'd be cool.
Title: Re: Platforming and Collision Code
Post by: Connors on September 08, 2012, 04:45:31 PM
I kept the original code, which is fine, yet when I set "LET scroll = true" the map moves and the player acts as if his xpos variable is static.
Title: Re: Platforming and Collision Code
Post by: Gan on September 08, 2012, 05:22:03 PM
Fixed it for ya.
Title: Re: Platforming and Collision Code
Post by: Connors on September 08, 2012, 05:44:32 PM
Alright, this is fantastic! Now I can make big ol' levels where you have to make wall jumps and change direction in mid-air other such physics-defying nonsense.

May I ask what you did, and how I can constrain scrolling to the x or y axis if I want?
Title: Re: Platforming and Collision Code
Post by: Gan on September 08, 2012, 05:54:35 PM
Your code was right on for the most part.
I just made two variables:
Code: [Select]
LET cameraX = xpos(player) - 500
LET cameraY = ypos(player) - 300
MOVESPRITE object, x - cameraX, y - cameraY

This is the magic code that makes the universe revolve around the player. You can constrain with if statements:
Code: [Select]
IF cameraX > 500 THEN cameraX = 500
Title: Re: Platforming and Collision Code
Post by: Connors on September 08, 2012, 06:00:11 PM
Right. Great. And I can easily turn it on and off!

Now I need to remake the variable size editor. I could have sworn I already did this once before, though...