Beginner to HardonCollider

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
lieudusty
Prole
Posts: 14
Joined: Thu Feb 21, 2013 5:19 am

Beginner to HardonCollider

Post 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 
Attachments
help.love
(22.57 KiB) Downloaded 143 times
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Beginner to HardonCollider

Post by Zeliarden »

lieudusty
Prole
Posts: 14
Joined: Thu Feb 21, 2013 5:19 am

Re: Beginner to HardonCollider

Post 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.
User avatar
Towerfiend
Prole
Posts: 9
Joined: Mon Jun 21, 2010 3:35 am

Re: Beginner to HardonCollider

Post 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.
Post Reply

Who is online

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