Different types of collision depending on direction

Hello,

I am working on a classic platformer game. I want to implement one-way platforms (so when you jump from below you can get on top of the platform, but then you cannot go back down). The way I was thinking of doing it is setting different collision types depending on directions (so when direction.y is negative, it is a slide collision, while in any other direction, it is an overlap collision), but I am unable to do this.

Any ideas on how to achieve it with this or any other approach?

Did you try returning a different collision type in YourClass:collisionResponse() based on the y value ?

Something like:

function YourClass:collisionResponse(other)
	if other:isa(APlatform) then
		return self.y < 0 ? "overlap" : "slide"
	end
	
	return "slide"
end
1 Like

Thanks a lot for the response, I was thinking something like that in the collisionResponse of my Player controller, but I am not sure I understand your suggestion. In your example "y" would be a property of "YourClass" instance, which is a Sprite, not of the Collision, right?

I was expecting something more similar to this:

function Player:collisionResponse(other)
    if other:getTag() == TAGS.APlatform then
        collisionDirection.y > 0 ? "slide" : "overlap"
    else
        return "slide"
    end
end

But I am not sure how to access collisionDirection within Player:collisionResponse, as the available parameters are the 2 sprites (Player and Platform), but not the collision object.

Sorry, in my (bad) example, y was referring to the vertical speed of your sprite. So maybe something like:

function Player:collisionResponse(other)
	if other:getTag() == TAGS.APlatform then
		return self.velocity.y < 0 ? "overlap" : "slide"
	else
		return "slide"
	end
end
1 Like

That worked tweaking it slightly, but it also gave me an idea for another implementation that I prefer, by comparing the y location of both sprites and the height of the top sprite, to know which sprite is on top (has a lower y location):

function Player:collisionResponse(other)
	if other:getTag() == TAGS.APlatform then
		return self.y + self.height < other.y ? "slide" : "overlap"
	else
		return "slide"
	end
end

The reason I prefer this is that you can still touch the platform from the side while falling, and I want it to be “overlap” in that case. The above implementation ensures it is only slide when the Player collision sprite is fully above the platform sprite.

Thanks a lot for the help!!

2 Likes