Jump to content
  • Advertisement
dream rz

2D how to implement picking for irregular texture, just need scheme.

Recommended Posts

A texture rectangle, texture type is RGBA.
the alpha channel like this: (0=0,x=255)
[0][0][0][0][0][0][0]
[0][0][x][x][0][0][0]
[0][x][x][x][0][0][0]
[0][x][x][x][x][x][0]
[0][0][0][x][x][x][0]
[0][0][0][x][x][0][0]
[0][0][0][0][0][0][0]

I want a function to picking. now I have a very clumsy solutions:

//system Init.............
//..........
//pixel buffer to texture....... (texture is handle, a number.cannot get pixel matrix.)
//..........
BYTE pixel_alpha[7][7];//save alpha channel matrix
//..........
texture.draw(30,30);//draw the picture in the window    x=30 , y=30
//..........
if(picking(GetMouseX,GetMouseY))MessageBox("click!"); // picking functio
//..........

/*picking function*/
bool picking(int x,int y)
{
    int mouse_to_image_x, mouse_to_image_y;//get mouse position in the image
    mouse_to_image_x = 30 - x;
    mouse_to_image_y = 30 - y;
    if((mouse_to_image_x < 0 && mouse_to_image_x > 7) && (mouse_to_image_y < 0 && mouse_to_image_y > 7))return false;//mouse is not in the image
    for(int i = 0; i < 7 * 7; i++)
    {
        if(pixel_alpha[mouse_to_image_x][mouse_to_image_y] == 255)return true;//in the image and alpha channel is 255
    }
    return false;
}
//=============================================================================================
but......
1.an excessive amount of memory in alpha channel matrix.
2.i think efficiency is not high.
so, how to do? 
not is ray picking, What is this technology called?

Share this post


Link to post
Share on other sites
Advertisement
25 minutes ago, Scouting Ninja said:

This is confusing. What software, API or what ever are you using and what is your goal?

Any 2D graphics engine. SDL2, openframeworks(use OpenGL) ............
textures is rectangular and loading a PNG image as a texture.
this PNG is a button, maybe it's various shapes.
the mouse must click the effective alpha channel. alpha >= 100 maybe.

Share this post


Link to post
Share on other sites
17 hours ago, dream rz said:

textures is rectangular and loading a PNG image as a texture.

See this is the confusing part. OpenGL has functions for loading textures it also has functions for finding pixels in a texture.

https://open.gl/textures

https://stackoverflow.com/questions/8000921/how-to-get-color-from-the-pixel-opengl

17 hours ago, dream rz said:

this PNG is a button, maybe it's various shapes.

Do you mean that you want to extract sprites from a texture. Things like UI buttons and such:

https://stackoverflow.com/questions/9609423/applying-part-of-a-texture-sprite-sheet-texture-map-to-a-point-sprite-in-ios

To get it as various shapes like a circle you first need to use vector math to plot the points, so you get it like a UV map.

 

These topics are all very basic and can be found in many OpenGL tutorials and with simple quick searches.

Did any of this help or are you looking for something else?

Share this post


Link to post
Share on other sites
42 minutes ago, Scouting Ninja said:

See this is the confusing part. OpenGL has functions for loading textures it also has functions for finding pixels in a texture.

https://open.gl/textures

https://stackoverflow.com/questions/8000921/how-to-get-color-from-the-pixel-opengl

Do you mean that you want to extract sprites from a texture. Things like UI buttons and such:

https://stackoverflow.com/questions/9609423/applying-part-of-a-texture-sprite-sheet-texture-map-to-a-point-sprite-in-ios

To get it as various shapes like a circle you first need to use vector math to plot the points, so you get it like a UV map.

 

These topics are all very basic and can be found in many OpenGL tutorials and with simple quick searches.

Did any of this help or are you looking for something else?

Yes, as you said.
that's exactly what I mean.
but.......but.........
I want a more efficient means.
applications run faster.................
only taking small area in internal memory.......................

Because in the game, FPS is very high(60 or ????). work to every frame.

so, there may be a better solution to the problem??

Share this post


Link to post
Share on other sites
8 hours ago, dream rz said:

but.......but.........
I want a more efficient means.
applications run faster.................
only taking small area in internal memory.......................

If there was a more efficient way we would already be using it. Maybe there is but we haven't found it yet, but then asking on forums isn't going to make it appear.

 

Although there isn't a more efficient way, there are ways to improve memory; it's just not more efficient because it's going to need more calculations done; slowing down the game.

For example:

Re-using textures is more memory efficient. So changing the way the texture looks when rendered, like changing the color, saves memory; instead of loading a same texture with a new color.

Using more optimized textures. For example PNG images is often 24bit but you could use a RGB 8bit and a 1bit black and white image. But a 1bit can only store 0 and 1. So transparent or not. it can't store half transparent values.

 

There are also many other ways but these are more memory-efficient not efficient, because they will need more work or some other thing done to save memory.

The thing is that all our data types are build from 0 and 1. There really isn't much more that can be done to make it efficient. You would need to run thousands of tests to find more efficient way of using textures.

Share this post


Link to post
Share on other sites

Let's say you have a texture which is an image of a button or something - and now you want 15 buttons. And another texture - a kitten - so you have 20 "Entities".

One way to do all this is to have a struct or Entity class which has it's own bottom left x,y and top right x, y to form a bounding box, yes, a good old fashioned rectangle. Not buttons, not kittens but rectangles.

Now you would have an array or list of all such Entities, each with their own x, y locations.

So you can go through your master list which has all the Entities - if the mouse x, y is not even in the bounding box of an Entity then it can quickly and easily be eliminated. There is no point in drilling down into the details of each kitten pixel if you aren't even in the same ball park as the bound box rectangle. Right?

For these "broad phase" collision tests you can use other simple shapes as well:- like circles, spheres, cylinders or whatever. The concept is the same. 

Back in the day I was an avid arcade game player and could've sworn they were doing pixel perfect collision detection. Years later I heard the actual author say it was just a bounding box or circle test. Had me fooled.

Yes you can drill down and "sample" the RGBA of a texture but I'd get the bigger picture as described above sorted out first. 

And if you have many thousand objects then you would benefit from partitioning space. A simple Grid with Cells or Quadtree for 2D, Octree for 3D will help you reduce the master list, you'd have a sub-list which only contains Entities in that neighbourhood ...

(At any rate I would not create my own data structure containing just the alpha channel bits. That's all on the CPU with regular system memory, you'd want more OpenGL and hardware acceleration help).

 

Share this post


Link to post
Share on other sites
16 hours ago, Scouting Ninja said:

If there was a more efficient way we would already be using it. Maybe there is but we haven't found it yet, but then asking on forums isn't going to make it appear.

 

Although there isn't a more efficient way, there are ways to improve memory; it's just not more efficient because it's going to need more calculations done; slowing down the game.

For example:

Re-using textures is more memory efficient. So changing the way the texture looks when rendered, like changing the color, saves memory; instead of loading a same texture with a new color.

Using more optimized textures. For example PNG images is often 24bit but you could use a RGB 8bit and a 1bit black and white image. But a 1bit can only store 0 and 1. So transparent or not. it can't store half transparent values.

 

There are also many other ways but these are more memory-efficient not efficient, because they will need more work or some other thing done to save memory.

The thing is that all our data types are build from 0 and 1. There really isn't much more that can be done to make it efficient. You would need to run thousands of tests to find more efficient way of using textures.

There appears to be no better way.
On the basis of the theory of click detection, looking for other ways to do this.
Anyway, thank you.

Share this post


Link to post
Share on other sites
1 hour ago, AlexKay said:

Let's say you have a texture which is an image of a button or something - and now you want 15 buttons. And another texture - a kitten - so you have 20 "Entities".

One way to do all this is to have a struct or Entity class which has it's own bottom left x,y and top right x, y to form a bounding box, yes, a good old fashioned rectangle. Not buttons, not kittens but rectangles.

Now you would have an array or list of all such Entities, each with their own x, y locations.

So you can go through your master list which has all the Entities - if the mouse x, y is not even in the bounding box of an Entity then it can quickly and easily be eliminated. There is no point in drilling down into the details of each kitten pixel if you aren't even in the same ball park as the bound box rectangle. Right?

For these "broad phase" collision tests you can use other simple shapes as well:- like circles, spheres, cylinders or whatever. The concept is the same. 

Back in the day I was an avid arcade game player and could've sworn they were doing pixel perfect collision detection. Years later I heard the actual author say it was just a bounding box or circle test. Had me fooled.

Yes you can drill down and "sample" the RGBA of a texture but I'd get the bigger picture as described above sorted out first. 

And if you have many thousand objects then you would benefit from partitioning space. A simple Grid with Cells or Quadtree for 2D, Octree for 3D will help you reduce the master list, you'd have a sub-list which only contains Entities in that neighbourhood ...

(At any rate I would not create my own data structure containing just the alpha channel bits. That's all on the CPU with regular system memory, you'd want more OpenGL and hardware acceleration help).

 

Of course, I know this way. high efficiency, imprecise.

int circle_x , circle_y , circle_radius;
int mouse_x , mouse_y;

int test_x = mouse_x - circle_x;//get distance difference
int test_y = mouse_y - circle_y;

int i = sqrt(test_x * test_x + circle_y * circle_y);//get square root

if(i <= circle_radius);//mouse pointer in a circle

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!