1
\$\begingroup\$

I should say first, that I have the rotation down. Its just that I want my square to rotate exactly 180 degrees. Currently, it will rotate but it will rotate but by less each jump. So after several jumps it will be moving on one of its side. There's not much code to show but I'll show you how I'm rotating:

if (jumped) 
        { 
            roation += 0.1f;
            playerVel.Y += gravity;
        } 

I haven't initialized rotation to anything. I've also tried using some Math functions but I just get the same result.

\$\endgroup\$
  • \$\begingroup\$ I got this: angle += (float)MathHelper.ToRadians(5.8f); But after about 7 jumps the square starts landing on an angle again \$\endgroup\$ – Dan Scott Aug 15 '13 at 13:29
0
\$\begingroup\$

You need to keep track of the total rotation for each rotation. A few other variables will help you out as well. The reason you're likely getting some angle when landing is because your 5.8 degrees per update isn't exactly right, or your timestep isn't a fixed update. You can manage that with something like the following:

float degreesPerSecond = 180f;
float jumpRotation = 0f;

//inside update

if(jumped) {
    float rotationThisFrame = degreesPerSecond * deltaTimeInSeconds;
    jumpRotation += rotationThisFrame;
    if(jumpRotation > 180) { //check if we're done
        rotationThisFrame -= 180 - jumpRotation; //remove the extra rotation
        jumpRotation = 0; //reset total for next time
        jumped = false;
    }
    roation += rotationThisFrame;
}

That should make an entire jump rotation last 1 second. (untested).

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This code still makes the the square land at random points (which I think might have been due to jumpRotaion not having enough time to reach 180), but going back to the first code I posted roation += 0.1f; And after the first jump, rotation is at 3.099999. So I just did if (angle >= 3.099999) Thanks for (trying to? It sounds a bit condesending) help though \$\endgroup\$ – Dan Scott Aug 15 '13 at 14:38
  • \$\begingroup\$ Sure Dan. It sounds like your method may be a little more unpredictable (sort of hacky). You may want to try combining the above with your code. Also, make sure you're using radians when rotating. For example, 3.099999 is not 180 degrees, 3.14 radians is 180 degrees, for more accuracy use pi. \$\endgroup\$ – MichaelHouse Aug 15 '13 at 14:58
0
\$\begingroup\$

Your rounding error is accumulating and overwhelming your calculation, which will always happen when you perform the calculations incrementally like this; that is the nature of floating point.

In order to eliminate rounding error accumulation, you must either:

  • use fixed-point arithmetic; or

  • recalculate the net value on each cycle instead of incrementing/decrementing an accumulator.

This is done by counting the cycle-number cycle and setting:

if (jumped) {  
   cycle++;  
   rotation = cycle * deltaRotation;  
   playerVel.Y = cycle * gravity;  
} 
| improve this answer | |
\$\endgroup\$

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.