Need help with platformer game character- collider and jumping

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
seaspongicus
Prole
Posts: 3
Joined: Thu Mar 23, 2023 7:40 am

Need help with platformer game character- collider and jumping

Post by seaspongicus »

Hii, I'm new to game dev and using love2d, so I hope I can explain what my issues are at the moment.
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
These are some examples of what I mean:
Image
Image
Image

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
ii) Method 2:

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
I'd greatly appreciate any help. Additionally, I wish to keep using windfields for physics, and not box2d, that is much too complicated for my liking. Thank you all in advance!!
Attachments
characterimage3
characterimage3
Screenshot 2023-03-23 115049.jpg (5.27 KiB) Viewed 2338 times
characterimage2
characterimage2
Screenshot 2023-03-23 115045.jpg (4.07 KiB) Viewed 2338 times
characterimage1
characterimage1
Screenshot 2023-03-23 115038.jpg (4.27 KiB) Viewed 2338 times
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Need help with platformer game character- collider and jumping

Post by darkfrei »

axis-aligned bounding box (AABB) collision detection: https://love2d.org/wiki/BoundingBox.lua
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
seaspongicus
Prole
Posts: 3
Joined: Thu Mar 23, 2023 7:40 am

Re: Need help with platformer game character- collider and jumping

Post by seaspongicus »

Thanks for the link! Just looked through it. Could you explain what I should do with it? I'm a beginner. Will simply adding in the AABB collision-detection fix my game?
User avatar
BrotSagtMist
Party member
Posts: 657
Joined: Fri Aug 06, 2021 10:30 pm

Re: Need help with platformer game character- collider and jumping

Post by BrotSagtMist »

Honestly, you are new to this, what gave you the idea that starting with a physics lib is a good idea?
This is a platformer, one force apply, one tile check, two lines.
Trying to mangle this through a physics thing just ads a huge layer of confusion.
obey
tourgen
Citizen
Posts: 53
Joined: Sat Mar 18, 2023 12:45 am

Re: Need help with platformer game character- collider and jumping

Post by tourgen »

seaspongicus wrote: Thu Mar 23, 2023 3:04 pm Thanks for the link! Just looked through it. Could you explain what I should do with it? I'm a beginner. Will simply adding in the AABB collision-detection fix my game?
If you're a beginner I recommend looking at the box-box collision function previously posted, re-writing it out until you understand what it does. Then create your own boxes for your character. draw them with Lua. Run box-box overlap collision in love.update() and visualize it with simple drawing routines - color the overlap region with a translucent rectangle via love.graphics.rectangle() for instance.

Experiment with things. Do you know 2D vector math yet? Can you work out line-line intersection mathematics? This may be a case of not understanding the fundamentals. It's good to circle back to these critical, foundational blocks of knowledge. Visualize your velocity vectors and applied impulse vectors.

A full physics simulation system, with your main character that has player control also being a fully dynamic object in the physics world will make fine-tuning the "feel" of player control difficult. Your world scale (floating point domain & precision), gravity, restitution, various friction parameters will control the feel of moving the character. It will ultimately feel floaty and imprecise without quite a lot of work.
seaspongicus
Prole
Posts: 3
Joined: Thu Mar 23, 2023 7:40 am

Re: Need help with platformer game character- collider and jumping

Post by seaspongicus »

Thank you so much! I will try that out, this is really helpful.
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Need help with platformer game character- collider and jumping

Post by darkfrei »

:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest