Possible Duplicate:
Floating point comparison
Well this is a strange one. Ordinarily the following if( 4.0 == 4.0 ){return true}
will always return true
. In a simple little opengl 3d 'shooter' program I have, when I am trying to add 'jumping' effects, this is not the case.
The idea is fairly simple, I have a terrain of triangle strips, as the 'character' moves/walks you move along a 2d array of heights, hence you walk up and down the various elevations in the hills/troughs.
Outside of the drawScene()
function (or if you know opengl, the glutDisplayFunc()
), I have an update()
function which raises the character up and down as he 'jumps', this is called in drawScene()
. In words, as high level as I can explain it, the jumping algorithm is below:
parameters:
double currentJumpingHeight
double maximumJumpingHeight = 4.0
double ypos;
const double jumpingIncrement = 0.1; //THE PROBLEM!!!! HAS TO BE A MULTIPLE OF 0.5!!
bool isJumping = false;
bool ascending = true;
the algorithm:
(when space is pressed) isJumping = true.
if ascending = true and isJumping = true,
currentJumpHeight += jumpingIncrement (and the same for ypos)
if currentJumpingHeight == maximumJumpingHeight, //THE PROBLEM
ascending = false
(we decrement currentJumpingHeight and start to fall until we hit the ground.)
Its very simple, BUT IT ONLY WORKS WHEN jumpingIncrement IS A MULTIPLE of 0.5
!!
If jumpingIncrement
is, say, 0.1
, then currentJumpingHeight
will never equal maximumJumpingHeight
. The character takes off like a rocket and never returns to the ground. Even when the two variables are printed to the standard output and are the same, the condition is never true. This is the problem I would like to solve, it is absurd.
I don't want any feedback on the jumping algorithm - just the above paragraph please.
please help.