function love.load()
---创建一个名为circle的对象
circle={
x=100,
y=100,
radius=25,
speed=200,
}
--[[
--给它属性x,y,半径和速度
circle.x=100
circle.y=100
circle.radius=25
circle.speed=200
--]]
end
function love.update(dt)
end
function love.draw()
--画圆
love.graphics.circle("line",circle.x,circle.y,circle.radius)
end
function love.load()
---创建一个名为circle的对象
circle={
x=100,
y=100,
radius=25,
speed=200,
}
--[[
--给它属性x,y,半径和速度
circle.x=100
circle.y=100
circle.radius=25
circle.speed=200
--]]
end
function love.update(dt)
--love.mouse.getPosition返回光标的x和y位置
mouse_x,mouse_y=love.mouse.getPosition()
angle=math.atan2(mouse_y-circle.y,mouse_x-circle.x)
end
function love.draw()
--画圆
love.graphics.circle("line",circle.x,circle.y,circle.radius)
love.graphics.setFont(love.graphics.newFont(30))
--打印角度
love.graphics.print("angle:"..angle,10,10)
--Here are some lines to visualize the velocities这里有一些线来显示速度
love.graphics.line(circle.x, circle.y, mouse_x, circle.y)
love.graphics.line(circle.x, circle.y, circle.x, mouse_y)
--The angle角度
love.graphics.line(circle.x, circle.y, mouse_x, mouse_y)
end
function love.load()
---创建一个名为circle的对象
circle={
x=100,
y=100,
radius=25,
speed=200,
}
--[[
--给它属性x,y,半径和速度
circle.x=100
circle.y=100
circle.radius=25
circle.speed=200
--]]
end
function love.update(dt)
--love.mouse.getPosition返回光标的x和y位置
mouse_x,mouse_y=love.mouse.getPosition()
angle=math.atan2(mouse_y-circle.y,mouse_x-circle.x)
sin=math.sin(angle)
cos=math.sin(angle)
--让圆圈向鼠标移动
circle.x=circle.x+circle.speed*dt
circle.y=circle.y+circle.speed*dt
end
function love.draw()
--画圆
love.graphics.circle("line",circle.x,circle.y,circle.radius)
love.graphics.setFont(love.graphics.newFont(30))
--打印角度
love.graphics.print("angle:"..angle,10,10)
--Here are some lines to visualize the velocities这里有一些线来显示速度
love.graphics.line(circle.x, circle.y, mouse_x, circle.y)
love.graphics.line(circle.x, circle.y, circle.x, mouse_y)
--The angle角度
love.graphics.line(circle.x, circle.y, mouse_x, mouse_y)
end
function love.load()
---创建一个名为circle的对象
circle={
x=100,
y=100,
radius=25,
speed=200,
}
--[[
--给它属性x,y,半径和速度
circle.x=100
circle.y=100
circle.radius=25
circle.speed=200
--]]
end
function love.update(dt)
--love.mouse.getPosition返回光标的x和y位置
mouse_x,mouse_y=love.mouse.getPosition()
angle=math.atan2(mouse_y-circle.y,mouse_x-circle.x)
sin=math.sin(angle)
cos=math.sin(angle)
--让圆圈向鼠标移动
--circle.x=circle.x+circle.speed*dt
--circle.y=circle.y+circle.speed*dt
circle.x=circle.x+circle.speed*cos*dt
circle.y=circle.y+circle.speed*sin*dt
end
function love.draw()
--画圆
love.graphics.circle("line",circle.x,circle.y,circle.radius)
love.graphics.setFont(love.graphics.newFont(30))
--打印角度
love.graphics.print("angle:"..angle,10,10)
--Here are some lines to visualize the velocities这里有一些线来显示速度
love.graphics.line(circle.x, circle.y, mouse_x, circle.y)
love.graphics.line(circle.x, circle.y, circle.x, mouse_y)
--The angle角度
love.graphics.line(circle.x, circle.y, mouse_x, mouse_y)
end
function love.load()
---创建一个名为circle的对象
circle={
x=100,
y=100,
radius=25,
speed=200,
}
--[[
--给它属性x,y,半径和速度
circle.x=100
circle.y=100
circle.radius=25
circle.speed=200
--]]
end
function love.update(dt)
--love.mouse.getPosition返回光标的x和y位置
mouse_x,mouse_y=love.mouse.getPosition()
angle=math.atan2(mouse_y-circle.y,mouse_x-circle.x)
sin=math.sin(angle)
cos=math.sin(angle)
--让圆圈向鼠标移动
--circle.x=circle.x+circle.speed*dt
--circle.y=circle.y+circle.speed*dt
--circle.x=circle.x+circle.speed*cos*dt
--circle.y=circle.y+circle.speed*sin*dt
end
function love.draw()
--画圆
love.graphics.circle("line",circle.x,circle.y,circle.radius)
love.graphics.setFont(love.graphics.newFont(30))
--打印角度
love.graphics.print("angle:"..angle,10,10)
--[[
--Here are some lines to visualize the velocities这里有一些线来显示速度
love.graphics.line(circle.x, circle.y, mouse_x, circle.y)
love.graphics.line(circle.x, circle.y, circle.x, mouse_y)
--The angle角度
love.graphics.line(circle.x, circle.y, mouse_x, mouse_y)
--]]
love.graphics.circle("line", circle.x, circle.y, circle.radius)
love.graphics.line(circle.x, circle.y, mouse_x, mouse_y)
love.graphics.line(circle.x, circle.y, mouse_x, circle.y)
love.graphics.line(mouse_x, mouse_y, mouse_x, circle.y)
end
function getDistance(x1, y1, x2, y2)
local horizontal_distance = x1 - x2
local vertical_distance = y1 - y2
end
function getDistance(x1, y1, x2, y2)
local horizontal_distance = x1 - x2
local vertical_distance = y1 - y2
--这两个都管用
local a = horizontal_distance * horizontal_distance
local b = vertical_distance ^2
end
function getDistance(x1, y1, x2, y2)
local horizontal_distance = x1 - x2
local vertical_distance = y1 - y2
--这两个都管用
local a = horizontal_distance * horizontal_distance
local b = vertical_distance ^2
local c = a + b
local distance = math.sqrt(c)
return distance
end
function love.load()
---创建一个名为circle的对象
circle={
x=100,
y=100,
radius=25,
speed=200,
}
--[[
--给它属性x,y,半径和速度
circle.x=100
circle.y=100
circle.radius=25
circle.speed=200
--]]
end
function love.update(dt)
--love.mouse.getPosition返回光标的x和y位置
mouse_x,mouse_y=love.mouse.getPosition()
angle=math.atan2(mouse_y-circle.y,mouse_x-circle.x)
sin=math.sin(angle)
cos=math.sin(angle)
--让圆圈向鼠标移动
--circle.x=circle.x+circle.speed*dt
--circle.y=circle.y+circle.speed*dt
--circle.x=circle.x+circle.speed*cos*dt
--circle.y=circle.y+circle.speed*sin*dt
local distance = getDistance(circle.x, circle.y, mouse_x, mouse_y)
if distance < 400 then
circle.x = circle.x + circle.speed * cos * (distance/100) * dt
circle.y = circle.y + circle.speed * sin * (distance/100) * dt
end
end
function getDistance(x1, y1, x2, y2)
local horizontal_distance = x1 - x2
local vertical_distance = y1 - y2
--这两个都管用
local a = horizontal_distance * horizontal_distance
local b = vertical_distance ^2
local c = a + b
local distance = math.sqrt(c)
return distance
end
function love.draw()
--画圆
love.graphics.circle("line",circle.x,circle.y,circle.radius)
love.graphics.setFont(love.graphics.newFont(30))
--打印角度
love.graphics.print("angle:"..angle,10,10)
--[[
--Here are some lines to visualize the velocities这里有一些线来显示速度
love.graphics.line(circle.x, circle.y, mouse_x, circle.y)
love.graphics.line(circle.x, circle.y, circle.x, mouse_y)
--The angle角度
love.graphics.line(circle.x, circle.y, mouse_x, mouse_y)
--]]
love.graphics.line(circle.x, circle.y, mouse_x, mouse_y)
love.graphics.line(circle.x, circle.y, mouse_x, circle.y)
love.graphics.line(mouse_x, mouse_y, mouse_x, circle.y)
local distance = getDistance(circle.x, circle.y, mouse_x, mouse_y)
love.graphics.circle("line", circle.x, circle.y, distance)
end
function love.load()
arrow = {}
arrow.x = 200
arrow.y = 200
arrow.speed = 300
arrow.angle = 0
arrow.image = love.graphics.newImage("arrow_right.png")
end
function love.update(dt)
mouse_x, mouse_y = love.mouse.getPosition()
arrow.angle = math.atan2(mouse_y - arrow.y, mouse_x - arrow.x)
cos = math.cos(arrow.angle)
sin = math.sin(arrow.angle)
arrow.x = arrow.x + arrow.speed * cos * dt
arrow.y = arrow.y + arrow.speed * sin * dt
end
function love.draw()
love.graphics.draw(arrow.image, arrow.x, arrow.y, arrow.angle)
love.graphics.circle("fill", mouse_x, mouse_y, 5)
end
以上就是今天要讲的内容,本文仅仅简单介绍了Love2d之角度和距离(Angles and distance),与博主的lua语言文章结合更好的理解love2d的编码,如果你是一名独立游戏开发者,或者一位对游戏开发有着深厚兴趣,但是又对于unity3d,ue4等这些对于新手而言不太友好的引擎而头疼的开发者;那么现在,你可以试试Love2D。Love2D是一款基于Lua编写的轻量级游戏框架,尽管官方称呼其为引擎,但实际上它只能称得上是一个框架,因为他并没有一套全面完整的解决方案。不过,这款框架上手及其容易,是学习游戏开发的初学者入门的一个良好选择。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_44918090/article/details/124593973
内容来源于网络,如有侵权,请联系作者删除!