Jump to content

Limit car to roads


bartk
 Share

Recommended Posts

Good afternoon,

 

I'm trying to make a small driving game, and I want to limit my player's car so that he can't move off of the roads and on the sidewalks. Might anyone know how to best handle it? I'd rather not add lots of invisible "fences" to collide with the car if I can help it.

 

Cheers.

Link to comment
Share on other sites

Are you using a tilemap to define your "road"?  If so, when I'm using tilemaps I typically make an invisible "collision" layer, full of invisible tiles that are set to collide on all sides.  Alternatively, you can individually define collision on the tiles that are actively drawn.

If you require collision that's not a bunch of blocks (i.e. tilemap or arcade physics), like polygon or rounded edges, you'll need to use P2 physics.  You can probably define the outer walls of the track as one massive polygon and the inner walls as another, smaller polygon.  Then check collision on these two polygons and the cars.  Mind you, checking collision on large polygons with P2 is pretty slow so limit the number of polygons as much as possible.

Link to comment
Share on other sites

I have little to no experience with Phaser, or even with gamedesign, so I've just made a few dozen "road" objects with a sprite that's tiled across them. I realize it's possible to just build blocks all around the roads, but I was hoping there'd be a more painless way :P

Here's an example:

kL6Bo.png

Link to comment
Share on other sites

I'm afraid I don't really know what you mean by Tiled. Basically I've used a 50x150 sprite to build all my horizontal lengths of road, and a 150x50 to build the vertical lengths. One of these lengths may be:

roads = game.add.group();...road1 = game.add.tileSprite(0, 400, 150, 800, 'road');roads.add(road1);

If there's no other way than to create invisible collision "fences", so be it.

Link to comment
Share on other sites

I recommend taking a look at RPG maker tutorial or similar if you're new at Tile mechanics

 

 

it basically a few layers of graphic that drawn together with 32x32 tiles, and typically a layer of invisible tiles will be the collision that keep the character within the world

Link to comment
Share on other sites

By the way, what I'm trying to do isn't to keep the player in the world. That was easily done by world bounds as you said. I'm trying to keep the player limited to the roads, so that he can't drive over the sidewalks or into npcs.

Link to comment
Share on other sites

What you usually do is draw a few tiles (say 3 : sidewalk, road and house tiles). You import these in Tiled and easily build your map, placing the tiles wherever you like it. Then, let Tiled export your map as a JSON file.

 

Import this JSON into Phaser, as well as  the image with your 3 tiles, and Phaser will understand how to rebuild the map using your tiles. 

 

Then just point to Phaser that the sidewalks and houses tiles are collideable, and the trick is done. Your car won't be able to drive over them.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...