1
\$\begingroup\$
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
    if (i < 360) {
        setX((float) (groundState.getX() + groundState.getRadius() * Math.cos(Clock.delta() * i)));
        setY((float) (groundState.getY() + groundState.getRadius() * Math.sin(Clock.delta() * i)));
        i++;
    } else {
        i = 0;
    }
}

i = angle

setX/setY is setting the players' x & y

The player will move around groundState's circumference.

The problem is that 'i' never equals the angle. So it causes this weird graphical bug that makes it appear there are 2 players opposite each other on the circumference of groundState.

How to make 'i' actually equal the angle?

\$\endgroup\$
1
\$\begingroup\$

Math.cos uses radians, not degrees. You want to go from 0 to 2* Math.PI

You could do that like this:

if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
    if (i < 360) {
        //Convert to radians by multiplying by Math.PI*2f/360f
        float theta = Math.PI * 2f * (float) i / 360f;
        setX((float) (groundState.getX() + groundState.getRadius() * Math.cos(Clock.delta() * theta)));
        setY((float) (groundState.getY() + groundState.getRadius() * Math.sin(Clock.delta() * theta)));
        i++;
    } else {
        i = 0;
    }
}

Even better, just use a float for i and move setX and setY outside of the if:

if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
    float theta = Math.PI * 2f * (float) i / 360f;
    setX((float) (groundState.getX() + groundState.getRadius() * Math.cos(Clock.delta() * theta)));
    setY((float) (groundState.getY() + groundState.getRadius() * Math.sin(Clock.delta() * theta)));

    if (i < Math.PI * 2.0) {
        //Convert to radians by multiplying by Math.PI*2f/360f
        i+= Math.PI / 180.0;
    } else {
        i = 0;
    }
}

Lastly, you can make the if statement if bit smoother:

    i+= Math.PI / 180.0;
    if(i > Math.PI * 2.0) 
    {
        i -= Math.PI * 2.0;
    }
\$\endgroup\$
5
  • \$\begingroup\$ Okay so I've done this, but the movement is very jittery. And at certain points along the circumference it snaps back to a previous point. \$\endgroup\$ – Alex Gwyn Apr 9 '16 at 6:40
  • \$\begingroup\$ I've made some edits :) \$\endgroup\$ – Jeremy Apr 9 '16 at 6:53
  • \$\begingroup\$ Thank you very much :) The only problem is that the movement is now incredibly slow. Also when 'i' get's to 2 * PI, it jumps back to it's original position. \$\endgroup\$ – Alex Gwyn Apr 9 '16 at 7:08
  • 2
    \$\begingroup\$ You have been given the basic outline of the solution. It's now up to you to tweak the solution to fit your needs. Did you adopt the last piece of code in? \$\endgroup\$ – Tyyppi_77 Apr 9 '16 at 7:27
  • \$\begingroup\$ I did adopt the last piece of code in, and I've been tweaking it to try and get it to work. But it appears that nothing works. Though I will keep trying :) \$\endgroup\$ – Alex Gwyn Apr 9 '16 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.