vrld / HC

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

Problems with non central rotating #5

Closed dannyfritz closed 12 years ago

dannyfritz commented 12 years ago

It just simply doesn't work with rectangles that aren't rotating around their center.

require 'HC'

function love.load()
  HC.init(100, on_collision)
  rect = HC.addRectangle(200,200,200,50)
  rect.tag = "rect"
  mouse = HC.addRectangle(0,0,32,32)
  mouse.tag = "mouse"
end

function love.update(dt)
    function love.keypressed(key)
        if (key == "left") then
        rotate = dt
        end
        if (key == "right") then
        rotate = -dt
        end
        if (key == "kpenter") then
        rotate = 0
        end
    end
    rect:rotate(rotate or 0, 150, 150)

  mouse:moveTo(love.mouse.getPosition())
    debugUPDATE()
    HC.update(dt)
    --end testing
end

function on_collision(dt, shape_a, shape_b)
  text[#text+1] = string.format("Colliding")
  local shape_other
  if shape_a.tag == "mouse" then
    shape_other = shape_b
  elseif shape_b.tag == "mouse" then
    shape_other = shape_a
  end
  if shape_other.tag == "enemy" then
    text[#text+1] = string.format("Enemy "..shape_other.id.."!")
  elseif shape_other.tag == "rect" then
    text[#text+1] = string.format("Rect!"..dt)
  end
end

function love.draw()
  rect:draw('line')
  mouse:draw('fill')
  debugDRAW()
end

text = {}
function debugUPDATE()
    while #text > 40 do
      table.remove(text, 1)
    end
end
function debugDRAW()
  for i = 1,#text do
    love.graphics.print(text[#text - (i-1)], 10, i * 15)
  end
end
vrld commented 12 years ago

Good catch. Took me a while to figure this one out. It will be fixed in the next commit (which will happen soon). To fix this, add the line

self.centroid = (self.centroid - center):rotate_inplace(angle) + center

to Polygon:rotate() in polygon.lua.

vrld commented 12 years ago

Fixed