Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Mouse hit detection?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Danshaikh




PostPosted: Mon Jun 13, 2011 10:12 pm   Post subject: Mouse hit detection?

What is it you are trying to achieve?
<Replace all the <> with your answers/code and remove the <>>
I'm trying to make a maze game where users have to put their mouse through a series of mazes.

What is the problem you are having?
I quite literally do not know how to do mouse hit detection. I want to figure out a way to end the program, or tell the player they lose when their mouse hits a certain colour. Like black for example.

Describe what you have tried to solve this problem
Nothing. I thought I could use coordinates but that is just way too much coding and quite unnecessary if I can use some sort of hitbox detection, or colour detection.
Sponsor
Sponsor
Sponsor
sponsor
2F_Zelda




PostPosted: Mon Jun 13, 2011 10:21 pm   Post subject: Re: Mouse hit detection?

Perhaps you should look into whatdotcolour.
Tony




PostPosted: Mon Jun 13, 2011 10:44 pm   Post subject: RE:Mouse hit detection?

whatdotcolour will sort of work if the user moves the mouse one pixel at a time. It would be trivial to jump over drawings simply by moving the pointer fast enough.

Coordinates are naturally much more precise, and it's not much code once the hitbox functions are in place (though one has to write those on their own).
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Danshaikh




PostPosted: Mon Jun 13, 2011 10:48 pm   Post subject: Re: Mouse hit detection?

Thanks a lot for the replies. I will look into whatdotcolour right now. Also, would whatdotcolour work if I'm importing the mazes from paint? Like, i'm making them in paint, then importing them into turing. So would it still apply?
Tony




PostPosted: Mon Jun 13, 2011 11:28 pm   Post subject: RE:Mouse hit detection?

In theory, yes. In practice, if parts of the image are rendered one shade off (99% grey, instead of 100% black) you might not be able to see the difference, but it's a different colour. This is common for compressed image formats such as jpeg.

What you have to realize is that whatdotcolour attempts to make your screen double for both displaying images _and_ being the coordinate system. My personal opinion is that it introduces more issues than it solves; it certainly limits what you can do.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
2F_Zelda




PostPosted: Tue Jun 14, 2011 1:26 am   Post subject: Re: Mouse hit detection?

Tony, if you were presented with a maze game and told that if you touched the walls you would lose, would your first thought be trying to move the mouse fast enough to get by without the program noticing? This is a slow paced game, and therefore the rate at which whatdotcolour checks is not an issue.
Insectoid




PostPosted: Tue Jun 14, 2011 3:22 am   Post subject: RE:Mouse hit detection?

Whatdotcolor can be used and yet still have a nice, colorful foreground. Set the screen mode to offscreenonly, then draw a black&white image of your map (use a bitmap if possible- compressed images like jpegs distort the color to off-white and off-black). Do all your whatdotcolor checks, then draw the 'real' map that doesn't look like crap, view.update, voila.

Solving 'skipping over textures' is can be as easy as drawing a line between where the curser is now, and where it was before, and seeing if it crosses any walls, though this strategy may have issues clipping corners. This can be done with whatdotcolor as well, however it would be a fair bit of work, and as such it's probably better to use a coordinate map or some such to trivialize it.

@Tony, can't a bitmap function, theoretically, as a coordinate map? I mean, you could create a map file in which each bit represents a different pixel, with a 0 for 'this is a wall' or a 1 for 'this is a path', which is essentially the same as using a BW bitmap where 1 byte is 1 pixel, and black is a wall and white is a path. The only real difference is the coding involved (less for the bitmap), the effort required to create the map (less for the bitmap), and filesize (bitmaps would be larger). Unless you meant to group pixels into larger tiles (say, 100^2 pixels = 10^2 tiles) that represent an entire section of pathway or wall and mathematically derive what section you're on.

I dunno, I guess I'm using this post to think out loud, mostly.

EDIT: I remember a game I played once, where map files were stored as pairs of coordinates, with each pair denoting a wall segment (similar to draw.rectangle). Given this data (as it's in the map file), you can easily generate an array of walls to iterate over to see if the mouse has collided with it. This seems by far the easiest solution, once you've created a map file. Bonus: you can create a map editor fairly quickly to speed up level development.
Tony




PostPosted: Tue Jun 14, 2011 10:38 am   Post subject: RE:Mouse hit detection?

@2F_Zelda that's certainly _one of_ my thoughts. By extension, if you were were presented with a login screen (computer account, website user, banking, etc.), would your first thought be to try to enter someone else's id? That answer shouldn't affect the requirement to also ask for a password Wink It's not a matter of "will most users do this", but the fact that you are allowing for buggy behaviour for when some users will behave a certain way (by accident or otherwise).

@Insectoid -- yes, but it's obviously faster to access an element of an array than to draw the entire map and sample a pixel colour. Another issue is that the common approach/mentality to using whatdotcolour is something along the lines of "I'm on a white square now, so <some assumption>". Those assumptions don't always hold (such as the wall jumping example above).
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: