love2d角度,方向以及旋转

新建程序入口main.lua

function love.load()
  tank = {
      x = 400,
      y = 300,
      w = 60,
      h = 100,
      speed = 1,
      rot = 0,
      cannon = {
          w = 10,
          h = 50,
          radius = 20
        }
    }
    
    target ={
      x = 0,
      y = 0
      }
end

function keyControl()
  local down = love.keyboard.isDown
  if down("a") then
    tank.rot = tank.rot - 0.1
  elseif down("d") then
    tank.rot = tank.rot + 0.1
  elseif down("w") then
    tank.x = tank.x + tank.speed * math.sin(tank.rot)
    tank.y = tank.y - tank.speed * math.cos(tank.rot)
  elseif down("s") then
    tank.x = tank.x - tank.speed * math.sin(tank.rot)
    tank.y = tank.y + tank.speed * math.cos(tank.rot)
  end
end

function getRot(x1, y1, x2, y2)
  if x1==x2 and y1==y2 then 
    return 0 
  end
  local angle = math.atan((x2-x1)/(y2-y1))
  if y1-y2 < 0 then
    angle=angle-math.pi 
  end
  if angle > 0 then
    angle = angle - math.pi*2
  end
  return -angle
end

function mouseControl()
  target.x, target.y  = love.mouse.getPosition()
  local rot = getRot(target.x, target.y, tank.x, tank.y)
  tank.cannon.rot = rot
end

function love.update(dt)
  keyControl()
  mouseControl()
end

function love.draw()
  --车身
  love.graphics.push()
  love.graphics.translate(tank.x, tank.y)
  love.graphics.rotate(tank.rot)
  love.graphics.setColor(128,128,128)
  love.graphics.rectangle("fill", -tank.w/2, -tank.h/2, tank.w, tank.h)
  love.graphics.pop()
  
  --炮塔
  love.graphics.push()
  love.graphics.translate(tank.x, tank.y)
  love.graphics.rotate(tank.cannon.rot)
  love.graphics.setColor(0, 255, 0)
  love.graphics.circle("fill", 0, 0, tank.cannon.radius)
  love.graphics.setColor(0, 255, 255)
  love.graphics.rectangle("fill", -tank.cannon.w/2, 0, tank.cannon.w, tank.cannon.h)
  love.graphics.pop()
  
  --激光
  love.graphics.setColor(255,0,0)
  love.graphics.line(tank.x, tank.y, target.x, target.y)
end

 

运行效果

转载于:https://www.cnblogs.com/JimmyCode/p/7421217.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值