I'm developing a simple game engine for practise purpose, and I used codes like this to do collision detection (fake code):

bool PhysicalObject.isCollideWith(rect: PhysicalObject) =>
  x + width >= rect.x && rect.y <= y + height &&
    x <= rect.x + rect.width &&
    y <= rect.y + rect.height

But this treats all objects as a rectangle.
What I want is to detect collision when two objects are overlapping on pixel level (I used java.awt.Graphics to draw my game objects).

What's the best way of doing that? Can I create a two-dimension array that represent each pixel and store the pointer to the game object that the corresponding pixel belongs to?

  • 2
    \$\begingroup\$ I'm pretty sure that treats everything as a rectangle \$\endgroup\$ – Bálint Nov 28 '17 at 10:55
  • \$\begingroup\$ java and bool? java and pointers? Generally you'd check if two objects'rects collide and the check if any pixels collide. (Broadphase then Narrowphase col detection.) \$\endgroup\$ – Niels Nov 28 '17 at 11:47
  • 1
    \$\begingroup\$ S/he pointed out that the code is not supposed to be actual code, and "pointers" probably just means "references". \$\endgroup\$ – Thomas Nov 28 '17 at 11:48
  • \$\begingroup\$ Ah, I missed that, sorry \$\endgroup\$ – Niels Nov 28 '17 at 11:50
  • \$\begingroup\$ on topic: I think this is a bit depending on what kind of sprite we're talking about. pixel-perfect collision detection isn't easy to implement in my quick view. And I think that this is too advanced for something that should be an simple game, I personally recommend trying to make the collision rectangle at around the important part of the sprite, and smaller than the sprite so the collision hits the least whitespace as possible. \$\endgroup\$ – Steven Nov 29 '17 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.