Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How to handle tracks in a 2D racing game?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 drowsyn   Members   -  Reputation: 142

Like
0Likes
Like

Posted 16 June 2011 - 02:26 AM

I've been tinkering with a 2D car game in the style of Slicks 'n Slide and GeneRally, and am not sure how to handle tracks, especially the collisions. My implementation at the moment is that I draw the track in Photoshop, and that's used as the actual graphic in the game. From that picture, I create a map that features just a few colors, which the game then reads, and assigns each pixel a state from an enumeration. These states are tarmac, grass, and wall. During gameplay, on every update, I check which pixels my car is colliding with, and react accordingly.

Now, this way of doing it works, but it feels like a complicated way of doing something fairly simple. I played around with two other options:
1. Create a pure tile-based level structure, where I just plop down tiles that have the corners on them. The problem with this approach is that I still can't figure out a good way of doing collisions. I'll still have to check each pixel on the tile, so really, this is just another way of doing exactly what I'm currently doing.
2. Draw the track as previously, but turn everything into objects. The road is made up of sections of "road objects", grass is "grass objects", and walls are "wall objects". Then check for collisions between the objects and the car, and react accordingly. The main issue with this is that it feels kind of clunky to me. The great thing with this approach is that I could easily add physics to everything, and thus for example have the walls move if someone hits them.

Does anyone have ideas? TIA.


Sponsor:

#2 Andy474   Members   -  Reputation: 690

Like
1Likes
Like

Posted 16 June 2011 - 04:08 AM

I was toying with this idea the other day, this is the idea i had

1/ Create a Map in Photoshop / paint or whatever, this is the artwork so don't worry about collisions when making this.

2/ Create another Texture, same size etc. which is colored Green/Red (Collision Map). This map is used to detect collisions, every time you update, grab a Rectangle of pixels from this Map (where the players car is) and check for any Non-green pixels.

To do Grass / Road / Wall you could use:

Green: Road
Yellow: Grass (car move speed *0.7 or whatever)
Red: Wall

I'm not sure how good this solution is in-terms of lag or efficiency. I used a technique similar when i did a 2D top-down shooter a while back, worked well, no lag.

What are you using C++, XNA java?


[EDIT] Found this, it is in java but basically describes what im talking about: Java Collision Maps

Edited by Andy474, 16 June 2011 - 04:09 AM.


#3 drowsyn   Members   -  Reputation: 142

Like
0Likes
Like

Posted 16 June 2011 - 04:14 AM

I'm doing this in XNA. What you're describing sounds very similar to what I've been doing, just streamlined a little bit. I'll go ahead with this, and refine it as needed. It's definitely a workable solution.


#4 Haps   Members   -  Reputation: 1315

Like
1Likes
Like

Posted 16 June 2011 - 09:24 AM

Here's an XNA tutorial containing what Andy474 suggested, if you think it might help.

Tutorial

#5 bluwind   Members   -  Reputation: 181

Like
1Likes
Like

Posted 17 June 2011 - 12:07 AM

Hiya, i did a very similar thing in VB6 a few years back. What i did was create a 2 dimensional array like "map(800,600)" then when the screen loads i get the pixel colour of each pixel and load it into the array following a quick case statement, i would use a slightly off black colour as collisional walls so if th pixel is off black the number "10" would be loaded into the array at that pixels coords, grass could be entered in as "5", tarmac as "0" and the actual players position would be entered as "1" so when a player moves you check hisnext coords on the map array - empty space would be "1+0" (allow to run normal), if you hit a wall "1+10 = 11" (collision detected, dont allow move) .




The benefit of doing it this way is that anything that could possibly cause you lag is done on loading, you can also easily change the map at runtime too without any slowdown. i know i didnt explain that very well and i think i made it sound alot more confusing than it actually is.. I would write you a little example in XNA but my laptop wont let me run it.. something about pixel shaders not being able to handle it.


Rob

#6 drowsyn   Members   -  Reputation: 142

Like
0Likes
Like

Posted 17 June 2011 - 01:25 AM

Thanks for all the help guys. I put together a version of the game yesterday, and it works beautifully.

So here's another thought... Is it possible to do bridges that go over the track while utilizing this kind of simple 2D geometry? I thought about making multiple layers, and then somehow testing the player's physics against the layer that he's on at any moment, but that seems like a really bad idea, and I don't even quite know how to make it work. I know Slicks 'N Slide had bridges, and it looked very 2D to me, so obviously there is a way. Obviously it would be easy in 3D, but I don't know if it's worth the hassle to go 3D with a game like this.


#7 Andy474   Members   -  Reputation: 690

Like
1Likes
Like

Posted 17 June 2011 - 03:41 AM

So here's another thought... Is it possible to do bridges that go over the track while utilizing this kind of simple 2D geometry?



hmm prehaps you just have to be clever with the artwork? no need to look at 3D i can think of 2 solutions (i dont know if they'll work)

1/ Just be clever with the artwork on both art and collision map

2/ Use layers have a black or White strip on the Collision map which change the layer when the player triggers over them?
[Or checkpoints?]

e.g. instead of having the whole track as one Image file, use multiple files for between checkpoints.

So between the start flag and first checkpoint is one texture, this will prevent the bridge "overlapping" so the player can cut corners
maybe just have the checkpoint maps as collision maps?
- hope that makes sense :)





#8 bluwind   Members   -  Reputation: 181

Like
1Likes
Like

Posted 17 June 2011 - 06:13 AM

Well to do a bridge u just need to add a height variable to ur car. Starts at height 1 and when he passes a hidden marker it's height changes to 2. When the height is 2 the car can drive over the bridge and when height is 1 he can drive under it, u just need to make the Walls on the bridge collision detection based on the height of the car and also change the cars z-order so he appears under or above the bridge.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS