Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Selection  (Read 1687 times)

0 Members and 1 Guest are viewing this topic.

MickeyKnox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Selection
« on: June 07, 2011, 12:33:24 pm »
I want to be able to select an object, when the user clicks on it.
But how can i identify the object, the user clicked on?

I guess i could simply test the mouse point against all my objects,
but that would be rather inefficient, i guess.

Does SFML offer some infrastructure to do that?
If not, what would be a good approach?

I'm using RenderWindow and i just have a purely 2D Environment,
in case that makes things easier.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Selection
« Reply #1 on: June 07, 2011, 01:37:45 pm »
You could divide the screen into multiple squares, so that every object knows in which square(s) it currently is. Then, you can find out which square was clicked and only check the objects inside this square.

But this is only worth the trouble if you have really many objects. 100 is not many. A linear search is good enough (or even faster) for few objects.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MickeyKnox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Selection
« Reply #2 on: June 07, 2011, 02:06:58 pm »
ok nexus, thanks.
I don't have that many objects, so i'll do a linear search.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Selection
« Reply #3 on: June 07, 2011, 02:39:40 pm »
Quote from: "Nexus"

You could divide the screen into multiple squares, so that every object knows in which square(s) it currently is. Then, you can find out which square was clicked and only check the objects inside this square.

But this is only worth the trouble if you have really many objects. 100 is not many. A linear search is good enough (or even faster) for few objects.


Simplification of Quadtree? :P

Want to add that it's only worth it if they are evenly spread out(Worse case scenario, all objects are in one square).

Anyway it all depends on how the game/application looks like. If you have a game world that extends outside the window/screen then you have to use some other neat tricks. But if the world is limited to the screen you can do some other stuff.

Things like this often boils down to how the game operates.

If you have a game world that extends outside the screen I recommend a Quadtree (or if it's a small world a grid) and then you just make a AABB vs AABB test against the tree's nodes to see if they are inside the view(what's currently viewing)
Developer and Maker of rbSFML and Programmer at Paradox Development Studio