if (positionY > 25.0f)
{
positionY = 25.0f;
velocityY = 0.0f;
onGround = true; // <- bug fix: previously the have been set to false
}
Yay, i have tested it usig code below and it works. The dot jumps up and falls down to ground if i click my button. 
struct PhilsGame
{
float positionX = 0;
float positionY = 30;
float velocityX = 0;
float velocityY = 0;
float gravity = 1;
bool onGround = false;
bool key_jump = false;
void StartJump()
{
if (onGround)
{
velocityY = -12.0f;
onGround = false;
}
}
void EndJump()
{
if (velocityY < -6.0f)
{
velocityY = -6.0f;
}
}
void Update()
{
if (key_jump)
StartJump();
velocityY += gravity;
positionY += velocityY;
positionX += velocityX;
if (positionY > 25.0f)
{
positionY = 25.0f;
velocityY = 0.0f;
onGround = true;
}
}
void Loop(int val)
{
Update();
//drawSprite();
RenderPoint (vec2(positionX, positionY), 1,1,1);
RenderArrow (vec2(positionX, positionY), vec2(velocityX, velocityY), 0.1f, 1,1,1);
//glutPostRedisplay();
//glutTimerFunc(33, Loop, 0);
}
};
static PhilsGame game;
if (ImGui::Button("JUMP!"))
game.StartJump();
game.Loop(0);
ImGui::Text("p %f v %f", game.positionY, game.velocityY);