vrld / HC

General purpose collision detection library for the use with LÖVE.
http://hc.readthedocs.org/
402 stars 48 forks source link

collision_stop callback isn't called. #1

Closed dannyfritz closed 12 years ago

dannyfritz commented 12 years ago
HC = require 'lib.HardonCollider'

function love.load()
    print("LOAD!")

    math.randomseed(os.time())
    math.random();math.random();math.random()
    camera = {speed = 10, distance = 0}
    gravity = 60
    HC.init(100)
    HC.setCallbacks(on_collision, end_collision)
    ball = new_ball()
    wall_left = HC.addRectangle(-10,-5000, 10, 15000)
    wall_left.id = "wall"
    wall_right = HC.addRectangle(800,-5000, 10, 15000)
    wall_right.id = "wall"
    --ground = HC.addRectangle()
    barriers = {new_barrier()}
end

function love.update(dt)
    camera.distance = camera.distance - camera.speed*dt
    camera.speed = camera.speed*1.005
    ball.update(dt)
    HC.update(dt)
end

function love.draw()
    love.graphics.translate(0, camera.distance)
    for _,barrier in ipairs(barriers) do
        barrier[1]:draw()
        barrier[2]:draw()
    end
    love.graphics.rectangle('fill',50,50,50,50)
    ball:draw('fill')
end

function new_ball()
    if ball then
        HC.remove(ball)
    end
    local ball = HC.addCircle(400,300,20)
    ball:moveTo(400, 0)
    ball.id = "ball"
    ball.collisions = 0
    ball.onGround = false
    ball.velocity = {x=0, y=0}
    ball.update =
        function(dt)
            if not ball.onGround then
                ball.velocity.y = ball.velocity.y + gravity*dt
                ball:move(0, ball.velocity.y*dt)
            end
            ball.velocity.x = ball.velocity.x-0.3*ball.velocity.x*dt
            if love.keyboard.isDown('left') then
                ball.velocity.x = ball.velocity.x - 450*dt
            elseif love.keyboard.isDown('right') then
                ball.velocity.x = ball.velocity.x + 450*dt
            end
            ball:move(ball.velocity.x*dt, 0)
        end
    return ball
end

function new_barrier()
    local barrier = {}
    local hole_width = 80
    local random_x = math.random(0,800-hole_width)
    local rect = HC.addRectangle(0, camera.distance + 600, random_x, 30)
    rect.id = "barrier"
    table.insert(barrier, rect)
    local rect = HC.addRectangle(random_x+hole_width, camera.distance + 600, 800-(random_x+hole_width), 30)
    rect.id = "barrier"
    table.insert(barrier, rect)
    return barrier
end

function on_collision(dt, shape_a, shape_b, mtv_x, mtv_y)
    if shape_a.id == "ball" then
        other = shape_b
    elseif shape_b.id == "ball" then
        other = shape_a
    else
        return
    end
    ball.collisions = ball.collisions + 1
    if other.id == "wall" then
        ball.velocity.x = -ball.velocity.x
    elseif other.id == "barrier" then
        ball.velocity.y = 0
        ball.onGround = true
        ball:move(mtv_x, mtv_y)
    end
end

function end_collision(dt, shape_a, shape_b)
    print("STOP!")
    if shape_a.id == "ball" then
        ball = shape_b
    elseif shape_b.id == "ball" then
        ball = shape_a
    else
        return
    end
    ball.collisions = ball.collisions - 1
    if ball.collisions <= 0 then
        print("FALLING!")
        ball.onGround = false
    end
end
vrld commented 12 years ago

Seems to work at my end with a fresh clone of the repository. Are you using the latest version?

dannyfritz commented 12 years ago

Do I have to do more than just 'git pull' to get the latest version?

$ git pull
remote: Counting objects: 76, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 65 (delta 40), reused 60 (delta 35)
Unpacking objects: 100% (65/65), done.
From git://github.com/vrld/HardonCollider
   6473ce4..63abae5  gh-pages   -> origin/gh-pages
   36fb13a..5aa6b33  master     -> origin/master
Updating 36fb13a..5aa6b33
Fast-forward
 class.lua       |  102 +++++++++++++++++++++++---------------
 init.lua        |  146 +++++++++++++++++++++++++++++++++++++++---------------
 polygon.lua     |    7 ++-
 shapes.lua      |  116 +++++++++++++++++++++++++++++++++++++------
 spatialhash.lua |    6 +-
 5 files changed, 272 insertions(+), 105 deletions(-)
$```
dannyfritz commented 12 years ago

Apparently Dropbox didn't propagate fast enough to my machine before I copied it over to lib/. Oops!