Another issue is realism.
The rate at which your elevator is moving up doesn’t reflect the true rate at which an elevator would move.
It feels right in the context of the game, but in reality an elevator that moved that fast and slowed that rapidly in the real-world would have the same effect on a human or object in the elevator as you’re seeing. It kind of makes me chuckle to think what that would look like.
Anyways, this leaves us with a conundrum. The Box2D physics library is trying to follow the rules, but the result isn’t what we want. i.e. We want fast movement (feels right in the game), but we also want fast stopping and objects not bouncing / thrown around.
@XeduR suggested some options and I have another. Instead of stopping the elevator abruptly, reduce the rate to zero over a short period just before it reaches the end of the move distance.
I think you can use the proxy helper from SSK2 to achieve this:
You don’t need all of SSK, you can grab the proxy helper alone: https://github.com/roaminggamer/SSK2/blob/master/ssk2/external/proxy.lua
The general idea (code may not be entirely right) is something like this:
-- PSEUDO CODE!!!
-- PSEUDO CODE!!!
-- PSEUDO CODE!!!
local proxy = require "proxy" -- assumes is in same folder as main.lua, but put it wherever you want and change require call
elevator = ssk.proxy.get_proxy_for( elevator )
function elevator:propertyUpdate( event )
if( <moving up> && elevetor.y >= elevator.targetY ) begin
-- Stop moving and snap to target Y
elevator:setLinearVelocity(0,0)
elevator.y = elevator.targetY
end else if( <moving down> && elevetor.y <= elevator.targetY ) begin
-- Stop moving and snap to target Y
elevator:setLinearVelocity(0,0)
elevator.y = elevator.targetY
else begin
elevator.setLinearVelocity( 0, self.yVel )
end
end
elevator:addEventListener( "propertyUpdate" )
... then later
elevator.targetY = <calculate me>
local distance = math.abs(platform.y - elevator.targetY)
local moveTime = <calculate me based on rate of movement and distance>
elevator.yVel = <initial velocity>
transition.to( elevator, { time = moveTime, yVel = 0, transition = easing.inOutSine } )
-- or maybe try easing.outSine
If this idea is at all attractive, you might code up a standalone test to check the code first and get it all working, then apply that to your game.
Alternately, if this is totally confusing but you want to try it and can’t work it out, post back. I’ll try to find some time to make a demo. I just can’t right now.