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

Author Topic: Sprite hit area, how to?  (Read 5602 times)

0 Members and 1 Guest are viewing this topic.

WajdaW

  • Newbie
  • *
  • Posts: 4
    • View Profile
Sprite hit area, how to?
« on: October 02, 2010, 06:45:49 pm »
If I have for example two sprites that are no squares, more like two neighbour contries with all that wavy border, hot to make exactly hit area for those sprites?

something like this:

[/img]

Quillraven

  • Newbie
  • *
  • Posts: 11
    • View Profile
Sprite hit area, how to?
« Reply #1 on: October 02, 2010, 07:56:34 pm »
there are different approaches to this problem:

an easy way would be to make your own "collision rectangles" for each frame of your sprite and then check for all that rectangles, if they collide with any of the other sprite's collision rectangles.

in your graphic f.e. you can make one big rect for the whole right side of the yellow sprite, one medium rect for the left "halfisle" and another small rect for the tiny little halfisle on the left bottom corner.



another way would be to check the non-transparent pixels of the sprites and if there are overlapping pixels, there is a collision. to optimize this second approach you can save the indizes of the transparent pixels to avoid checking pixel per pixel.

if you have f.e. a 8x8 pixel sprite and pixel 1 to 27 are transparent, you store this information in your spriteclass or whatever. if you now check for a pixelcollision you start at pixel 1. you now know that the next 26 pixels are transparent so you can directly jump to pixel 28, etc..



but to keep it simple i would recommend the first approach.

Canadadry

  • Hero Member
  • *****
  • Posts: 1081
    • View Profile
Sprite hit area, how to?
« Reply #2 on: October 02, 2010, 08:05:33 pm »
First of all, you check if the two rectanlge are hitting each other. Then  you check the color of each pixel (of both sprite). There is colission, if you could found one pixel no transparent on both sprite (at the same position).  Am i clear ?

If i remember correctly there a code in the french wiki.

PeterWelzien

  • Newbie
  • *
  • Posts: 38
    • View Profile
Sprite hit area, how to?
« Reply #3 on: October 03, 2010, 12:04:57 pm »
/Peter Welzien

WajdaW

  • Newbie
  • *
  • Posts: 4
    • View Profile
Sprite hit area, how to?
« Reply #4 on: October 03, 2010, 10:21:06 pm »
Thanks for reply, but I think that I asked maybe little wrong... :)
I need to determine when my mouse button click on this border which country it really clicked, because if I click on spot where owerlaping rectangles are, i will be noticed that mouse is over both countries...

I hope I made myself little bit clearer. :)

Those sprites will be static, they won't move or colide, I just need to click on them and get one that is exactly clicked.

Canadadry

  • Hero Member
  • *****
  • Posts: 1081
    • View Profile
Sprite hit area, how to?
« Reply #5 on: October 03, 2010, 11:01:34 pm »
The sprite wil overlap but not the the picture in it ? And you wanna not which one is it ?

WajdaW

  • Newbie
  • *
  • Posts: 4
    • View Profile
Sprite hit area, how to?
« Reply #6 on: October 04, 2010, 10:04:10 am »
Yes, that rectangles in picture I attached are sprites and they overlap, and those counties are images and they dont overlap, so I want to know how to after mouse click I can determine which country is clicked onto if click happened in sprite overlap area.
I thought to make all neighboud contries different color and ask which color is beneath mouse and then determine a country on that color.
I thought if is there some property to set costum hit area for countries, like there is a hit area in flash...

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Sprite hit area, how to?
« Reply #7 on: October 04, 2010, 12:28:58 pm »
If I were you, I will use a simple "Is my point is in my polygon ?" algorithm. The country beeing the polygon :
Code: [Select]
////////////////////////////////////////////////////////////
/// Check if a point is inside the polygon's area
////////////////////////////////////////////////////////////
template <typename T>
bool sfPoly<T>::Contains(T X, T Y) const
{
    for (unsigned int i=0;i<Coords.size()-2;i+=2)
    {
        if (((Y-Coords[i+1])*(Coords[i+2]-Coords[i]) - (X-Coords[i])*(Coords[i+3]-Coords[i+1]))<0)
        {
            return (false);
        }
    }
    //The last test is special
    unsigned int j = Coords.size();
    if (((Y-Coords[j-1])*(Coords[0]-Coords[j-2]) - (X-Coords[j-2])*(Coords[1]-Coords[j-1]))<0)
    {
        return (false);
    }
    return (true);
}

My function is using a simple Template object (int or float, like Rect of the SFML) and two coordinats of a point to see if this point is inside the polygon.
Mindiell
----

 

anything