0

I have 2 circular buttons spinning around in a circle and i need the program to be able to recognize that i have clicked within the circles, one of the buttons is for start one is for quiting.

Here are the declared relevant variables at the top of my code

double angleStart=1.5*pi;
double angleQuit=0.5*pi;
int radius=120;
int centerX=300;
int centerY=160;
float startPosX = (float) (centerX + Math.sin(angleStart)*radius);
float startPosY = (float) (centerY + Math.cos(angleStart)*radius);
float quitPosX = (float) (centerX + Math.sin(angleQuit)*radius);
float quitPosY = (float) (centerY + Math.cos(angleQuit)*radius);

the images/buttons are drawn at startPosX/Y and quitPosX/Y

Here is the code that makes the buttons spin

    int posX = Mouse.getX();
    int posY = Mouse.getY();
    double constant=0.002*pi;

    startPosX = (float) (centerX + Math.sin(angleStart)*radius);
    startPosY = (float) (centerY + Math.cos(angleStart)*radius);
    quitPosX = (float) (centerX + Math.sin(angleQuit)*radius);
    quitPosY = (float) (centerY + Math.cos(angleQuit)*radius);

    angleStart+=constant;
    angleQuit+=constant;
    if (angleStart>=2*pi){
        angleStart-=2*pi;
    }
    if (angleQuit>=2*pi){
        angleQuit-=2*pi;
    }

Also as you can see PosX and PosY are the mouse coords

Now finally here is the code that decides if the buttons have been clicked or not

//start button
    float startXDist=posX-(startPosX+50);
    float startYDist=posY-(startPosY+50);
    float startDist=(float) Math.sqrt((startXDist*startXDist)+(startYDist*startYDist));
    if(startDist<=50){
        if(Mouse.isButtonDown(0)){
            sbg.enterState(1);
        }
    }

    //quit button
    float quitXDist=posX-(quitPosX+50);
    float quitYDist=posY-(quitPosY+50);
    float quitDist=(float) Math.sqrt((quitXDist*quitXDist)+(quitYDist*quitYDist));
    if(quitDist<=50){
        if(Mouse.isButtonDown(0)){
            System.exit(0);
        }
    }

When run nothing (normally) happens when i click the buttons but if i spam my mouse randomly all over the screen sometimes is hits an area which is perceived by the program as me clicking the button, thanks for the help in advance

extra note the +50's are in there because the button has a radius of 50px

im afraid i cant post images yet but heres a link to a screenshot http://s2.postimg.org/h9ykqpd5l/Capture6.png

also the little bean is a temporary graphic in place until i make one my self

Shardj
  • 1,800
  • 2
  • 17
  • 43
  • For better help sooner, post an [SSCCE](http://sscce.org/). See also [this SSCCE](http://stackoverflow.com/a/14575043/418556) for tips. – Andrew Thompson Jul 12 '13 at 14:05
  • will do, gimi 5 minutes – Shardj Jul 12 '13 at 14:08
  • *"gimi 5 minutes"* That i barely enough time to read the document. Please don't 'guess' what it means. Also, please use the correct spelling for words like 'you', 'your' & 'please'. This makes it easier for people to understand and help. – Andrew Thompson Jul 12 '13 at 14:11
  • don't be an ass, also i believe I've used correct grammar throughout my post so why say that I'm spelling words like 'you' and 'your' wrong – Shardj Jul 12 '13 at 14:17

2 Answers2

0

I think this is because you're only checking if the mouse button is down. Not if the mouse button has been pressed and released. I'd guess if you dragged the mouse over your button it would trigger. If you drag your mouse near when you're wildly spamming it will trigger.

Try spamming clicks more carefully.

Also check that your x and y axis run in the same direction for your mouse as they do your rendering space.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
  • your right about it not needing to be pressed and released but it still doesn't work when i click the button's, i cant seem to find any pattern between where i click and whether it does something or not – Shardj Jul 12 '13 at 14:07
  • Does your mouse coordinate space match up with your rendering space? Does X run left to right, and y bottom to top for both mouse coords and rendering coords for instance? – William Morrison Jul 12 '13 at 14:08
  • ive tried having it without the +50's, no luck, they are there because i need the difference between the center of the circle and the mouse not between where its drawn from and the mouse which would be the top left of the image – Shardj Jul 12 '13 at 14:11
  • Did you check to see the coordinate space matches? – William Morrison Jul 12 '13 at 14:13
  • ahh point, must of skipped over that part of your comment or you edited it, ill check up on that – Shardj Jul 12 '13 at 14:19
  • Ok thanks man :) mouse coords are from bottom left while image is from top left, life saver – Shardj Jul 12 '13 at 14:23
  • Awesome, glad to have helped. accept the answer to close this question please! – William Morrison Jul 12 '13 at 14:23
  • 1
    Problem solved thanks to William Morrison, i simply added to the code where the y difference was calculated for start and quit so it looked like this float quitYDist=(480-posY)-(quitPosY+50); The problem was that mouse coords are taken from the bottom left while image coords are taken from top left and since the game container has a height of 480px that value was put in to get the value of y for the mouse from the top rather than the bottom – Shardj Jul 12 '13 at 14:29
  • ok thanks william, yeah im new and where abouts is this green checkmark, im probably being thick here – Shardj Jul 12 '13 at 14:29
  • Its actually just a gray outline to the left of my answer. Just click on it. – William Morrison Jul 12 '13 at 14:31
0

well the circle equation is r^2 = mX^2 + mY^2 so in your case assuming radius of your circle is r, mouse clicks is mouseX, mouseY and circle center is cX and cY then

if (Math.pow(mouseX-cX,2)+Math.pow(mouseY-cY,2)<Math.pow(r,2))
  // mouse is within that circle do related codign here
Onur A.
  • 3,007
  • 3
  • 22
  • 37
  • thats exactly what ive done except i square rooted the left side of the equation and put < r instead of < r^2 – Shardj Jul 12 '13 at 14:20
  • it should be work out, be careful with negative values, make sure you take absolute value of them with Math.abs() – Onur A. Jul 12 '13 at 14:30