forums

TNT Basic Forums > Programming Q&A
colliding with tiles in map
< Last Thread     Next Thread >
Author
Thread        Post A Reply

01-14-2004 01:11

Posted by:
fjerkon

Location:
Sweden

Find more posts by fjerkon

Is there a way to make my character collide with elements in my map?
like in pacman. say you make a map that represent the walls. how would i make pacman unable to walk thru them?
copy the map as a sprite?

01-14-2004 04:24

Posted by:
eric

Location:
earth

Click Here to Email eric   Find more posts by eric

Tites should stray tiles

Using sprite for boundaries is a bad idea, it will slow your amination. Instead, using your "pacman" location, test to see what tile he is on. a little math should do the treat.
Say your tile are 32*32, on your pacman is located at x=40 y=50. Then what kind of tile is he on?
40/32=1.25
50/32=1.5625
round it up to the nearest digit, so he is on tile x=1 y=1
Anyway, that kind of the idea. However, you need to test all for hedge of your sprite
if your sprite is say 20 by 20 and is located at X and Y
tile still 32 by 32
and your sprite coordination is based on his upper left corner

test tup
tileY=Y/32

test right
tileX=(X+20)/32
Around it up, test the tile at tileX and tileY

test bottom
tileY=(Y+20)/32

test left
tileX=X/32

Around it up, test the tile at tileX and tileY


sonthing like that
good luck

01-14-2004 21:29

Posted by:
kemalyun

Location:
Frankfurt/Germany

Find more posts by kemalyun

Yes, that's right, just check the contents of the map. Since the maze is made up of tiles, you can even make your PacMan always move by one full tile, i.e. when you press the key for moving right, you can make a little moan-program which moves him 32 pixels to the right while animating his mouth. Set a counter at the same time counting down from 32 to 0 in the main loop and allow another keypress only when the counter is 0, so he can only move back or another direction after he moved the full tile. In my "Azteks Revenge" I used the same principle for moving the hero, check that out.

The other solution, making the whole map out of sprites, is useful when you need a pixel-collision between sprite and background, let's say you make a Lunar Lander game. In that case, either make lots of small sprites or even one large one with size of the whole screen and check for collision. Didn't try that yet so don't know how much that slows down, but anyway the only possibility to check pixel-collision between sprite and background ...

01-15-2004 08:39

Posted by:
fjerkon

Location:
Sweden

Find more posts by fjerkon

Thanks!

but..

i'm a bit confused!
how do i know that my pacman is not on a 'wall'? should i make a command that gets info out of the map and checks if the specific tile is a wall-tile or am i mistaking?

btw.

just for testing. how do you copy a map viewport into a sprite with black as the alpha key? and will the alpha area then be handeled as non-wallish?

01-15-2004 16:15

Posted by:
denis caspar

Location:
montréal

Click Here to Email denis caspar   Find more posts by denis caspar

Map collision detection

I experienced a very simple way to detect a collision with map elements, a color detection. It works well with amall sprites.It is possible to use invisible colors.
col=pixel colour(sprite x(n),sprite y(n)).
sure map must be designed in that way.

01-16-2004 15:46

Posted by:
fjerkon

Location:
Sweden

Find more posts by fjerkon

Aaaah! that's very smart! it might just work in my game! thanks!

01-16-2004 16:54

Posted by:
eric

Location:
earth

Click Here to Email eric   Find more posts by eric

Pixel color test was a very popular way back in the 80's. Especially when the game were programmed in assembly. however, I would recommend you to develop some little sub-routine for tile collision for a generic sprite. It will be usefull in the furture.
Luck

01-18-2004 08:47

Posted by:
kemalyun

Location:
Frankfurt/Germany

Find more posts by kemalyun

In the solution I offered, at the moment the user can make in input (say which direction he wants to move) the PacMans position will always be at a certain tile, i.e. when tile-size is 32 pixels, his x and y position will be divideable by 32. Means you know he's at tile position x/32,y/32. Now check the map tile in the direction he wants to go to. If it's a wall tile, he can't. If it's empty or a "dot" or something else passable, set the moan for moving and animating him and set the counter for his "don't accept key input" time. The benefits are:

- Smooth animation
- Not much fuzzing around with pixels and therefore
- You can design the playfield using whatever colors you like, you can even create hidden passages (looks like walls but you can pass through).

Another nice trick for collision-detection when using a lot of different tiles is to set up an array containing a "meaning-code" for each tile-number, i.e. tile 1, 5, 10-20 aso. are "walls", put a "1" in the array at positions 1, 5, 10,11,12,13...20. Then, when checking a collision, check the array at the position of the map-tile-content. Benefit:

- You can use many different tiles for different functions but don't have to check them all in your code, one simple IF checks what the tile is (instead of IF a=1 or a=5 or (a>=10 and a<=20) then ....
- You can change the assignments of the tiles later without any problem, just change the content of the array, don't change any code at all.

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.