I have a scene with some sprite on it (added), the gamer has to select one of them and take the next action.

What is the simplest way to select one of the sprites accurately?

I am looking for a method that recieves a click coordinate and returns one of the sprites on the scene.

Since the sprites cant be well fitted in a bounding box I dont like to use a minimum bounding box approximation.

I was thinking of loading the sprites into a matrix and creating a scene matrix that each member belongs to a specified sprite, but it costs a lot( each pixel must be transformed using an affine transformation and it must be recalculated each time one of the sprites move)

Can someone show me a better way with a lower cost?

P.S I am using Swift and Apple's SpriteKit There is a code for minimum bounding box but I am looking for an accurate method.

Inaccurate method if some interested is something like this :

     for touch in touches
            var location : CGPoint = (touch as! UITouch).locationInNode(self)
            for asprite in self.viewIso.children
                if (asprite.containsPoint(location))
                    println("Selected : \(asprite.name as String)")

Thanks Iman

  • \$\begingroup\$ It's great that you found a solution to your problem, but in the future if you're looking for a platform/language-specific solution it will be helpful for the rest of us if you indicate the platform/language in your question and/or tags. Also, you should accept your answer since you found your own solution. \$\endgroup\$
    – Sandalfoot
    Jul 23, 2015 at 18:42
  • \$\begingroup\$ Actually, I didn't find my own answer, I was wrong so I deleted the answer, and thank you for suggestion to indicate the language and platform, I did, But I am looking for an algorithm. and about accepting my own answer, I should say that It will be possible after at least 24 hours. Why minus one???!?! \$\endgroup\$
    – Iman Nia
    Jul 23, 2015 at 19:08
  • \$\begingroup\$ The -1 was for you saying "this is the answer" and then posting code, when there was no way anyone would have known that you wanted a solution in that specific language. Your rewrite is a lot clearer so I'll reverse it. \$\endgroup\$
    – Sandalfoot
    Jul 23, 2015 at 19:15
  • \$\begingroup\$ Thanks, beside the plus and minus, How do you do this? I want your answer, I am creating an isometric map kind of game and I need a good click and pick algorithm. \$\endgroup\$
    – Iman Nia
    Jul 23, 2015 at 19:22
  • \$\begingroup\$ Mick Waites gave you the correct answer, so I up voted him. \$\endgroup\$
    – Sandalfoot
    Jul 23, 2015 at 19:23

1 Answer 1


If I remember correctly, I iterate through all sprites and use their inverse affine matrix to convert the click coordinate into local space for the sprite. I then use a trivial bounding box check to determine if the local coord can be ruled out quickly as a hit for that sprite.

If the bounding box check passes, I then perform an alpha test on the pixel of the sprite's texture. If it passes the required threshold, it is classed as a hit.

  • \$\begingroup\$ The problem is, the sprites are not very neat and the MBB check will not be very good. Also the second filter which actually select the right sprite against the candidates is very hard to implement. \$\endgroup\$
    – Iman Nia
    Jul 23, 2015 at 19:25
  • \$\begingroup\$ @Iman The BB check is simply to perform a computationally trivial check to see if the sprite can be ignored - the remaining processing can be expensive on some systems (possibly requiring "unlocking" a texture to read its data). It also prevents you reading memory outside the bounds of the texture data if the point to check is outside the bounding box. Edit: I see you have updated your question. I have no experience with Apple's SpriteKit, but I suggest you consider seeing if my method can be applied to that library if you don't find a nice easy sprite->getIsHit(x, y) method. \$\endgroup\$ Jul 24, 2015 at 6:41
  • \$\begingroup\$ Thanks Mike, Thats what I was thinking, to have two filter, first finds the candidates and secound does the precise camputation to find out which sprites exactly contains the hit point, but as you mentioned too, the second filter however is very hard to implement and its expense doesnt matter really since it is going to be executed a few times. Thanks for your help. Every strategic game must do such a computation, how they manage this, do they all use a BB? \$\endgroup\$
    – Iman Nia
    Jul 24, 2015 at 14:12
  • \$\begingroup\$ BBs are used a lot in both 2D and 3D engines exactly for the purpose of reducing the amount of complicated calculations are performed. I'm no expert on 3D engines, but in the engine I work with every day I've seen BBs (axis aligned) used in the 3D raycasting code. \$\endgroup\$ Jul 24, 2015 at 14:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .