How to convert a image into a body in Lua ??
Posted: Tue Mar 07, 2023 11:10 pm
--I'm new to this language--
I'm trying to make a maze game and I need an image that can act as a body
I need to set the image to a static body type and I'm not sure how to do that .
I want the square that is drawn to not go through the image .
if there is any other way to do that or a solution for this that would be very helpful
I'm also getting an error because I tried a solution from my head but it doesn't work
Any Help Would be GREATLY Appreciated
THIS IS MY CODE
|
v
____________________________________________
function love.load()
canvas = love.graphics.newCanvas(800, 600)
-- square = love.graphics.newImage("Mymaze-removebg-preview.png")
love.graphics.line( 30, 50, 332, 145)
-- rect = Collider:addRectangle(200,400,400,20)
-- Rectangle is drawn to the canvas with the regular/default alpha blend mode ("alphamultiply").
love.graphics.setCanvas(canvas)
-- love.graphics.setBlendMode("alpha")
--love.graphics.setColor(1, 0, 0, .5)
-- love.graphics.rectangle("line", 0,0, 100,100)
love.graphics.setCanvas()
world = love.physics.newWorld(0,0, true)
--body1 = love.physics.newBody( world, player_x, player_y, "dynamic" )
--shape = love.physics.newRectangleShape( 50, 50 )
--fixture = love.physics.newFixture( body1, shape, 10 )
-- body2 = love.physics.newBody( world, player_x, player_y, "dynamic" )
--shape = love.physics.newRectangleShape( 90, 90 )
-- fixture = love.physics.newFixture( body2, shape, 10 )
-- body1 = Shape:getBody( )
Player ={}
player_x=100
player_y=100
local point
-- local distance = (maze.Position - square.Position).magnitude
objects = {} -- table to hold all our physical objects
--let's create a ball
objects.square = {}
objects.square.body = love.physics.newBody(world, player_x, player_y, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
objects.square.shape = love.physics.newRectangleShape( 50,50) --the ball's shape has a radius of 20
objects.square.fixture = love.physics.newFixture(objects.square.body, objects.square.shape, 1) -- Attach fixture to body and give it a density of 1.
-- objects.ball.fixture:setRestitution(0.9) --let the ball bounce
---------------------------------------------------------------
-- table to hold all our physical objects
--let's create a ball
-- objects.square12 = {}
-- objects.square12.body2 = love.physics.newBody(world, 100, 100, "static") --place the body in the center of the world and make it dynamic, so it can move around
--objects.square12.shape = love.physics.newRectangleShape( 100,50) --the ball's shape has a radius of 20
--objects.square12.fixture = love.physics.newFixture(objects.square12.body2, objects.square12.shape, 1) -- Attach fixture to body and give it a density of 1.
-- objects.ball.fixture:setRestitution(0.9) --let the ball bounce
--Player = body1
--maze =body2
--player.x = player_x
--player.y = player_y
local points = {}
----------------------------------------------------------------------------------------------
local getX
local getY
--objects.maze = {}
-- objects.maze.body = love.physics.newBody(world, 100, 100, "static") --place the body in the center of the world and make it dynamic, so it can move around
--
-- objects.maze.fixture = love.physics.newFixture(objects.maze.body, objects.maze.shape, 1) -- Attach fixture to body and give it a density of 1.
-- objects.ball.fixture:setRestitution(0.9) --let the ball bounce
objects.square12 = {}
objects.square12.body2 = love.physics.newBody( world, 100, 100, "static" )
-- objects.square12.image = love.physics.newRectangleShape(50,50 ) --the ball's shape has a radius of 20
objects.square12.shpe = objects.square12.image("maze")
objects.square12.fixture = love.physics.newFixture(objects.square12.body2, objects.square12.shape, 1) -- Attach fixture to body
--objects.square12.body2 = maze
end
-- create the point object
function love.update(dt)
local distancex
local distancey
world:update(dt)
x, y = objects.square.body:getPosition( )
if love.keyboard.isDown("d") then
--if objects.square12.body2 == maze then
--touching = objects.square.body:isTouching(objects.square12.body2)
if touching == true then
objects.square.body:setLinearVelocity( 0, 0 )
end
--end
--player_x =player_x + 2
objects.square.body:setLinearVelocity( 70, 0 )
else
end
if love.keyboard.isDown("a") then
player_x =player_x -2
objects.square.body:setLinearVelocity( -70, 0)
objects.square.body:setLinearVelocity( -70, 0)
objects.square.body:setLinearVelocity( -70, 0 )
else
end
if love.keyboard.isDown("s") then
player_y =player_y +2
objects.square.body:setLinearVelocity( 0, 70 )
else
end
if love.keyboard.isDown("w") then
player_y =player_y -2
objects.square.body:setLinearVelocity( 0, -70 )
else
end
if love.keyboard.isDown("x") then
player_y =player_y -2
objects.square.body:setLinearVelocity( 0, 0 )
else
end
--function title(maze)
-- love.graphics.draw(maze)
--end
end
function love.draw()
--title(maze)
--love.graphics.rectangle("fill",player_x,player_y, 20,20)
--love.draw.image("Mymaze-removebg-preview-removebg-preview.png")
love.graphics.draw( objects.square12.shape , 100, 100)
--love.graphics.draw(square, 20, 20)
--maze=body2
--draw.add("rect", 10, 50, 220, 200, "blue", 3)
--draw.overdraw()
--love.graphics.shape("fill", player_x, player_y, 50, 50, 2, 2,"2")
--love.graphics.rectangle("fill", objects.square12.body2:getX(), objects.square12.body2:getY(),20,20)
love.graphics.setColor(0.76, 0.18, 0.05) --set the drawing color to red for the ball
--love.graphics.rectangle("fill",player_x,player_y, 20,20 )
love.graphics.rectangle("fill", objects.square.body:getX(), objects.square.body:getY(),20,20)
-------------------------------
--love.graphics.rectangle("fill", objects.square12.body2:getX(), objects.square12.body2:getY(),100,100)
--love.graphics.draw(maze.image, objects.square12.body2:getX(), objects.square12.body2:getY(), objects.square12.body2:getAngle(), 1, 1, objects.square12.body2s.image:getWidth()/2, objects.square12.body2.image:getHeight()/2)
----------------------------------------------
--love.graphics.rectangle("fill", objects.square12.body2:getX(), objects.square12.body2:getY(),50,50)
if objects.square12.body2.image then
touching = objects.square.body:isTouching(objects.square12.body2 )
if touching == true then
objects.square.body:setLinearVelocity( 0, 0 )
end
end
red= 115/255
green = 27/255
blue = 135/255
alpha = 50/100
love.graphics.setBackgroundColor( red, green, blue, alpha)
end
I'm trying to make a maze game and I need an image that can act as a body
I need to set the image to a static body type and I'm not sure how to do that .
I want the square that is drawn to not go through the image .
if there is any other way to do that or a solution for this that would be very helpful
I'm also getting an error because I tried a solution from my head but it doesn't work
Any Help Would be GREATLY Appreciated
THIS IS MY CODE
|
v
____________________________________________
function love.load()
canvas = love.graphics.newCanvas(800, 600)
-- square = love.graphics.newImage("Mymaze-removebg-preview.png")
love.graphics.line( 30, 50, 332, 145)
-- rect = Collider:addRectangle(200,400,400,20)
-- Rectangle is drawn to the canvas with the regular/default alpha blend mode ("alphamultiply").
love.graphics.setCanvas(canvas)
-- love.graphics.setBlendMode("alpha")
--love.graphics.setColor(1, 0, 0, .5)
-- love.graphics.rectangle("line", 0,0, 100,100)
love.graphics.setCanvas()
world = love.physics.newWorld(0,0, true)
--body1 = love.physics.newBody( world, player_x, player_y, "dynamic" )
--shape = love.physics.newRectangleShape( 50, 50 )
--fixture = love.physics.newFixture( body1, shape, 10 )
-- body2 = love.physics.newBody( world, player_x, player_y, "dynamic" )
--shape = love.physics.newRectangleShape( 90, 90 )
-- fixture = love.physics.newFixture( body2, shape, 10 )
-- body1 = Shape:getBody( )
Player ={}
player_x=100
player_y=100
local point
-- local distance = (maze.Position - square.Position).magnitude
objects = {} -- table to hold all our physical objects
--let's create a ball
objects.square = {}
objects.square.body = love.physics.newBody(world, player_x, player_y, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
objects.square.shape = love.physics.newRectangleShape( 50,50) --the ball's shape has a radius of 20
objects.square.fixture = love.physics.newFixture(objects.square.body, objects.square.shape, 1) -- Attach fixture to body and give it a density of 1.
-- objects.ball.fixture:setRestitution(0.9) --let the ball bounce
---------------------------------------------------------------
-- table to hold all our physical objects
--let's create a ball
-- objects.square12 = {}
-- objects.square12.body2 = love.physics.newBody(world, 100, 100, "static") --place the body in the center of the world and make it dynamic, so it can move around
--objects.square12.shape = love.physics.newRectangleShape( 100,50) --the ball's shape has a radius of 20
--objects.square12.fixture = love.physics.newFixture(objects.square12.body2, objects.square12.shape, 1) -- Attach fixture to body and give it a density of 1.
-- objects.ball.fixture:setRestitution(0.9) --let the ball bounce
--Player = body1
--maze =body2
--player.x = player_x
--player.y = player_y
local points = {}
----------------------------------------------------------------------------------------------
local getX
local getY
--objects.maze = {}
-- objects.maze.body = love.physics.newBody(world, 100, 100, "static") --place the body in the center of the world and make it dynamic, so it can move around
--
-- objects.maze.fixture = love.physics.newFixture(objects.maze.body, objects.maze.shape, 1) -- Attach fixture to body and give it a density of 1.
-- objects.ball.fixture:setRestitution(0.9) --let the ball bounce
objects.square12 = {}
objects.square12.body2 = love.physics.newBody( world, 100, 100, "static" )
-- objects.square12.image = love.physics.newRectangleShape(50,50 ) --the ball's shape has a radius of 20
objects.square12.shpe = objects.square12.image("maze")
objects.square12.fixture = love.physics.newFixture(objects.square12.body2, objects.square12.shape, 1) -- Attach fixture to body
--objects.square12.body2 = maze
end
-- create the point object
function love.update(dt)
local distancex
local distancey
world:update(dt)
x, y = objects.square.body:getPosition( )
if love.keyboard.isDown("d") then
--if objects.square12.body2 == maze then
--touching = objects.square.body:isTouching(objects.square12.body2)
if touching == true then
objects.square.body:setLinearVelocity( 0, 0 )
end
--end
--player_x =player_x + 2
objects.square.body:setLinearVelocity( 70, 0 )
else
end
if love.keyboard.isDown("a") then
player_x =player_x -2
objects.square.body:setLinearVelocity( -70, 0)
objects.square.body:setLinearVelocity( -70, 0)
objects.square.body:setLinearVelocity( -70, 0 )
else
end
if love.keyboard.isDown("s") then
player_y =player_y +2
objects.square.body:setLinearVelocity( 0, 70 )
else
end
if love.keyboard.isDown("w") then
player_y =player_y -2
objects.square.body:setLinearVelocity( 0, -70 )
else
end
if love.keyboard.isDown("x") then
player_y =player_y -2
objects.square.body:setLinearVelocity( 0, 0 )
else
end
--function title(maze)
-- love.graphics.draw(maze)
--end
end
function love.draw()
--title(maze)
--love.graphics.rectangle("fill",player_x,player_y, 20,20)
--love.draw.image("Mymaze-removebg-preview-removebg-preview.png")
love.graphics.draw( objects.square12.shape , 100, 100)
--love.graphics.draw(square, 20, 20)
--maze=body2
--draw.add("rect", 10, 50, 220, 200, "blue", 3)
--draw.overdraw()
--love.graphics.shape("fill", player_x, player_y, 50, 50, 2, 2,"2")
--love.graphics.rectangle("fill", objects.square12.body2:getX(), objects.square12.body2:getY(),20,20)
love.graphics.setColor(0.76, 0.18, 0.05) --set the drawing color to red for the ball
--love.graphics.rectangle("fill",player_x,player_y, 20,20 )
love.graphics.rectangle("fill", objects.square.body:getX(), objects.square.body:getY(),20,20)
-------------------------------
--love.graphics.rectangle("fill", objects.square12.body2:getX(), objects.square12.body2:getY(),100,100)
--love.graphics.draw(maze.image, objects.square12.body2:getX(), objects.square12.body2:getY(), objects.square12.body2:getAngle(), 1, 1, objects.square12.body2s.image:getWidth()/2, objects.square12.body2.image:getHeight()/2)
----------------------------------------------
--love.graphics.rectangle("fill", objects.square12.body2:getX(), objects.square12.body2:getY(),50,50)
if objects.square12.body2.image then
touching = objects.square.body:isTouching(objects.square12.body2 )
if touching == true then
objects.square.body:setLinearVelocity( 0, 0 )
end
end
red= 115/255
green = 27/255
blue = 135/255
alpha = 50/100
love.graphics.setBackgroundColor( red, green, blue, alpha)
end