zoukankan      html  css  js  c++  java
  • 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

    运行效果

  • 相关阅读:
    js事件冒泡替我背了很多黑锅 嘿嘿
    opencvmin函数
    关于Block Formatting Context--BFC和IE的hasLayout
    javascript面向对象包装类Class的类库解析
    nodejs中exports与module.exports的区别
    ie6固定定位层fixed
    CSS的类名交集复合选择器
    遮罩层覆盖整个页面
    nodejs的require模块及路径
    struts.properties配置详解
  • 原文地址:https://www.cnblogs.com/JimmyCode/p/7421217.html
Copyright © 2011-2022 走看看