love2d соединяет объекты сегмента линии, чтобы сделать карандашный инструмент, который рисует линии - PullRequest
0 голосов
/ 15 мая 2018

Я пытаюсь создать игру, похожую на линейного наездника.Я вроде как запрограммировал карандаш прямо сейчас, за исключением того, что линии - это рассеянные объекты.

enter image description here

Я хочу иметь возможность соединить эти красные точки ссделайте плавную линию, чтобы шарик мог катиться по ней.

Это должно выглядеть так:

enter image description here

Это мой код

function love.load()
  printx = 0    --position to be drawn on when the mouse is pressed
  printy = 0    --position to be drawn on when the mouse is pressed

  love.physics.setMeter(64) --the height of a meter our worlds will be 64px
  world = love.physics.newWorld(0, 9.81*64, true) --The world the everything exists in. Horizontal gravity=0. Bertical gravity=9.81. True says that the world is allowed to sleep.

  objects = {} -- table to hold all our physical objects

  --let's create the ground
  objects.lines = {}
  objects.ground = {}
  objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2) 
  objects.ground.shape = love.physics.newRectangleShape(650, 50) --make a rectangle with a width of 650 and a height of 50
  objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape); --attach shape to body

  --let's create a ball
  objects.ball = {}
  objects.ball.body = love.physics.newBody(world, 650/2, 600, "dynamic") --Determines where the object will start. In the center of the world and dynamic(moves around)
  objects.ball.shape = love.physics.newCircleShape(20) --the ball's shape has a radius of 20
  objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- Attach fixture to body and give it a density of 1.


  --initial graphics setup
  love.graphics.setBackgroundColor(0.41, 0.53, 0.97) --set the background color to a nice blue
  love.window.setMode(650, 650) --set the window dimensions to 650 by 650
end


function love.update(dt)
  world:update(dt) --this puts the world into motion 

  if love.mouse.isDown(1) then
    local printx = love.mouse.getX()
    local printy = love.mouse.getY()
    line = {}
    line.body = love.physics.newBody(world, printx, printy, "static")
    line.shape = love.physics.newRectangleShape(0, 0, 5, 5)
    line.fixture = love.physics.newFixture(line.body, line.shape, 5) -- A higher density gives it more mass.
    table.insert(objects.lines, line)
    love.graphics.setColor(50,50,50)
    love.graphics.polygon("fill", line.body:getWorldPoints(line.shape:getPoints()))
  end

end 

function love.draw()
  love.graphics.setColor(0.28, 0.63, 0.05) -- set the drawing color to green for the ground
  love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates

  love.graphics.setColor(0.76, 0.18, 0.05) --set the drawing color to red for the ball
  love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius())

  for _, block in pairs(objects.lines) do
    love.graphics.polygon("fill", block.body:getWorldPoints(block.shape:getPoints()))
  end

end

1 Ответ

0 голосов
/ 29 мая 2018

Внутри love.update

  if not love.mouse.isDown(1) then
    oldx = nil
    oldy = nil
  end


  --draw lines when the mouse is down
  if love.mouse.isDown(1) then
    local printx = love.mouse.getX() + Camera.x --x coordinate of the mouse
    local printy = love.mouse.getY() + Camera.y --y coordinate of the mouse

    if oldx ~= nil then
        line = {}
        line.x1 = oldx
        line.x2 = printx
        line.y1 = oldy
        line.y2 = printy
        line.body = love.physics.newBody(world, 0, 0, "static")
        line.shape = love.physics.newEdgeShape(printx, printy, oldx, oldy)
        line.fixture = love.physics.newFixture(line.body, line.shape, 5)
        table.insert(objects.lines, line) 
    end

    oldx = printx
    oldy = printy

  end

Внутри love.draw ()

  for _, line in pairs(objects.lines) do
    love.graphics.line( line.x1 - Camera.x, line.y1 - Camera.y, line.x2 - Camera.x, line.y2 - Camera.y) --:getWorldPoints(block.shape:getPoints()))
  end
...