So, your character is 80 cm tall, and your conversion factor is 30 px/m.
Now, 9.8 m/s² = (9.8
m/1s²)*(30px/1
m) = 294 px/s². That's the value to enter in love.graphics.newWorld (or in world:setGravity).
About jumps: ivan's calculator returns an initial velocity of ~6.8586 m/s, i.e. ~205.758 px/s. Now you have a little problem: there's no function to add that vertical velocity directly. Your options are applyForce, applyLinearImpulse and setLinearVelocity.
applyForce divides the acceleration to apply, by the mass of the body, so you have to compensate for the mass in order to apply an acceleration. But you don't want to apply an acceleration, you want to apply a velocity. The acceleration determines how much velocity will be added after 'dt' seconds have passed, therefore in order to apply a specific velocity, you need to know in advance the dt that will be applied in the next world:update(dt) to compensate for that. In your example, you're applying the jump in love.keypressed, therefore you don't even have dt. I don't recommend this approach.
applyLinearImpulse divides the value by the mass of the body to obtain the velocity to apply, so you can compensate by multiplying it by the mass. This is a possibility.
setLinearVelocity can't be used directly, because you need to set both horizontal and vertical components. But you can retrieve the current velocity, add the values, and set the new one, so this option is not off the table. It has the additional advantage that you can easily set, rather than add to, the current vertical velocity.
My recommendation is to use applyLinearImpulse(0, getMass*-205.758).
Tried that in your world. I don't know much about Windfield, but apparently it gives you the actual world and bodies. Edited main.lua follows. I've moved setMeter before newWorld which is where it should be, even if it doesn't make a difference in this case. I've added a rectangle with the desired height for reference. The actual height seems to be a wee bit short, not sure if that's a miscalculation, rounding errors, or internal precision of Box2D.
Code: Select all
function love.load()
Object = require'classic'
anim8 = require 'anim8'
love.graphics.setDefaultFilter('nearest', 'nearest')
Camera = require "Camera"
camera = Camera()
--camera:setFollowStyle('PLATFORMER')
camera:setFollowStyle('SCREEN_BY_SCREEN')
camera.scale = 1
---- physics library
wf = require 'windfield-master/windfield'
love.physics.setMeter(30)
world = wf.newWorld(0, 0, true)
world:setGravity(0, 294)
---add player
require 'player'
player = Player(400, 300)
-- add some platforms
require 'block'
block = Block(0, 500.5, 1000, 30)
end
function love.keypressed(key)
if key == 'w' then
-- player.collider:applyForce(0, -10000)
--local x, y =player.collider:getLinearVelocity()
--player.collider:setLinearVelocity(0, y-205.758)
player.collider:applyLinearImpulse(0, player.collider:getMass()*-205.758)
end
end
function love.update(dt)
camera:update(dt)
camera:follow(player.x + player.width / 2, player.y + player.height / 2)
player:update()
--- update physics
world:update(dt)
end
function love.draw()
camera:attach()
player:draw()
block:draw()
world:draw()
camera:detach()
camera:draw()
love.graphics.rectangle("line", 300.5, 500.5-24*3, 17, 24*3-1)
love.graphics.rectangle("line", 300.5, 500.5-24*2, 17, 24)
end