Better collision than BoundingBox

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
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Better collision than BoundingBox

Post by rokit boy »

I made a slope in my platformer, but it doesn't act as a slope...
Can somebody lead me to a better path collision? (The game I am making is 8 bit)
PLEASE do not show me a lib.
u wot m8
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Better collision than BoundingBox

Post by tentus »

Without a lib, good luck. You're on your own.
Kurosuke needs beta testers
User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Re: Better collision than BoundingBox

Post by rokit boy »

But the libs were made in lua. So...
u wot m8
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Better collision than BoundingBox

Post by tentus »

So?

Every game posted on this forum was made in Lua. I didn't have any trouble with slopes in Kurosuke, nor have I had trouble with them in games using HardonCollider.
Kurosuke needs beta testers
User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Re: Better collision than BoundingBox

Post by rokit boy »

I am using a lib made by a guy called Maurice form stabyourself.net, and it has AABB (boundingbox) collision.
I just need a couple (if not 534254) lines of code which will help me, or something that could lead me the right way.
u wot m8
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Better collision than BoundingBox

Post by trubblegum »

If you're using simple bounding box collision, that's pretty much what you're stuck with, unless you can rotate your boxes, in which case it's no longer a simple bounding box method.

You could define a slope as a gradient (slope = {0, 32} or whatever) along the x axis of a tile, and check your player's position along x against a proportional y value, but I think you'd just be digging yourself a hole.

Just for the hell of it, this works on a calculator .. your mileage may vary

Code: Select all

x = tile.w / (player.x - tile.x)
height = (slope[1] - slope[2]) / x
y = (slope[1] < slope[2] and tile.y + height) or (slope[1] > slope[2] and tile.y - height) or tile.y
collision = player.y > y
An alternative might be a pixel check, but do this judiciously.

Sometimes it's better to learn a good lib which can handle stuff like this more flexibly, and make life easier for you in the future too.
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Better collision than BoundingBox

Post by molul »

tentus wrote:I didn't have any trouble with slopes in Kurosuke, nor have I had trouble with them in games using HardonCollider.
I'm very curious on this. I've been strugglling with slopes for like a month, first using tile-based collisions, then using HardonCollider (as you recommended me when I registered here) in two different ways. I couldn't get slopes to work, and I also never found a good example to study (not to copy-paste it).

So, do you have anything I could look at? ^^
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Better collision than BoundingBox

Post by tentus »

Sure, here's a little test I put together a few months ago:

Code: Select all

function love.load()
	local HC = require 'hardoncollider'

	collider = HC(100, on_collide)
	
	hero = collider:addCircle(400,300, 24)
	boxes = {}
	for i=1, 6 do
		boxes[i] = collider:addRectangle(100 * i - 50, 150 + (i * 50), 50,50)
		collider:setPassive(boxes[i])
	end
	boxes[7] = collider:addRectangle(700, love.graphics.getHeight() - 100, 100,100)
	boxes[7]:rotate(math.rad(45))
	boxes[8] = collider:addRectangle(0,love.graphics.getHeight() - 50, love.graphics.getWidth(), 500)
	for i=1, #boxes do
		collider:setPassive(boxes[i])
	end
	
	foe = collider:addRectangle(500,450, 50,50)
	foe.last = 0
	foe.walk = {x = -100, y = 0}

	speed = 200
	hp = 5
	
	gravity = 100
	
	love.graphics.setFont(20)
end

function love.update(dt)
	collider:update(dt)
	
	local hx, hy = 0, 0
	-- player movement
	if love.keyboard.isDown('up') then	-- we can fly
		hy = -speed * dt
	else 
		hy = gravity * dt
	end
	if love.keyboard.isDown('left') then
		hx = -speed * dt
	elseif love.keyboard.isDown('right') then
		hx = speed * dt
	end
	hero:move(hx, hy)
	
	foe.last = foe.last + dt
	foe:move(foe.walk.x * dt, foe.walk.y * dt)
	local fx, fy = foe:center()
	if fx < 100 or fx > 500 then
		foe.walk.x = -foe.walk.x
	end
end

function love.draw()
	hero:draw('fill', 24)
	for i=1, #boxes do
		boxes[i]:draw('fill')
	end
	foe:draw('line')
	love.graphics.print("HP: "..hp, 5, 5)
end

function on_collide(dt, a, b, x,y)
	if b == foe then	-- gettin fiesty
		if b.last > 1 then		-- we can only collide once per second
			b.last = 0
			hp = hp - 1
		end
	else	-- it's a solid
		hero:move(x, y)
	end
end
It's mostly just me dicking around with HC, figuring out some basic concepts.
Kurosuke needs beta testers
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Better collision than BoundingBox

Post by molul »

Thank you! I'll see if I can apply that to my project. There are a few things I have to tweak, but overall it works much better and it's much simpler than what I had.

This should be uploaded to the snippets section, IMO :)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 77 guests