[SOLVED] Multiple Colliders On 1 Object / Sprite

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
i_love_u
Prole
Posts: 28
Joined: Sat Nov 29, 2014 12:13 am

[SOLVED] Multiple Colliders On 1 Object / Sprite

Post by i_love_u »

:awesome: I need to have separate colliders on my player character: 1 for the feet, and 1 for the head. I need to because the foot one prevents infinite jumping while the head should not.

:o The player is able to basically glitch it out now because I can jump, hit the other body from below (It is a suspended ledge), then keep hovering mid air.

:awesome: Otherwise, is there another way to detect if a player is in midair? How can I do this? (But if do it this way, I need something to block off the top of the sprite so that he is "hitting his head" and bouncing off when hitting a body from below)

Here is the code:

Code: Select all

function love.load()
   player = {
	x = 400,
	y = 475,
	image = love.graphics.newImage("pig.png"),
	body = love.physics.newBody(world, 250, 250, "dynamic"),
   shape = love.physics.newRectangleShape(10, 10), 
   }
	player.fixture = love.physics.newFixture(player.body, player.shape) --attach shape to body
end

function love.draw()
   love.graphics.draw(player.image,player.body:getX(), player.body:getY(), math.rad(360), 1, 1, width, height+12.5)
end

function love.keypressed(key)
    if key == " " then
		if jumps>0 then
			jumps=jumps-1 -- Allow only double jumps
			player.body:applyForce(0, -400)
		end
    end
end

function beginContact(a, b, coll)
	jumps=2 -- Reset jump count
end
Any help would be greatly appreciated! :awesome: :crazy: :huh: :cool:
Last edited by i_love_u on Sat Dec 06, 2014 7:09 pm, edited 1 time in total.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Multiple Colliders On 1 Object / Sprite

Post by kikito »

Hi there,

Allow me to plug my own library here: bump.lua. You probably have read about it if you frequent the "projects" forum:

viewtopic.php?f=5&t=79223
I need to have separate colliders on my player character: 1 for the feet, and 1 for the head. I need to because the foot one prevents infinite jumping while the head should not.
It is possible to add multiple separate colliders in bump. The way you do this is by creating a "main" collider for general movement, and "secondary ones" for "details" or "long libs" (Street Fighter does that). That said, for the particular issue of "detecting wether one is on the ground, so he can jump", you don't need extra colliders. The way I detect this with one collider is by looking at the direction of the collision: if it's "upward" then the player is in the ground, and he can jump.

This is how it is done in the demo: https://github.com/kikito/bump.lua/blob ... r.lua#L130

Since it is done with one collider, there's no risk of "the head moving inside of a block because it is a separate collider"
When I write def I mean function.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Multiple Colliders On 1 Object / Sprite

Post by Azhukar »

i_love_u wrote:I need to have separate colliders on my player character: 1 for the feet, and 1 for the head. I need to because the foot one prevents infinite jumping while the head should not.
You can add multiple fixtures (head and foot) to a single body and mark them using https://love2d.org/wiki/Fixture:setUserData

You can then handle the collisions differently based on what kind of fixture (foot, head, body?) is colliding with what kind of terrain fixture (floor, ceiling, wall?) and notify your jump handler whether the character with said fixtures can perform a jump.

Collisions are handled by using https://love2d.org/wiki/World:setCallbacks

In your case all you need to do is add a "foot" fixture to your body and check for collision with your "floor" fixture, then set your character variable for jumping to true, indicating it can perform a jump, otherwise always set it to false.
User avatar
i_love_u
Prole
Posts: 28
Joined: Sat Nov 29, 2014 12:13 am

Re: Multiple Colliders On 1 Object / Sprite

Post by i_love_u »

I do not understand how to set it to be "at the top" of the body, and the other at the bottom.

I am setting the collisions to simply reset my jump count back to 2. If the player presses space bar, then minus 1 jump count. But, right now you can basically jump under a ledge and keep hitting the collider to basically hover in the air.
User avatar
nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

Re: Multiple Colliders On 1 Object / Sprite

Post by nfey »

@i_love_u : you need to reset the jump counter only when the player touches the ground again. You are currently resetting it on all collisions.
In the beginContact() callback, you need to check the identity of the two fixtures and, if it's the player and the ground, reset the jump counter.
You will need to set the user data on the fixtures in order to be able to identify them in the callback (use fixture.setUserData() and fixture.getUserData())
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests