Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Pixel-Collisions is it possible?

I was curious if RMVX or RMXP supported Pixel-Collisions with RGSS2 or just RGSS? I'm good with C# and Game Makers scripting language so I know what Pixel-collision does and can do it fine within those to environments.

But I however have not been able to do it with RMVX or XP. Is there a specific way you have to check X/Y? Yes I know this sounds noobish but if I get helped out with this I'd focus on RGSS and actually learn it knowing that the type of game I have planned can be done with this program and save me a ton of time!

Here's an example of collision checking that I plan on doing. With VX I use 8X8 tile Movement, with XP I use Fotz's pixel-movement script.
Yes, it's an image. "X" signify's the pixel-collision well basically where you'd stand in VX as it's based on a 8X8 grid which allows you get 2-3 spots on any facing direction.
fjzqzd.png


If anyone knows about pixel collision checking with X/Y in Ruby/RGSS that'd be awesome! Not really asking for a script here.
 
Well you can do pixel collision check like this. You can simply use the idea that if an object hits the place which is transparent (alpha transparency of that pixel is 0) means that it wont collide into there.

Code:
 

def check_collision(bitmap1, xc, yc)

 

  # Grab the width and height of a bitmap

  wid, hei = bitmap1.width, bitmap1.height

  

  # Initalize vars

  pixel, x, y, collision = nil, 0, 0, false

  

  # Collision checking

  while y < hei

     pixel = bitmap1.get_pixel(x,y)

     collision = true if pixel.alpha != 0 and x == xc and y == yc

     x += 1

     if x > wid

      x = 0

      y += 1

     end

   end

   return collision

end

 
 
Alternatively, you could use a hitbox based approach, like most Mario games take. With that, you basically give everything its own virtual box that can be used to detect collision, and if the player's box hits the box of something collidable, you stop the player from moving. It's a bit more complex than that in most cases (for example, Mario games will do a quick calculation to backtrack movement if a collision takes you into a hitbox, to prevent that little jitter where you hit the ground hard and suddenly pop back out of it, by simply placing you at the point in the path that would leave you on the surface)

The reason I recommend hitboxes instead of full pixel-based collisions is rather simple. Hitboxes take far less memory, because you're just storing four points relative to any given sprite, instead of doing a full check pixel by pixel on a lot of images. At the same time, it also helps prevent little point of no return glitches from happening. For example, in Golden Sun: The Lost Age, which has full pixel based movement, including pixel-based collision, I managed to get permanently stuck in a few places. The most memorable instance was when I took my flying ship to the northern tundra, and somehow managed to get it permanently stuck in a small area between some forests and some mountains. The pixel-based collision in that game is also the reason you can glitch yourself back onto the continents that were in the first game, where you become permanently stuck if you save. (And the sad part is, there's nothing there, although you can still run into monsters)
 
Well it all boils down to what you want and where you want it. Pixel collisions are great for places where you want to make collision more realistic, when you don't need it use the hitbox method Glitch explained here. :)

As for getting stuck in the places that is going to be the issue for every game which was not throughly tested, in order to test that pixel collision you're gonna have to go into every possible place a character can go in the game and no matter how hard you try, you're not a machine, you can't check it all. :)
 
Drago del Fato":mt4bygx0 said:
Well you can do pixel collision check like this. You can simply use the idea that if an object hits the place which is transparent (alpha transparency of that pixel is 0) means that it wont collide into there.

Code:
 

def check_collision(bitmap1, xc, yc)

 

  # Grab the width and height of a bitmap

  wid, hei = bitmap1.width, bitmap1.height

  

  # Initalize vars

  pixel, x, y, collision = nil, 0, 0, false

  

  # Collision checking

  while y < hei

     pixel = bitmap1.get_pixel(x,y)

     collision = true if pixel.alpha != 0 and x == xc and y == yc

     x += 1

     if x > wid

      x = 0

      y += 1

     end

   end

   return collision

end

 

Ok is that RGSS or RGSS/2?

How would this be used in events(I'm coming strictly from javascript/game maker language)

If I got this right, this will check any object that's on the map correct? if not, how would you check if one enemy was attacking any pixel of you?
 
Ok is that RGSS or RGSS/2?
Well this one is universal.

How would this be used in events(I'm coming strictly from javascript/game maker language)
Events as in events in RPG Maker, or you're talking strictly about RGSS?

If I got this right, this will check any object that's on the map correct? if not, how would you check if one enemy was attacking any pixel of you?

This is just a function for checking, it probably can be done in a way that it will be faster than this. It checks the bitmap (sprite, picture, charset, battler whatever image you throw at it) for the x, y coordinates you set in xc and yc arguments... If those coordinates are in a pixel which is not transparent (or if you would, hitting that bitmap) then it will return true as collision...
You can use this function in conjunction with if clause in RGSS.

For example:

[rgss] 
  # Test sprite collision coordinates
  # I am considering that the above function I wrote is defined somewhere
 
  # Make a new sprite
  spritex = Sprite.new
  # Add a bitmap to the sprite, I'm considering you have a sprite you want to be checked
  # located in Graphics\Prictures folder
  spritex.bitmap = Bitmap.new('./Graphics/Pictures/sprite')
 
 # Check if a place defined by X and Y coordinates is hitting a sprite
 # For this example I will set x = 3 and y = 4
 x, y = 3, 4
 
 if check_collision(spritex.bitmap, x, y)
  print "You have collision."
 else
  print "No collision detected."
 end
 
 
[/rgss]
 
Oh I get it now.

So the sprite bitmap should be the same size as the actual sprite(saves time I guess)

of course I'd set x/y to $game_map_events or $game_player as my abs is more ruby/evented than just a large script(there is a few scripts being used)

so in events I'd just do something like this?
Code:
 

if condition branch script&#058;  check_collision(spritex.bitmap,$game_map_events[event_id].x,$game_map_events[event_id].y)

  show_message: Hey idiot, I'm colliding with you!

end

 else

  show_message: Hey idiot, why am I not colliding with you?

end

 

Since I am using events I'd probably use a few extra variables like sprite_hero, and sprite_event.

But this seems like it should work just fine, curious why you selected x=3, y=4? wouldn't it be easier to check the hero's or events X/Y?
 
But this seems like it should work just fine, curious why you selected x=3, y=4? wouldn't it be easier to check the hero's or events X/Y?
I'm pretty sure this is just an example.

By the way, get_pixel is very slow. I recommend that you use get_pixel only once to fill an Array or a Table with the Data (true or false = passable or impassable).
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top