Page 1 of 1

How to pick up objects

Posted: Sun Aug 14, 2022 5:42 pm
by A-lox
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

Re: How to pick up objects

Posted: Sun Aug 14, 2022 6:45 pm
by gcmartijn
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.

Re: How to pick up objects

Posted: Sun Aug 14, 2022 6:55 pm
by A-lox
gcmartijn wrote: Sun Aug 14, 2022 6:45 pm 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.
yes drag and drop

Re: How to pick up objects

Posted: Sun Aug 14, 2022 7:01 pm
by gcmartijn

Re: How to pick up objects

Posted: Sun Aug 14, 2022 7:05 pm
by pgimeno
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

Posted: Sun Aug 14, 2022 7:21 pm
by A-lox
how to detect if a hitbox is tuching the mouse??????????????

Re: How to pick up objects

Posted: Sun Aug 14, 2022 8:36 pm
by Kilo
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.

Re: How to pick up objects

Posted: Sun Aug 14, 2022 9:51 pm
by darkfrei
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
2022-08-14T23_50_52-Untitled.png
2022-08-14T23_50_52-Untitled.png (11.41 KiB) Viewed 2595 times

Re: How to pick up objects

Posted: Sun Aug 14, 2022 10:36 pm
by A-lox
darkfrei wrote: Sun Aug 14, 2022 9:51 pm 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
2022-08-14T23_50_52-Untitled.png
thank you very much

Re: How to pick up objects

Posted: Mon Aug 15, 2022 1:15 am
by A-lox
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())