GLBasic forum

Main forum => GLBasic - en => Topic started by: kamakazieturtle on 2008-Dec-13

Title: Help With 2d Hill Collision
Post by: kamakazieturtle on 2008-Dec-13
Okay so in my side scroller i want to have my sprite collide with a hill and walk up it. I'm not coming up with anything that works :'(. My approach was if player collides with hill move player up, but it didn't seem to work so any suggestions will be appreciated.
Title: Re: Help With 2d Hill Collision
Post by: Schranz0r on 2008-Dec-14
post your code pls.
Title: Re: Help With 2d Hill Collision
Post by: kamakazieturtle on 2008-Dec-14
Problem is I don't use tiles in my game so my code is shitty not organized at all. I think I'm to try and learn tile engines. PeeJay has a good tutorial doesn't he?
Title: Re: Help With 2d Hill Collision
Post by: FutureCow on 2008-Dec-23
What you need to know is the player's X position, and then for that position what the height of the hill is. Obviously then Player Y = hill height X. Having a height listing for every x coordinate on your level though would take up a lot of space, not to mention being horrendously tedious to program. This is where using tiles makes your life so much easier.

Here's one way to implement it. (I appologise that this is all in pseudocode, I haven't been using GLBasic for very long so I'll end up writing something wrong if I try and write in real code. Let me know if you don't follow this or need it in real code and I'll do the conversion).

You start off with a tile number that represents the hill (eg. a gentle slope may be tile type 10)
You then have an height offset array for tile type 10 that says how high the tile is for each given offset into the tile (eg. when player is 1 pixel into the tile, height =0, when they're 2 pixels into the tile, height =1 etc)

It then becomes a simple if-then scenario to put the player in the right location, something similar to :

HeightOffsetArray[10]=0,1,1,2,2,3,3,4,4,5

Player's current X tile =  PlayerX / TileWidth
Player's current Y tile =  PlayerY / TileHeight
Players Current Tile Number = (Players Current Y Tile * number of tiles across Screen) + Players Current X Tile
Look up type of tile player is currently on based on the current tile number

if the player is currently on tile type 10 then
  Player X Offset = PlayerX mod tilesize   (ie. how far into the tile are they in the "X" direction)
  PlayerY= The Y location of the tile on the screen + HeightOffsetArray[Player X Offset]
end if

Hope that makes things a bit clearer.