Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] Collision against a tile array

Author
Message
Steveotronic
9
Years of Service
User Offline
Joined: 8th Feb 2015
Location: Leicestershire
Posted: 4th Jan 2019 12:54
Hello Everyone,

Ive been toying on and off with a top down 2d game as well as a 3d game, and I've pulled up my old 2d tile array test I did last year, but in doing so realised the reason that I stopped was because I couldn't figure out how to register a collision with a tile in an array, sprite to sprite I get. But sprite to array tile im not sure I understand. I have read through the examples on the help site, but I am just confused. I have also searched the forums but couldn't see an AppGameKit thread on this.

If I am mistaken and there is a thread I apologise and please point me in the right direction.

I include my test project below.

Any explanation of why it is not working/how it should work or just general collision against an array info would be very greatly appreciated.

Kind Regards,

Steve

Attachments

Login to view attachments

The author of this post has marked a post as an answer.

Go to answer

Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 4th Jan 2019 14:34 Edited at: 4th Jan 2019 14:38
This post has been marked by the post author as the answer.
Hi Steve

Your collision isnt working as its checking against one single sprite ID (spr4) for collisions when there are a number of sprites that are collidable.

One way to fix this is to store all the Sprite ID's of the collidable tiles then check for a collision against them. This is done in the code below and can simply replace your main file and works with collision. If a collision occurs then the player is moved back to where they were before the collision as to not get stuck on the object they collided against.



I would also add the following points too.

1) You can apply physics properties to the wall sprites and use physics to move the character then collision is then handled for you by AGK. Its still possible to do linear movement but the collision is a bit more realistic and easier to handle.

2) The above method is slow as it checks for collision against every single sprite rather than just checking those which are closest to the character - it was easy to implement though. If you store all the sprite ID's as an array then you could just then check the closest 9 sprites to the player and so the collision check is a lot faster rather then checking all the wall sprites against the player sprite

3) Someone (i think it was garbenjamin) did a platform game template which is worth checking out as this has the code for the collision testing built it. Even though it was a tile based platformer it is still valid for top down adventure. Also, there are numerous tile importers to import from the Tiled software straight into AppGameKit and so using a txt fie isnt needed and will allow you to design your maps in a specific free bit of software designed for it.

4) In your original code your creating a lot of sprites but not storing the ID's anywhere and so deleting them at the end of a level would be difficult without calling deleteallsprites(). Its probably worth having a map array variable to store all the Sprite id's in.
Steveotronic
9
Years of Service
User Offline
Joined: 8th Feb 2015
Location: Leicestershire
Posted: 4th Jan 2019 17:32
@Bengismo thanks for the really quick reply.

so let me just see if this is correct.

In the code you posted all you have done is create another array called cols, then when spr4 is created you put the id of that tile into cols.

Then you loop through and check for a collision between the player sprite and the tile id of any spr4's in the cols array, then obviously do the movement code back to old x/old y. Is that correct?

just a few replies to the points you made.

1) I didn't think I would need physics as I just want to create a small single screen game. I have also tried them in my last game and didn't get on with them too well. but I will have another look into it. Thank you.

2) do you think it would be too slow for a single screen game the size of the code I posted? I will attempt to implement the checking of the 9 tiles around the player that sounds like a good idea, again thanks.

3) Yeah I think I came across something from GarBenjamin about parallax scrolling that I used to help me lay out the tiles, I will check again for a collision example. Yeah I saw the tiled threads but honestly thought it would be simpler and better for me to "roll my own" to learn how things work. ideally I would eventually like to make my own tile editor as an application.

4) So here would I do as above and insert into an array just after creation of the sprites used in the tile-map? Would this aid me later on for my tile-map editor?

thanks again for your very detailed response and helpful points. I really really appreciate it.

Kind regards,

Steve

Login to post a reply

Server time is: 2024-04-20 15:26:54
Your offset time is: 2024-04-20 15:26:54