I'm trying to create a simple platformer game, and am using windfields (from github) for the physics part.
PROBLEM 1:
I wanted to create a BSG rectangle collider for my main player but the shape is either too big/wide or ends up above the player, which makes my player sink into the platform below, and unable to jump properly when other objects are above due to the huge size of the rectangle. If it helps: I'm also using STI (simple tiled implementation) to make quads of my spritesheet, and anim8 to animate player movements.
Code: Select all
player = {}
player.collider = world:newBSGRectangleCollider(60, 145, 40, 60, 1) ---> x-pos,y-pos,width,height,shape-concavity
player.collider:setFixedRotation(true)
player.x = 120
player.y = 290
player.speed = 170
player.xvel = 0
player.yvel = 0
PROBLEM 2:
When I make my character jump without the collider, it works just fine. But if the player collider exists, the player descends extremely slowly, and the jump is really weird and broken- in that I can press the up key and the character keeps going up no matter how far the distance. So, essentially my jump isn't working as intended anymore. I tried making the gravity stronger, but that just makes the player heavier and fixes nothing.
I tried using 2 methods for the jump (one at a time), both of which failed:
i) Using an impulse: I put this function at
Code: Select all
function love.keypressed(key)
if key == 'up' then
player.collider:applyLinearImpulse(0, -5000)
end
end
Code: Select all
function love.load()
...
-- Add this below the player.img
player.ground = player.y -- This makes the character land on the plaform.
player.y_velocity = 0 -- Whenever the character hasn't jumped yet, the Y-Axis velocity is always at 0.
player.jump_height = -300 -- Whenever the character jumps, he can reach this height.
player.gravity = -500 -- Whenever the character falls, he will descend at this rate.
end
...
function love.update(dt)
...
-- jump key assignment
-- This is in charge of player jumping.
if love.keyboard.isDown('up') then
if player.y_velocity == 0 then
player.y_velocity = player.jump_height
end
end
end
...
function love.update(dt)
...
-- Add this below the jump key assignment given above.
if player.y_velocity ~= 0 then
player.y = player.y + player.y_velocity * dt
player.y_velocity = player.y_velocity - player.gravity * dt
end
-- This is in charge of collision, making sure that the character lands on the ground.
if player.y > player.ground then
player.y_velocity = 0
player.y = player.ground
end
end