Page 1 of 1

Beginner to HardonCollider

Posted: Sun Aug 04, 2013 4:52 am
by lieudusty
Hi everyone! :D

I'm starting to use HardonCollider and it seems great! I took an attempt to make a basic moving box with walls on the map but I can't figure out how to stop the player from moving through the wall. On the collision callback I have no idea how to stop it from colliding. Can someone please help?

Code:

Code: Select all

function love.load()
	HC = require("hardoncollider")
	msg = ""
	
	function onCollide(dt, shapeA, shapeB)
		msg = "Colliding!"
	end
	
	Collider = HC(100, onCollide)
	
	px = 100
	py = 100
	
	player = Collider:addRectangle(px, py, 50, 50)
	box = Collider:addRectangle(200, 200, 50, 50)
	
	love.graphics.setBackgroundColor(200, 200, 200)
end

function love.update(dt)
	player:moveTo(px, py)
	
	Collider:update(dt)
	if love.keyboard.isDown("w") then
		py = py - 1 * 200 * dt
	elseif love.keyboard.isDown("s") then
		py = py + 1 * 200 * dt
	end
	
	if love.keyboard.isDown("a") then
		px = px - 1 * 200 * dt
	elseif love.keyboard.isDown("d") then
		px = px + 1 * 200 * dt
	end
end

function love.draw()
	love.graphics.setColor(51, 51, 51)
	player:draw("fill")
	box:draw("fill")
	love.graphics.setColor(0, 0, 0)
	love.graphics.print(px .. ", " .. py, 5, 10)
	love.graphics.print(msg, 5, 30)
	msg = ""
end

function love.keypressed(key)
end 

Re: Beginner to HardonCollider

Posted: Sun Aug 04, 2013 2:18 pm
by Zeliarden

Re: Beginner to HardonCollider

Posted: Sun Aug 04, 2013 2:46 pm
by lieudusty
Zeliarden wrote:Check the HC github reference?
http://vrld.github.io/HardonCollider/re ... tCallbacks
Thanks! I should of checked the reference first

Edit: Okay I just checked it and tried to follow the example. I got the player to move the wall but thats not what I wanted, I need players to stop at the wall and be unable to move.

Re: Beginner to HardonCollider

Posted: Mon Aug 19, 2013 5:02 am
by Towerfiend
If you're still looking to resolve the problem this is how it works for me at the moment.

Code: Select all

function love.load()
	HC = require("hardoncollider")
	msg = ""
	
	function onCollide(dt, shapeA, shapeB, x, y)
    if shapeA == player then
      shapeA:move(x, y)
    else
      shapeB:move(x, y)
    end
		msg = "Colliding!"
	end
	
	Collider = HC(100, onCollide)
	
	px = 100
	py = 100
	
	player = Collider:addRectangle(px, py, 50, 50)
	box = Collider:addRectangle(200, 200, 50, 50)
	love.graphics.setBackgroundColor(200, 200, 200)
end

function love.update(dt)
	if love.keyboard.isDown("w") then
		player:move(0,-200 * dt)
	elseif love.keyboard.isDown("s") then
		player:move(0,200 * dt)
	end
	if love.keyboard.isDown("a") then
		player:move(-200 * dt,0)
	elseif love.keyboard.isDown("d") then
		player:move(200 * dt,0)
	end
  Collider:update(dt)
end

function love.draw()
  local x,y = player:center()
	love.graphics.setColor(51, 51, 51)
	player:draw("fill")
	box:draw("fill")
	love.graphics.setColor(0, 0, 0)
	love.graphics.print(x .. ", " .. y, 5, 10)
	love.graphics.print(msg, 5, 30)
	msg = ""
end

function love.keypressed(key)
end 
When the player character approaches the wall, he cannot move past it, and has to navigate around it.