Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Circle Mousebox Button

Author
Message
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 4th Feb 2013 22:16 Edited at: 4th Feb 2013 22:16
I have a neat and tidy little function for dealing with square /rectangle buttons that the mouse may click on.



Its very simple but effective, however i was wondering how to adapt this for a circle button.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Feb 2013 22:56


"You're not going crazy. You're going sane in a crazy world!" ~Tick
zeroSlave
14
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 4th Feb 2013 23:04 Edited at: 4th Feb 2013 23:06
Use a distance formula to check if the distance between the mouse and the center of the circle is less than the radius.



If I were you, I'd declare a variable to store the mouse's positions and clicks. If I remember correctly, you can only get the mousex(), mousey(), mousemovez(), mouseclick(), etc once per sync. I think it returns 0 after it's initial use and before the next sync. Don't remember and too lazy to test it.

Edit: beaten

Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 4th Feb 2013 23:19 Edited at: 4th Feb 2013 23:19
Quote: "sqrt((mouseX() - x)^2 + (MouseY() - y)^2)"


Did you know that (x)^0.5 is about 5 times faster than sqrt(x)?

As Phaelax demonstrates nicely, you don't need a square root anyway with distance checks.

TheComet

http://blankflankstudios.tumblr.com/
"ZIP files are such a retarded format!" - Phaelax
zeroSlave
14
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 4th Feb 2013 23:39
Yep, getting the square root puts a beating on the CPU. You can square both sides of the statement, canceling out the square root to speed up the calculation. Was just showing off the standard distance formula.

Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th Feb 2013 03:27
Quote: "Did you know that (x)^0.5 is about 5 times faster than sqrt(x)?"

I was not aware of that.

Quote: " If I remember correctly, you can only get the mousex(), mousey(), mousemovez(), mouseclick(), etc once per sync."

I don't know about mouseclick because I too store it in a variable, but I know mouseX/mouseY will constantly return a proper value multiple times without problems.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 5th Feb 2013 08:03 Edited at: 5th Feb 2013 08:05
If you want to get a few more units of FPS, only do this and other input command checks 5 or 6 times per second. This will be essential if you have lots of buttons being checked



or check the mouse click condition before performing the distance calculation. Short circuit:



Quote: " If I remember correctly, you can only get the mousex(), mousey(), mousemovez(), mouseclick(), etc once per sync."


Quote: "If I remember correctly, you can only get the mousex(), mousey(), mousemovez(), mouseclick(), etc once per sync"


It is only mouse move commands that you can only use once per loop.

But storing the values in variables is good. MouseClick() calls are 3 times slower than referencing a variable

Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 5th Feb 2013 09:16
Excellent stuff guys.

Also i did not know the square root thing. So instead of using SQRT i should just basically use the proper equation then? I.e 3^3 or something is that right.
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 5th Feb 2013 11:56
That code works beautifully Phaelax.
As demonstrated here.

I just wonder if you could break it down a little to tell me what this line is doing.
if (mousex()-centerX)^2 + (mousey()-centerY)^2 <= radius*radius
zeroSlave
14
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 5th Feb 2013 16:51 Edited at: 5th Feb 2013 18:15
Hello there. It's a modified 2D Distance Formula used to find the distance between two points: x1,y1 and x2,y2. In your case, it's: mousex(), mousey() and CircleCenterX, CircleCenterY.

distance# = sqrt((x2 - x1)^2 + (y2 - y1)^2)
distance# is how far away the points are from each other.
If distance# is less than the radius of the circle, then the point is inside the circle.

The reason it's modified:
sqrt((x2 - x1)^2 + (y2 - y1)^2) = distance#
is the same as: (^2 both sides of the statement)
sqrt((x2 - x1)^2 + (y2 - y1)^2)^2 = distance#^2
is the same as: (^2 cancels out the √ [/b])
[b](x2 - x1)^2 + (y2 - y1)^2) = distance#^2

is the same as: (distance#^2 = distance#*distance#
(x2 - x1)^2 + (y2 - y1)^2) = distance#*distance#

is because SQRT() is costly on the CPU in DBP. If it's going to be used frequently, use ^0.5 or the multiplication/division property of equality to get rid of it.

Here is a good visual representation of the Distance Formula.

Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th Feb 2013 17:44
Distance formula is Pythagorean Theorem. You know, the a² + b² = c²

As others have pointed out, square root can be a costly function. Because you're only comparing the distance to another value, you don't actually need the true distance and therefore you can leave the resolved squared itself and simply square the distance of what you're checking it against.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 5th Feb 2013 20:31
Thanks folks. I understand it a little better, im sure more usage of it over time will also help me understand even more.

Login to post a reply

Server time is: 2024-04-19 22:24:21
Your offset time is: 2024-04-19 22:24:21