Need help finding an algorithm to see of click is on a block

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
Cpt. Dave
Prole
Posts: 2
Joined: Wed May 15, 2013 9:22 pm

Need help finding an algorithm to see of click is on a block

Post by Cpt. Dave »

So I've been building this small game for fun, 3 bricks fall at randomly selected coordinates that you then have to click with your mouse to "save" them from falling
so i need some code to put in here:

Code: Select all

function love.mousepressed()

end
so that when i click on my three blocks:

Code: Select all

objects.enemy.body
objects.enemy2.body
objects.enemy3.body
they will be saved and the player will be awarded a point.

I began coding in C# so i thought i could just borrow and convert some code i did in a different project that did the same thing i need done here:

Code: Select all

private void Form1_MouseDown(object sender, MouseEventArgs e)
        {

            int x = 40;
            int y = 40;
            int diffX = e.X - buttonRec.X;
            int diffY = e.Y - buttonRec.Y - 10;

            double diff = Math.Sqrt((diffX * diffX) - (diffY * diffY));

            if (diff <= x && diff <= y)
            {
                score++;
                labelscore.Text = score.ToString();
            }
        }
for those who don't code C# this basically finds the distance between the mouse click and the image that moves around on screen, and if the click is within the image you get one point.

but sadly i couldn't get it to work.

And for good measure ill include the main.lua code so you can get the whole view:(i know the codes a little rough, i plan on cleaning it up later though)

Code: Select all

function love.load()
	love.physics.setMeter(64)
	world = love.physics.newWorld(0, 9.81 * 64, true)

	objects = {}

	objects.enemy = {}

	objects.enemy.body = love.physics.newBody(world, 650/2, 650/5, "dynamic")
	objects.enemy.shape = love.physics.newRectangleShape(0, 0, 50, 100)
	objects.enemy.fixture = love.physics.newFixture(objects.enemy.body, objects.enemy.shape, 2)
	
	objects.enemy2 = {}
	
	objects.enemy2.body = love.physics.newBody(world, 650/3, 650/5, "dynamic")
	objects.enemy2.shape = love.physics.newRectangleShape(0, 0, 50, 100)
	objects.enemy2.fixture = love.physics.newFixture(objects.enemy2.body, objects.enemy2.shape, 2)
	
	objects.enemy3 = {}
	
	objects.enemy3.body = love.physics.newBody(world, 600, 650/5, "dynamic")
	objects.enemy3.shape = love.physics.newRectangleShape(0, 0, 50, 100)
	objects.enemy3.fixture = love.physics.newFixture(objects.enemy3.body, objects.enemy3.shape, 2)

	font = love.graphics.newFont(20)

	recSaved = 0
	
	thetime = 0
	
	
--graphic setup
love.graphics.setBackgroundColor(104, 136, 248)-- set background color to blue
love.graphics.setMode(650, 650, false, true, 0)-- window dimensions to 650 by 650, no full screen, vsync on, no antialiasing
end


function love.update(dt)
	world:update(dt)
	
	if objects.enemy.body:getY() > 770 then
		newSpawnX = math.random(50, 600)
		
		if newSpawnX > 100 then
			newSpawnX2 = math.random(50 + 100, 600)
		elseif newSpawnX < 550 then
			newSpawnX2 = math.random(50, 450)
		else 
			newSpawnX2 = math.random(50, 600)
		end
		
		newSpawnX3 = math.random(50, 600)
		
		objects.enemy.body:setPosition(newSpawnX, 650/5)
		objects.enemy.body:setLinearVelocity(0,0)
		
		objects.enemy2.body:setPosition(newSpawnX2, 650/5)
		objects.enemy2.body:setLinearVelocity(0,0)
		
		
		objects.enemy3.body:setPosition(newSpawnX3, 650/5)
		objects.enemy3.body:setLinearVelocity(0,0)
		
	end
end


function love.keypressed(key)
	if key == "down" then
		recSaved = recSaved + 1
		newSpawnX = math.random(50, 600)
		newSpawnX2 = math.random(50, 600)
		newSpawnX3 = math.random(50, 600)
		objects.enemy.body:setPosition(newSpawnX, 650/5)
		objects.enemy.body:setLinearVelocity(0,0)
		
		objects.enemy2.body:setPosition(newSpawnX2, 650/5)
		objects.enemy2.body:setLinearVelocity(0,0)
		
		objects.enemy3.body:setPosition(newSpawnX3, 650/5)
		objects.enemy3.body:setLinearVelocity(0,0)
		
	end
end

function love.mousepressed()

	
end

function love.draw()
	love.graphics.setColor(72, 260, 14)--set drawing color for ground to green
	love.graphics.polygon("fill", objects.enemy.body:getWorldPoints(objects.enemy.shape:getPoints()))
	
	love.graphics.setColor(72, 260, 14)--set drawing color for ground to green
	love.graphics.polygon("fill", objects.enemy2.body:getWorldPoints(objects.enemy2.shape:getPoints()))
	
	love.graphics.setColor(72, 260, 14)--set drawing color for ground to green
	love.graphics.polygon("fill", objects.enemy3.body:getWorldPoints(objects.enemy3.shape:getPoints()))
	
	love.graphics.setColor(0, 255, 0, 255)
	love.graphics.setFont(font)
	love.graphics.print("Rectangles Save: " .. recSaved, 25, 25)
	
	
	love.graphics.setColor(72, 260, 14)
	love.graphics.setFont(font)
	love.graphics.print("Timer: " .. thetime, 500, 25)
end
Any help or advice would be helpful, thank you.
Attachments
Brick fall.love
My game so far.
(1.13 KiB) Downloaded 109 times
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Need help finding an algorithm to see of click is on a b

Post by Sheepolution »

I'm not sure what you're trying to do. But if you want to check if you're clicking on a block then we need to know the following things:

Is the x-position of the mouse further to the right, than the left side of the object?
Is the x-position of the mouse further to the left, than the right side of the object?
Is the Y-position of the mouse further to the bottom than the top side of the object?
Is the Y-position of the mouse further to the top than the bottom side of the object?

If yes, then that means your mouse is inside the object, and thus you're clicking it.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests