love2D love.physics 并将图像设置为正文
love2D love.physics and setting images to body
我正在尝试将图像设置为 Love2D 中的物理体。我已经到了在物理对象上有图像的部分,但我似乎不能只拥有图像并使物理对象的主体透明。
我有一个 class,所以我可以轻松地创建实体实例。这是绘制函数:
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,它似乎根本没有显示任何东西。
谁能帮我得到我需要的结果?
全部object.lua:
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) -- create a body at x,y
instance.shape = love.physics.newRectangleShape(x,y,width,height)
instance.fixture = love.physics.newFixture(instance.body,instance.shape) -- create a fixture of width/height
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
如果我理解正确你的问题,你应该只需要删除,
love.graphics.polygon("fill",self.body:getWorldPoints(self.shape:getPoints()))
从您的 Draw
函数中不绘制物理对象。但是,这会导致没有 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
我正在尝试将图像设置为 Love2D 中的物理体。我已经到了在物理对象上有图像的部分,但我似乎不能只拥有图像并使物理对象的主体透明。
我有一个 class,所以我可以轻松地创建实体实例。这是绘制函数:
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,它似乎根本没有显示任何东西。
谁能帮我得到我需要的结果?
全部object.lua:
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) -- create a body at x,y
instance.shape = love.physics.newRectangleShape(x,y,width,height)
instance.fixture = love.physics.newFixture(instance.body,instance.shape) -- create a fixture of width/height
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
如果我理解正确你的问题,你应该只需要删除,
love.graphics.polygon("fill",self.body:getWorldPoints(self.shape:getPoints()))
从您的 Draw
函数中不绘制物理对象。但是,这会导致没有 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