Help With Tile Collisions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
koopatroopa
Prole
Posts: 6
Joined: Thu Dec 08, 2011 11:35 pm

Help With Tile Collisions

Post by koopatroopa »

Well, hello
I am new to Love and lua
but not completely new to programming as I feel I have a very basic grounding in java however none at all in game programming except for scratch.

What I am trying to do is simply create a plat former arena type game, but for now I'm just trying to get the mechanics down.
right now that entails moving a green block around a space and having it stop when it hits a wall.
Sounds Basic....
Probably is....

and way they way I decided to go about doing this was to create a 'list' or array' or 'table' I'm not actually sure which it is and in this represent my map of tiles that I created in a tile map editer.

Code: Select all

map = {5, 1, 1, 1, 1, 1, 1, 6,
           4, 0, 0, 0, 0, 0, 0, 2,
           8, 3, 3, 4, 0, 0, 0, 2,
           5, 1, 1, 5, 0, 0, 0, 2,
           4, 0, 0, 0, 0, 0, 0, 2,
           8, 3, 3, 3, 3, 3, 3, 7}
anyway, I know to block understands which tile it is in (starting at zero) because it says so in the top left corner of the block, however I can not get it to stop when it comes to another side.

by the way the way I tried to represent the block on my map with numbers was to say
-'1' means directly above
-'2' means on the left
-'3' means underneath
-'4' means on the left
-'5' means above and on left (corner block)
-'6' means above and on right (corner block)
-'7' means bellow and on right (corner block)
-'8 means bellow and on left (corner block)


Thanks
Attachments
game again.zip
(36.69 KiB) Downloaded 325 times
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: Help With Tile Collisions

Post by Kazagha »

There is a lot to get my head around;

Firstly do you need to change the following from true to false?

Code: Select all

    if love.keyboard.isDown("w") and colisionOnBottom == true then
        player1Y = player1Y - 1
    end
I am not quite sure why it's not picking up collisions in the right places, but I think because of the way you have calculated the 'position', based off the player1X and player1Y coordinates it's only getting a collision depending on where the top left hand side corner is. If the top left corner is just 1 pixel into row1, it won't detect a collision in row2 even though the rest of the shape is hanging out into row two.

Maybe you need to go for a system similar to below, in theory you should be able to parse your existing map (1 for solid, 0 for empty maybe) into creating the required rectangles. Not sure how to implement it off hand though.

http://silentmatt.com/rectangle-intersection/

-Kazagha
koopatroopa
Prole
Posts: 6
Joined: Thu Dec 08, 2011 11:35 pm

Re: Help With Tile Collisions

Post by koopatroopa »

Thanks Anyways
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: Help With Tile Collisions

Post by Kazagha »

I changed a few things around to help me understand how things work but the principal should work, have a look at the attached; I changed the map to 1s and 0s for ease of understanding. Also the collision is from the top left corner the up/down/left/right numbers have changed. The inelegant solution I implemented to getting stuck at the top pushes the player down when they collide with the top. Also for some reason when you collide on the left hand side it wont' stop colliding... not sure why though.

Additionally the following is the wrong way down. Pressing 'w' you are moving up and should be checking for a collision above, and pressing 's' to move down you should be checking for a collision below.

Code: Select all

    if love.keyboard.isDown("w") and colisionOnBottom == true then
        player1Y = player1Y - 1
    end
    
    if love.keyboard.isDown("s") and colisionOnTop == false then
        player1Y = player1Y + 1
    end
Your map array starts on one, so should your position so it gets a +1.

Code: Select all

    position = (((positionY) * 8) + positionX + 1)
-Kazagha
koopatroopa
Prole
Posts: 6
Joined: Thu Dec 08, 2011 11:35 pm

Re: Help With Tile Collisions

Post by koopatroopa »

Where is the attachment?
Thanks again
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: Help With Tile Collisions

Post by Kazagha »

Bugger. I won't have access to that computer again until tomorrow. I will try to post it again then.

-Kazagha
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: Help With Tile Collisions

Post by Kazagha »

Have a look at the attached.

I am no expert; speaking of which the left hand side collision seems to stick for some reason...

-Kazagha
Attachments
gameagainnew.love
(36.68 KiB) Downloaded 169 times
Post Reply

Who is online

Users browsing this forum: No registered users and 50 guests