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.

DarkBASIC Discussion / tile based collision the 2nd

Author
Message
Nabz_32x
11
Years of Service
User Offline
Joined: 25th Oct 2008
Location:
Posted: 11th Aug 2011 21:17
Hi all, again the problem with tile based collision...
First of I want to have that tile based collision, not a pixel perfect one.

I just want to look at the array (a simple width * height array for example) on the position the player would be positioned in if there was no collision routine. By looking on the players movement direction he had at the collision time (when he was within collision zone) I generated borders, for example if he comes to a tile and is heading right the leftmost border is given to the player process and players x position is set to that point, when he falls the upper border of the tile and so on.

Can you guys tell me if this method could lead to success, as I having trouble implementing all colideable sides. I have 2 functions one for y and one for x borders. Just want to know if it is worth debugging any further or if the method could work with some work on implementation

Phaelax
DBPro Master
17
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Aug 2011 22:55
Divide the character's position by the tile size and you'll know what tile it currently occupies. Then it's relatively easy to determine the border positions of the surrounding tiles.

Ashingda 27
12
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 12th Aug 2011 01:47
Show some codes of your progress, it's easier to speak codes on the forums. You'll get a more accurate response to your problem rather than just talking methods or theory. By the way the method you're coding works very well.

Latch
14
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 12th Aug 2011 02:11
If I may be so bold as to elaborate a little further on what Phaelax suggested, let's say that you have a screen size that is 640x480. For the sake of argument, we'll say that this is the entire map for a level.

Now, we don't want to look at every single pixel (like you said), we want to look at tiles.

Make an array that stores all of the tiles and assign values to those tiles that represent borders, or doors, or mountains, or whatever you want. The tile size (how many pixels tall and wide) will help determine the size of the array.

Let's say an individual tile size is 32x32. On a 640x480 screen, we will divide the screen size, by the tile X size and the tile Y size. This will give us our array's dimensions:

640/32 = 20 tiles in the X direction
480/32 = 15 tiles in the y direction

We now know our array is 20x15 tiles big:



Back to identifying what is on a tile; we assign individual tile locations a number that represents some feature on our map. For example:



Then assign the values to the array where these features exist. So if a tree is in the upper left hand corner of the screen at tile position 1,1 then:



Apply this method to your whole array representing the features on the map.

Now back to what Phaelax was telling you. When your character is moving around the map, you can find out exactly what tile they are on by dividing their screen x and y positions by the tile size (32X32).

So if your character is at 357 X and 310 Y then they are on tile:

357/32 = 11
310/32 = 9

Now, since the actual screen coordinates start at 0,0 then you have to ADD 1 to the values to keep it lined up with your array

tilex=(357/32)+1
tiley=(310/32)+1

Then you just plug those values into your array and you know exactly what feature you are over or are trying to move to:



There are variations of this method, but I'm guessing Phaelax was hinting at something like this.

Enjoy your day.
Nabz_32x
11
Years of Service
User Offline
Joined: 25th Oct 2008
Location:
Posted: 12th Aug 2011 21:50 Edited at: 12th Aug 2011 23:21
okay here are my Collision functions:


So I read out this data (plst(11 & 12)) to determine if collision had happened and if player x or y should be altered, I check for a left or right collision (x position) first, then alter the players x position and after that I check for y collision.

The points where I check the x collision are in the y mid of the player plain and on the rightmost or leftmost border of the plain.
The y collision I check on the downmost y border of the player plain (at the shoes obviously), and above the players head, so x and y collision won´t argue themselves to death.

Edit: I made some great progress, and the collision works now on all sides if the player object moves slower than a speed of 25 pixels (x or y) per frame.

So I removed the ugly player control code from this thread

Phaelax
DBPro Master
17
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th Aug 2011 22:45 Edited at: 12th Aug 2011 22:45


Attachments

Login to view attachments
Nabz_32x
11
Years of Service
User Offline
Joined: 25th Oct 2008
Location:
Posted: 12th Aug 2011 23:14 Edited at: 12th Aug 2011 23:48
thx Phaelax for reminding me that those who can hit the right bracket type on their keyboards are in a clear advantage

I figured out that the implmentation of my collision functions into the players control algorythm where kinda sloppy done, as I found a misspelled variable at an attempt to clean up the code a bit.
The variable was misspelled where it should prohibit further movement into x direction when a x collision appeared, that caused the player to reset its x and y position to 0 as the player could stay in an collision area for consecutive frames.

Login to post a reply

Server time is: 2020-08-12 02:40:34
Your offset time is: 2020-08-12 02:40:34