I have been using love2d for over 2 months but I am new to love2d pysics
My question is how to pic objects with your mouse
How to pick up objects
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: How to pick up objects
do you mean drag and drop?
In the adventure game i'm trying to make, I delete the object from the scene and place it in an player inventory table.
In the adventure game i'm trying to make, I delete the object from the scene and place it in an player inventory table.
Re: How to pick up objects
When I use google: love2d drag and drop I found this.
https://stackoverflow.com/questions/385 ... -in-love2d
https://gist.github.com/mebens/1196228
https://stackoverflow.com/questions/385 ... -in-love2d
https://gist.github.com/mebens/1196228
Re: How to pick up objects
The question is too broad to give a good answer. It's possible that what the OP wants is a MouseJoint.
Re: How to pick up objects
how to detect if a hitbox is tuching the mouse??????????????
Re: How to pick up objects
There's a tutorial on drag & drop in this old thread:
viewtopic.php?p=249895#p249895
It still works if you make some very minor edits.
viewtopic.php?p=249895#p249895
It still works if you make some very minor edits.
Re: How to pick up objects
Here is an easy example how to do the drag-n-drop rectangles and circles:
Code: Select all
-- License CC0 (Creative Commons license) (c) darkfrei, 2022
local dndo = {} -- drag-n-drop-objects
function dndo.load (objects)
dndo.objects = objects
print ('#objects: ' .. #objects)
end
local draw = {}
function draw.rectangle (object, fill, line)
love.graphics.setColor(fill)
love.graphics.rectangle ('fill', object.x, object.y, object.w, object.h)
love.graphics.setColor(line)
love.graphics.rectangle ('line', object.x, object.y, object.w, object.h)
end
function draw.circle (object, fill, line)
love.graphics.setColor(fill)
love.graphics.circle ('fill', object.x, object.y, object.r)
love.graphics.setColor(line)
love.graphics.circle ('line', object.x, object.y, object.r)
end
function dndo.draw ()
for i, object in ipairs (dndo.objects) do
if dndo.pressed and dndo.pressed == object then
draw[object.type](object, object.color, object.pressedOutlineColor)
elseif dndo.hovered and dndo.hovered == object then
draw[object.type](object, object.color, object.hoveredOutlineColor)
else
draw[object.type](object, object.color, object.outlineColor)
end
end
end
local collisionWith = {}
function collisionWith.rectangle (x, y, object)
if x > object.x and y > object.y
and x < object.x+object.w and y < object.y + object.h then
return true
end
return false
end
function collisionWith.circle (x, y, object)
if x > object.x - object.r and y > object.y - object.r
and x < object.x+object.r and y < object.y + object.r then
local dx = object.x-x
local dy = object.y-y
if (dx*dx+dy*dy) < object.r*object.r then
return true
else
return false
end
end
return false
end
function dndo.mousepressed( x, y, button, istouch, presses )
if dndo.hovered then
dndo.pressed = dndo.hovered
dndo.hovered = nil
end
end
local function checkHovered (x, y)
for i = #dndo.objects, 1, -1 do -- backwards
local object = dndo.objects[i]
if collisionWith[object.type] (x, y, object) then
dndo.hovered = object
return
end
end
dndo.hovered = nil
end
function dndo.mousemoved( x, y, dx, dy, istouch )
if dndo.pressed then
dndo.pressed.x = dndo.pressed.x+dx
dndo.pressed.y = dndo.pressed.y+dy
else
-- check hovered
checkHovered (x, y)
end
end
function dndo.mousereleased( x, y, button, istouch, presses )
if dndo.pressed then
dndo.pressed = nil
checkHovered (x, y)
end
end
return dndo
- Attachments
-
- drag-n-drop-objects-01.love
- License: CC0 (no license)
- (1.32 KiB) Downloaded 87 times
Re: How to pick up objects
thank you very muchdarkfrei wrote: ↑Sun Aug 14, 2022 9:51 pm Here is an easy example how to do the drag-n-drop rectangles and circles:
2022-08-14T23_50_52-Untitled.pngCode: Select all
-- License CC0 (Creative Commons license) (c) darkfrei, 2022 local dndo = {} -- drag-n-drop-objects function dndo.load (objects) dndo.objects = objects print ('#objects: ' .. #objects) end local draw = {} function draw.rectangle (object, fill, line) love.graphics.setColor(fill) love.graphics.rectangle ('fill', object.x, object.y, object.w, object.h) love.graphics.setColor(line) love.graphics.rectangle ('line', object.x, object.y, object.w, object.h) end function draw.circle (object, fill, line) love.graphics.setColor(fill) love.graphics.circle ('fill', object.x, object.y, object.r) love.graphics.setColor(line) love.graphics.circle ('line', object.x, object.y, object.r) end function dndo.draw () for i, object in ipairs (dndo.objects) do if dndo.pressed and dndo.pressed == object then draw[object.type](object, object.color, object.pressedOutlineColor) elseif dndo.hovered and dndo.hovered == object then draw[object.type](object, object.color, object.hoveredOutlineColor) else draw[object.type](object, object.color, object.outlineColor) end end end local collisionWith = {} function collisionWith.rectangle (x, y, object) if x > object.x and y > object.y and x < object.x+object.w and y < object.y + object.h then return true end return false end function collisionWith.circle (x, y, object) if x > object.x - object.r and y > object.y - object.r and x < object.x+object.r and y < object.y + object.r then local dx = object.x-x local dy = object.y-y if (dx*dx+dy*dy) < object.r*object.r then return true else return false end end return false end function dndo.mousepressed( x, y, button, istouch, presses ) if dndo.hovered then dndo.pressed = dndo.hovered dndo.hovered = nil end end local function checkHovered (x, y) for i = #dndo.objects, 1, -1 do -- backwards local object = dndo.objects[i] if collisionWith[object.type] (x, y, object) then dndo.hovered = object return end end dndo.hovered = nil end function dndo.mousemoved( x, y, dx, dy, istouch ) if dndo.pressed then dndo.pressed.x = dndo.pressed.x+dx dndo.pressed.y = dndo.pressed.y+dy else -- check hovered checkHovered (x, y) end end function dndo.mousereleased( x, y, button, istouch, presses ) if dndo.pressed then dndo.pressed = nil checkHovered (x, y) end end return dndo
Re: How to pick up objects
y am i getting a trying to use a destroid joint error
when i did
entetie.joint = world:addJoint('MouseJoint', entetie.hitbox, mx,my, -100, 50, true)
table.insert(entetie,entetie.joint)
table.insert(enteties,entetie)
and then
c.joint:setTarget(love.mouse.getPosition())
when i did
entetie.joint = world:addJoint('MouseJoint', entetie.hitbox, mx,my, -100, 50, true)
table.insert(entetie,entetie.joint)
table.insert(enteties,entetie)
and then
c.joint:setTarget(love.mouse.getPosition())
Who is online
Users browsing this forum: Bing [Bot] and 3 guests