tiktuk Posted December 20, 2016 Share Posted December 20, 2016 Hi everybody ! I'm making a simple multiplayer snake game. It's all pretty basic, I'm drawing all snakes directly to one big bitmap, filling the whole game. I'm drawing the snakes simply by filling a rectangle at the 'head' position continually. And then I'm trying to detect collisions with other snakes by checking a pixel in front of the snake head for alpha values greater than zero. It doesn't work though. It's like the bitmap doesn't reflect the drawn pixels. And I am calling bitmap.update() for every game update. Yes, performance is also pretty bad… The game is playable at http://tiktuk.net/snakes/, controlled with the keys QW, AS, ZX, ER etc. Source is (also) here. Any ideas? Link to comment Share on other sites More sharing options...
mattstyles Posted December 21, 2016 Share Posted December 21, 2016 Why aren't you doing bounding checks against snake segments? Whenever a snake turns it creates a new segment, which will be a rectangle, you store all of these in a list somewhere and then when a snake moves you make a bounding check for the head against every snake segment. There are probably numerous more efficient ways of doing it but this is pretty simple and should be easily fast enough on crappy hardware even with hundreds of snake segments. If your snakes move on a predefined grid (as traditional snake games do) then the lookup is far simpler. Link to comment Share on other sites More sharing options...
tiktuk Posted December 21, 2016 Author Share Posted December 21, 2016 4 hours ago, mattstyles said: Why aren't you doing bounding checks against snake segments? Whenever a snake turns it creates a new segment, which will be a rectangle, you store all of these in a list somewhere and then when a snake moves you make a bounding check for the head against every snake segment. There are probably numerous more efficient ways of doing it but this is pretty simple and should be easily fast enough on crappy hardware even with hundreds of snake segments. If your snakes move on a predefined grid (as traditional snake games do) then the lookup is far simpler. Yes, that is exactly what I'm gonna do if this can't work . I'm sure the performance will be much better that way as well. It's just strange that my current approach isn't functioning. Thanks for your reply, btw. Link to comment Share on other sites More sharing options...
rich Posted December 21, 2016 Share Posted December 21, 2016 BitmapData.getPixelRGB (and all getPixel operations) require integers, but you're passing in floats because your pos.x + prope.x values, more often than not, create floats when combined. So you're saying "give me pixel 345.345 x 869.493", which of course isn't a valid canvas coordinate. Floor your x/y first, then you'll get an accurate pixel color object back again. var x = Math.floor(this.pos.x + this.prope.x); var y = Math.floor(this.pos.x + this.prope.y); var color = bmp.getPixelRGB(x, y); Lypzis 1 Link to comment Share on other sites More sharing options...
tiktuk Posted December 21, 2016 Author Share Posted December 21, 2016 2 hours ago, rich said: BitmapData.getPixelRGB (and all getPixel operations) require integers, but you're passing in floats because your pos.x + prope.x values, more often than not, create floats when combined. So you're saying "give me pixel 345.345 x 869.493", which of course isn't a valid canvas coordinate. Thank you so much, that was it! It makes so much sense and now I can continue ☺️ . Link to comment Share on other sites More sharing options...
rich Posted December 21, 2016 Share Posted December 21, 2016 Yeah it made me update the jsdocs for BitmapData today as a result Link to comment Share on other sites More sharing options...
Recommended Posts