Tile based game NPC's / Collision

Started by
9 comments, last by WhiteRaven 12 years, 4 months ago
Hey guys,

this is my first post here! I always use gamedev for help with the amount of information on here but unfortunately I have to post now because I just cant get my head around NPC's in a game I'm making.

I am making a simple tile scrolling game. I have my tile scrolling set up so that the player avatar never actually moves, he is always at screenWidth / 2 and 384 on the Y axis and when he presses Left or Right, the world scroll's to simulate the moving world.

I have a problem now. I am wanting to start adding NPC's/Collision but have no idea how to implement it.. obviously the NPC's will only want to be rendered when the tiles they patrol are on the screen, but I dont know how to control that.

I also want some information on collision, obviously I have had a go myself with bounding box but it just lags the whole game out when I check for collision (obviously doing it wrong).

Does anyone have some simple tutorials/books(I would buy) they can point me at? or some simple advice on how to go about this?

I am quite proficient in c++, I just have trouble getting my head around some of the concepts..

Thanks alot,

Matt.
Advertisement
The easier way to do scrolling is to implement a camera. Treat the player as an entity in a 3D world and make the camera follow him. You won't need to offset everything everywhere and will make your code much cleaner.

You can figure out if an NPC must be rendered by checking if its sprite is in the screen. A simple AABB check with its sprite and projected screen position will give you that. If you know your 32x32 sprite will be at (-150, 100), then you can skip it.

You will have to profile to see where your performance bottleneck is. Wild guess is you're checking collision with everything. You may want to add spatial partitioning to your world with things like quadtrees.
Developer for Novus Dawn : a [s]Flash[/s] Unity Isometric Tactical RPG - Forums - Facebook - DevLog
As checking for collision in your game is slowing down things , you are probably checking your character for collision with every tile in your map per frame which are even not visible on the screen.i.e if your level has 500 tiles you are checking 500 collisions for each Npc per frame resulting in 500 n collision checks which is not acceptable.you need to consider only those tiles which are visible on the screen and check collisions with them only .

As Tiblanc mentioned implementing a scrolling camera , you can use it to filter the objects . Create a camera bounding box and use it to filter tiles first that will be visible on the screen.So , assume now you know you have 50 tiles visible on the screen out of 500 and also you know NPCs currently visible from camera . So , you will only check 50 collisions per visible NPC per frame .



To further speed up things you can use spatial partitioning techniques like grids.
hey guys

thanks for the replies its clearing it up in my head!

do you guys know any articles / books I can get to help me work on it?
doing it all my head is not a good idea as I always look to much into things and usually do it a much harder way than actually required haha.

Matt
I don't know about books, but there is plenty of material on net on these subjects. Just implement the bare minimum in a clean way that allows expansion. That should give you room if you need a more complex solution and will actually get code produced. Chances are you will refactor it later on, so there's no point stressing over it.
Developer for Novus Dawn : a [s]Flash[/s] Unity Isometric Tactical RPG - Forums - Facebook - DevLog
I'm using Java slick and started working on a side scroller a couple days ago. I'm tryin to work out a nice OO collision system starting with tiles. As it stands, when the level loads, I immediately build a list of only the collision tiles (built with Tiled Map Editor). Then in my update method, I check if the player rectangle intersects any of the tiles. If so, handle it. It's nice in the sense that I don't have to keep polling the map for tile id and collision properties. Also, as for the camera, at least in slick you can use graphics.translate(x,y) and everything rendered after that point will be shifted automatically, so your code will be nice and short. That method also allows you to easily add some parallax scrolling in there. Best of luck!

The easier way to do scrolling is to implement a camera. Treat the player as an entity in a 3D world and make the camera follow him. You won't need to offset everything everywhere and will make your code much cleaner.


I don't want to derail this thread, but this is a point I need some clarification on. How do you do this in a multi-layered tile map with parallax scrolling?

[quote name='Tiblanc' timestamp='1321708944' post='4885580']
The easier way to do scrolling is to implement a camera. Treat the player as an entity in a 3D world and make the camera follow him. You won't need to offset everything everywhere and will make your code much cleaner.


I don't want to derail this thread, but this is a point I need some clarification on. How do you do this in a multi-layered tile map with parallax scrolling?
[/quote]

Fake it I guess. Have your layers offset by a factor of the actual offset. So if your first layer is a 100% offset, the background layer could be 50%. If you move the camera 400 pixels to the right, the background layer is rendered 200 pixels to the left.
Developer for Novus Dawn : a [s]Flash[/s] Unity Isometric Tactical RPG - Forums - Facebook - DevLog
For collision detection look into quadtrees.

http://lab.polygonal...-demonstration/
http://en.wikipedia.org/wiki/Quadtree

For collision detection look into quadtrees.

http://lab.polygonal...-demonstration/
http://en.wikipedia.org/wiki/Quadtree


Quadtrees for a tile-based 2D map is massive overkill.

There is an easy solution. The easiest is, when an object is about to move from one tile to another, check if the tile they are moving onto would cause a collision (ie, the tile contains a player, wall, rock, etc.). That should not be very processor intensive.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement