forums

TNT Basic Forums > Programming Q&A
Maps
< Last Thread     Next Thread >
Author
Thread        Post A Reply

02-13-2002 14:48

Posted by:
Chris

Find more posts by Chris

Perhaps I have missed something, but how do maps work? I've gone through the tutorials and help pretty thoroughly, but can't seem to find any in-depth information. For example, I'm interested in working on a side-scroller (like Mario, but hopefully more like Abe's Odyssey) but there's no (beginner) info on how to define tile sets or map areas. I mean, how does the map know where you can walk, and where you can't? Where do you define that? Again, it's possible I'm missing somethign really simple, but if not I'd appreciate some help. Thanks.

02-13-2002 15:11

Posted by:
ERaZer

Find more posts by ERaZer

Thats not really in TNT yet. You have to do it via code, with things like that you check where the player is in the map(like dividing by 32 and suchs). Check the Bang On! code. I'm not very good at tile collision and I haven't seen any good code yet...

02-13-2002 15:21

Posted by:
Chris

Find more posts by Chris

Well, maybe a tutorial on this subject would be a good idea? I've looked at the Bang-On code, but there's not a single comment in there... so it's a bit like chasing my tail. I was under the impression that this was more or less the point of maps?

02-14-2002 00:18

Posted by:
Mark Tully

Find more posts by Mark Tully

Map collision detection

Hi Chris,

The trouble with automatic collision detection for maps is that a lot of games use maps differently.

You have your side-on platform games where you can pass up through blocks as you jump, but not down through them.

You have you top-down maze games where you can't pass through the blocks in any direction.

You have your shoot-em-ups where the tiles are just the background and you can fly over them all.

You have your games where some sprites can pass through the tiles, but others cannot (eg baddies can, goodies cannot).

What I'm trying to say is that it's hard to come up with a one-size-fits-all solution to collision detection in maps.

I think it would be more beneficial and more flexible if instead examples were put on line showing how to do collision detection with maps.

The basic way of doing it is (for right movement here, same principle applies for all directions):

if right ' user is pressing right direction

' work out the rightmost position of our hero, that's his x position + his width + his speed (as that is where he'll be if we move him)
int newRightMostX = myHeroXPosition + widthOfHero + xSpeedOfHero

' now work out what tile his right most point will be on if we move him
int tileX = newRightMostX / map tile width

' see what tile it is
int tile = get map tile (tileX, myHeroYPosition, 0)

if tile = kPassibleTile
myHeroXPosition = myHeroXPosition + xSpeedOfHero
end if

end if

This works out what rightmost point of the hero is if he was moved right. Then it looks at what tile is at that point in the map and sees if it is equal to kPassibleTile (you'd set this to whatever is a passible tile in your game). If it's passible then the hero is moved.

Is this clear or should I expand on it some more?

Any example code anyone can contribute on this would be more than welcome. Bang On is another good example, if there are any specifics parts of Bang On that are proving difficult to understand please post I'm sure John or me will explain them!

Thanks and good luck!

Mark

02-14-2002 13:50

Posted by:
Chris

Find more posts by Chris

Thanks for the tips, they're appreciated. Actually, after posting the question yesterday and not hearing much (no fault of yours) I decide to do what I should have done in the first place and simply start mucking around. The code I came up with is:

while not mouse button
'this bit draws the collision sprites
Global int mapTile = 20, initMapTile = 20
int x, y
for x = 0 to 20
for y = 0 to 15
if map tile(x,y,0) = 1
sprite mapTile, x*32, y*32, 0
mapTile = mapTile + 1
end if
next y
next x

'this bit draws the character sprite
sprite 0, char_x, char_y, 1
Steering()

DrawMap()
draw frame
wend

Procedure DrawMap()
draw map section 0,0 to 20,15,0 to 0,0
end Proc

Which basically draws a sprite in any location where the character sprite can't move. So far, this appears to be a functional approach. Any thoughts on speed? I'm on a G4 450 with 384MB RAM, so it runs pretty good on my machine:)

I appreciate the various uses of map collision detection, my fisrt game was a side-scroller that didn't need it. But something to think about in the future might be a function of the map that will allow simple collision detection with a sprite. Just a thought.

By the way, nice interview on iDevGames. Thanks for the help, and any further input is appreciated.

02-15-2002 11:19

Posted by:
Mark Tully

Find more posts by Mark Tully

Collisions

I'm a bit unclear, are you using the sprite col function to detect collisions with your sprite-tiles?

This will probably be quite slow (sprite collisions aren't the fastest things to calculate), I think you'd be best working out 4 points, the left most, top most, bottom most and right most points for your sprite and then testing each to see if there's a tile there.

Your method might work better for your game though!

Good luck!

Mark

02-19-2002 00:53

Posted by:
Chris

Find more posts by Chris

Regarding that code...

Boy, I've just been thunking my head thinking I was doing something wrong here... :)

<snip>
if right ' user is pressing right direction

' work out the rightmost position of our hero, that's his x position + his width + his speed (as that is where he'll be if we move him)
int newRightMostX = myHeroXPosition + widthOfHero + xSpeedOfHero

' now work out what tile his right most point will be on if we move him
int tileX = newRightMostX / map tile width

' see what tile it is
int tile = get map tile (tileX, myHeroYPosition, 0)

if tile = kPassibleTile
myHeroXPosition = myHeroXPosition + xSpeedOfHero
end if

end if
</snip>

On the "int tile=get map tile(...)" line there needs to be a couple of changes.

Firstly, remove the "get" as the command is just "map tile" and secondly, the "myHeroPosition" value needs to be divided by the tile width as well. I kept getting -1 as a tile value and couldn't figure out why! Now I know it's because it was outside the map range. Can't believe it took me this long to see it. Anyway, for any others who are looking for simple map-based collision detection, this is a fine solution. Be happy to post working code when I get it finished if anyone is interested (written as a more-or-less catch all procedure). And huge thanks to Mark for the starting point.

02-19-2002 22:15

Posted by:
Mark Tully

Find more posts by Mark Tully

Oopps...

(blush)

Sorry about that!

Mark

02-20-2002 18:31

Posted by:
Chris

Find more posts by Chris

No blushing required. It was nice enough of you to post the demo code in the first place! Having had to work at it a bit, I'm more appreciative.

Now get back to work on the next new and exciting TNT Basic installment! I can hardly wait... (panting, slavering, etc.)

09-18-2002 23:32

Posted by:
Scoops

Click Here to Email Scoops   Find more posts by Scoops

I think I am being really thick here but if this is the way to go to detect sprite collisions against a specific map tile then what would you use map polygons for?? My initial plan was to layout all the 'safe' areas through which a sprite could move or perform other tasks using polygons, is this the wrong approach to use? Some clarification on this matter would be nice, but then it is late and at any other time of the day I would probably get it!!! :)

09-18-2002 23:35

Posted by:
Scoops

Click Here to Email Scoops   Find more posts by Scoops

I think I am being really thick here but if this is the way to go to detect sprite collisions against a specific map tile then what would you use map polygons for?? My initial plan was to layout all the 'safe' areas through which a sprite could move or perform other tasks using polygons, is this the wrong approach to use? Some clarification on this matter would be nice, but then it is late and at any other time of the day I would probably get it!!! :)

09-26-2002 22:35

Posted by:
Mark Tully

Find more posts by Mark Tully

Polygons

No that's exactly what map polygons are for, just check if the tile about to be moved to is in a 'walkable area' map polygon and you're set.

Thanks,

Mark

09-28-2002 19:16

Posted by:
Scoops

Find more posts by Scoops

I see! That clears it up thanks!

11-30-2002 03:15

Posted by:
jamie

Find more posts by jamie

polygon vs tile testing?

Does one of these methods have a speed advantage over the other? I'm trying to speed up my game and right now I'm using the 'tile testing' method to check for movable areas, would it possibly be faster to check using the polygon method?

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.