I have a sneaking suspicion I’m doing this wrong. It works now, to the extend that gravity pulls the object down toward the ground, but I’m having trouble manipulating the speed of the object.
What this is, is a ball jumping and falling towards the ground.
I have another function called “jump” that just adds jSpeed to it’s yVel
I can increase gravity, and it falls faster.
I can increase the jSpeed speed, and it’ll rise up longer, but not faster
But I can’t get it to do everything faster. It just looks painfully slow, which may or may not be because of my emulator running at 11 fps, on average.
Is it just my emulator, or is it something on my end?
float time = elapsedTime/1000F;
if (speed < maxSpeed){
speed = speed + accel;
}
if(mY + mVelY < Panel.mHeight){ //0,0 is top-left
mVelY += (speed);
}
if (!(mY + height >= Panel.mHeight)){
mVelY = mVelY + gravity ;
}
mX = (float) (mX +(mVelX * time));
mY = (float) (mY + (mVelY * time));
I think you have the right general ideas here but a lot about your code is confusing.
My issues are mostly about the variable speed – it seems to me that your ball is being accelerated up by the variables speed and accel until reaching a maximum speed. Opposing this is gravity pulling (accelerating) the ball down.
Now typically this isn’t how a ‘jump’ the way you describe it is. So for me when the player hits ‘jump’ you should set the YVel to jspeed and just let the gravity part of the equation bring it back down – that is if you deleted the code:
Then it would go up for a bit and then loose momentum to the gravity and come back down – where as this code above keeps pushing it upwards until it hits the top, and as soon as it starts descending pushes it back up.
I wonder if the code around speed and maxspeed is supposed to act on XVel and not YVel as you have coded – that would make more sense.