Creating a 2D racing game, how to detect going off the track?

31 views
Skip to first unread message

James Grimwood

unread,
Jan 3, 2016, 9:26:42 AM1/3/16
to HaxeFlixel
I'm making a top-down 2D racing game and need to know how whether the player is trying to drive off the edges of the track. The track is oval and all I need to figure out is whether the player has gone from "on the track" to "not on the track".

Currently the track is a giant FlxSprite that covers the whole screen, and the player is a small FlxSprite.

So far I can think of two ways to do it

1. Have a collision bitmap and every frame test whether the point under the player is the "collision colour"
2. Somehow define the track as a polygon and do a point-in-polygon test

If option 1 is the best way, how do I actually do it? FlxSprites aren't really designed for having their bitmap data poked at, are they?

Dlean Jeans

unread,
Jan 4, 2016, 4:02:12 AM1/4/16
to haxef...@googlegroups.com
Actually, FlxSprite has a property called framePixels which is a BitmapData so you can call getPixel32(x, y) to get the color:
var color:Int = track.getPixel32(player.x, player.y);


Reply all
Reply to author
Forward
0 new messages