vrld / HC

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

How rotate over some point? #26

Closed Snuux closed 11 years ago

Snuux commented 11 years ago

I want to rotate ball shape by ball.angle like ox, oy:

love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy, kx, ky )

My code:

HC = require 'res.lib.hardoncollider'

function math.angle(x1,y1, x2,y2) return math.atan2(x2-x1, y2-y1) end

function on_collide(dt, shape_a, shape_b)

end

function love.load()
  Collider = HC(100, on_collide)

  ball = Collider:addRectangle(400, 300, 32, 32)
  ball.img = love.graphics.newImage("res/img/player.png")
  ball.velocity = {x = -100, y = 0}
  ball.angle = 0
end

function love.update(dt)
  local mx, my = love.mouse.getPosition()
  xs, ys = ball:center()
  ball.angle = -math.angle(xs, ys, mx, my)
  ball:setRotation(ball.angle, xs+16, ys+16) --rotate by right bottom angle

  Collider:update(dt)
end

function love.draw()
  ball:draw('line', 16)
  local x1,y1 = ball:center()
  local rx, ry = ball:center()
  love.graphics.draw(ball.img, x1, y1, ball:rotation(), 1, 1, 32, 32) --draw 
  love.graphics.circle('fill', xs + 16, ys + 16, 5) --show rotation point
  love.graphics.print(ball.angle, 25, 25)
end

I try this code, but... I don't know where mistake... Please help! Thanks!)

vrld commented 11 years ago

I think your issue is that the "bottom right" corner changes position when you rotate the shape: It is not always at xs+16, ys+16, but also depends on the current rotation of the shape.

You can probably work something out using hump.vector or the bundled hump.vector-light, e.g.

vector = require 'res.lib.hardoncollider.vector-light'
(...)
xs, ys = ball:center()
brx, bry = vector.add(xs, ys, vector.rotate(ball.angle, 16,16))
ball.angle = math.atan2(my-ys, mx-ys) -- SIC: atan2 has argument order y,x!
(...)

Anyway, since this issue is not a bug in the library you should ask further questions in the love forums. You also will probably be able to get a faster response this way.