Love2D中的love.physics和为对象设置图像

我正在尝试将图像设置为Love2D中物理体的一部分。我已经有了一个物理对象上的图像,但是我无法让物理对象的主体透明,只能让图像透明。

我有一个类,因此可以轻松创建物体实例。这里是绘图函数:

function object:Draw()
    love.graphics.polygon("fill",self.body:getWorldPoints(self.shape:getPoints()))
    if self.image then
        love.graphics.draw(self.image, self.body:getX(), self.body:getY(), self.body:getAngle(),  1, 1, self.image:getWidth()/2, self.image:getHeight()/2)
    end
end

如果我在填充多边形之前绘制self.image,则似乎根本不显示任何内容。

有人可以帮助我得到所需的结果吗?

所有对象的内容:

local object = {}
local object_mt = { __index = object }
function object.newRect(world,x,y,width,height,typeOfBody,sprite)
  local instance = {}
  setmetatable(instance, object_mt)
  instance.body = love.physics.newBody(world, x, y, typeOfBody or nil)  --在x,y处创建主体
  instance.shape = love.physics.newRectangleShape(x,y,width,height)
  instance.fixture = love.physics.newFixture(instance.body,instance.shape)  --创建宽度/高度的装置
  instance.image = sprite and love.graphics.newImage(sprite) or nil
  return instance
end
function object:Draw()
    love.graphics.polygon("fill",self.body:getWorldPoints(self.shape:getPoints()))
    if self.image then
        love.graphics.draw(self.image, self.body:getX(), self.body:getY(), self.body:getAngle(),  1, 1, self.image:getWidth()/2, self.image:getHeight()/2)
    end
end
function object.getPosition()
  return self.body:GetPosition()
end
return object
点赞
用户336528
用户336528

如果我理解你的问题正确的话,你只需要在 Draw 函数中移除下面这一行代码:

love.graphics.polygon("fill",self.body:getWorldPoints(self.shape:getPoints()))

就可以不绘制物理对象。然而,这会导致没有 image 的对象不显示任何内容。如果你只想从带有 image 的对象中移除多边形,则可以使用以下代码:

function object:Draw()
  if self.image then
    love.graphics.draw(
      self.image, self.body:getX(), self.body:getY(), self.body:getAngle(),
      1, 1, self.image:getWidth() / 2, self.image:getHeight() / 2
    )
  else
    love.graphics.polygon("fill", self.body:getWorldPoints(self.shape:getPoints()))
  end
end
2016-11-24 20:10:02