Creating an event based environment

Grab your favourite IDE and tinker with the innards of game engines

Creating an event based environment

Postby Black_Stormy on Sun Dec 29, 2013 8:39 pm

So I am fucking around with playmycode.com and my own javascript canvas crap and I am trying to create objects which fire off functions when they are clicked. Right now I am checking the position and bounds of every single object on screen whenever the user clicks the mouse in order to see what they are most likely clicking. It doesn't always work (I have to implement a z-buffer sorting thingy to fix it up a bit) and I know it will get a lot more inefficient as I add more objects to be clicked.

It would be silly to ask for a specific solution, rather I want to know the theory behind creating event-based objects. I really don't like polling every single object on screen each time the user clicks the mouse. Can I assign a property to each pixel when it is drawing a specific object (like a big array, one entry for each pixel) and have the mouse click location just check that array? Should I divide the screen up into smaller areas so I don't have to check as much at once? How is this usually handled?
User avatar
Black_Stormy
May Contain Skills
May Contain Skills
 
Joined: Sun Nov 28, 2010 6:03 am
Location: Cairns, QLD, AUS

Re: Creating an event based environment

Postby zombie@computer on Sun Dec 29, 2013 10:07 pm

Black_Stormy wrote:So I am fucking around with playmycode.com and my own javascript canvas crap and I am trying to create objects which fire off functions when they are clicked. Right now I am checking the position and bounds of every single object on screen whenever the user clicks the mouse in order to see what they are most likely clicking. It doesn't always work (I have to implement a z-buffer sorting thingy to fix it up a bit) and I know it will get a lot more inefficient as I add more objects to be clicked.

It would be silly to ask for a specific solution, rather I want to know the theory behind creating event-based objects. I really don't like polling every single object on screen each time the user clicks the mouse. Can I assign a property to each pixel when it is drawing a specific object (like a big array, one entry for each pixel) and have the mouse click location just check that array? Should I divide the screen up into smaller areas so I don't have to check as much at once? How is this usually handled?

On the Net (ie html) all objects are hierarchical. Thus, when you click something, the body is polled. For every child in body, that child is polled. For every child under the click event, each of that childs' objects are polled. Etc. Since on average objects are pretty nicely dispersed over the screen one rarely polls a ginormous amount. Still, depending on the environment, you may want to do some pre-selection using binary space partitioning or similar methods. It depends on your needs really. Just remember to poll squares only. If you want to do additional (ie per pixel) do it AFTER you've checked the square boundingbox.
When you are up to your neck in shit, keep your head up high
User avatar
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Return to Programming

Who is online

Users browsing this forum: No registered users