How to pick up objects

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

How to pick up objects

Post 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
gcmartijn
Party member
Posts: 144
Joined: Sat Dec 28, 2019 6:35 pm

Re: How to pick up objects

Post 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.
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

Re: How to pick up objects

Post 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
gcmartijn
Party member
Posts: 144
Joined: Sat Dec 28, 2019 6:35 pm

Re: How to pick up objects

Post by gcmartijn »

User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to pick up objects

Post by pgimeno »

The question is too broad to give a good answer. It's possible that what the OP wants is a MouseJoint.
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

Re: How to pick up objects

Post by A-lox »

how to detect if a hitbox is tuching the mouse??????????????
User avatar
Kilo
Prole
Posts: 2
Joined: Mon Jul 25, 2022 9:25 pm
Location: Pasadena, Ca

Re: How to pick up objects

Post 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.
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to pick up objects

Post 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 2736 times
Attachments
drag-n-drop-objects-01.love
License: CC0 (no license)
(1.32 KiB) Downloaded 87 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

Re: How to pick up objects

Post 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
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

Re: How to pick up objects

Post 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())
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests