21
Feb '12

So here’s a question.
I got a constant “yacceleration”, which acts as the gravity for all the objects. It is added to the speedy (vertical speed) variable of every object, every update (multiplied with delta since last frame).
Now, objects can have a custom gravity (self.gravity), which I use in waterlevels to make Mario fall slower. This overrides yacceleration if set.
The problem is that I made it so that if you enter a floor portal that leads to another floor portal, it forces your speed to be at least a value (minspeed) that allows you to get out of the portals, so that you can’t get stuck. So what I used to have is:

if speedy > -minspeed then
	speedy = -minspeed
end

This has the downside that it doesn’t take gravity into account at all, so underwater, where Mario has a gravity of 30 instead of 80, this shoots you upwards at a silly speed.
How will I need to change this so that I can make it multiply something with self.gravity or whatever so that the resulting height that you will reach after portalling will always be approximately the same?
I remember something about Ep=Ek -> mgh = ½mv² which I’m sure would help (Ep is at the top, Ek is what I should be finding) but I didn’t pay too much attention and always cheated in the exams.
Whoever solves this gets a cookie.

Edited:
Solved, thanks to Alessandro Fregoso: Someone who can apply basic algebra (unlike me)!

New code:

local grav = yacceleration
if self and self.gravity then
	grav = self.gravity
end

local minspeed = math.sqrt(2*grav*minportalspeedy)

if speedy > -minspeed then
	speedy = -minspeed
end