1
\$\begingroup\$

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?

\$\endgroup\$
6
  • 2
    \$\begingroup\$ I'm pretty sure that treats everything as a rectangle \$\endgroup\$ Commented Nov 28, 2017 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\$ Commented Nov 28, 2017 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\$ Commented Nov 28, 2017 at 11:48
  • \$\begingroup\$ Ah, I missed that, sorry \$\endgroup\$ Commented Nov 28, 2017 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\$ Commented Nov 29, 2017 at 9:58

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.