热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

love2d角度,方向以及旋转

新建程序入口main.

新建程序入口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

运行效果

技术分享

love2d角度,方向以及旋转


推荐阅读
  • packagetest;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOE ... [详细]
  • vector:在vc6中,如果要镶嵌使用vector,如vector,后面的两个应该用,空格隔开,否则被编译器认为是移位符string::npos的值为 ... [详细]
  • Spark 贝叶斯分类算法
    一、贝叶斯定理数学基础我们都知道条件概率的数学公式形式为即B发生的条件下A发生的概率等于A和B同时发生的概率除以B发生的概率。根据此公式变换,得到贝叶斯公式:即贝叶斯定律是关于随机 ... [详细]
  • 安全3AAuthentication:认证Authorzation:授权Accouting|Audition:审计用户管理用户:UID:0,不一定是root,root的uid非0时 ... [详细]
  • 吴恩达“机器学习”——学习笔记二
    定义一些名词欠拟合(underfitting):数据中的某些成分未被捕获到,比如拟合结果是二次函数,结果才只拟合出了一次函数。过拟合(overfitting):使用过量的特征集合, ... [详细]
  • 【实践】基于RTThread的智慧路灯案例实验分享
    之前分享了基于LiteOS的智慧农业案例实验分享基于LiteOS的智慧农业案例实验分享,阅读量挺不错,看样子大家都挺喜欢这种实验。那咱们就再来一个类似的实验:基于RT-Thread ... [详细]
  • 步骤一:明确主打的核心目标用户群(对应产品侧的定位)这个核心目标用户群体是该产品成功挤进市场的切入点,甚至是撬动市场的支点和撬杠。市面上几乎很少有产品是专门给一个群体用而对其他群体 ... [详细]
  • UDP协议开发
    UDP是用户数据报协议(UserDatagramProtocol,UDP)的简称,其主要作用是将网络数据流量压缩成数据报形式,提供面向事务的简单信息传送服务。与TCP协议不同,UD ... [详细]
  • Adapter相当于C(Controller,控制器),listView相当于V(View,视图)用于显示数据为ListView提供数据的List,数组或数据库相当于MVC模式中的 ... [详细]
  • #includestdafx.h#includeiostream#includesstream#includemap#includestring ... [详细]
  • 第38天:Python decimal 模块
    by程序员野客在我们开发工作中浮点类型的使用还是比较普遍的,对于一些涉及资金金额的计算更是不能有丝毫误差,Python的decimal模块为浮点型精确计算提供了支持。1简介deci ... [详细]
  • 利用ipv6技术,废旧笔记本变成server
    如果你家的路由器已经get到了ipv6地址,并且你家的电脑也获取了有效的ipv6地址,在广域网的设备可以访问到。那恭喜你,再配合我这个dd ... [详细]
  • 淘宝http:ip.taobao.cominstructions.php接口说明请求接口(GET):servicegetIpInfo.ph ... [详细]
  • rbac 4表 常规设计
    rbac4表常规设计设计模型:1、管理员表(users)Schema::create('users',function(Blueprint$table){$tabl ... [详细]
  • 这篇文章主要简要记录了对于研发团队工作的质量 ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有