Page 1 of 4

trying to make a click and drag curling rock demo

Posted: Mon Jan 14, 2013 7:39 am
by Sp1k3
I feel like a total n00b but the debugger freaks on line 26 and I can't seem to make a draggable curling rock. I want zero gravity with some friction which I think that I got right but any help on the user interaction would AWESOME! :)

Code: Select all

function love.load()
--initial graphics setup
  love.graphics.setMode(1280, 720, false, true, 8) --set the window dimensions to 720p
  love.physics.setMeter(64)
  world = love.physics.newWorld(0, 0, true)
  objects = {}
  objects.rock = {}
  objects.rock.body = love.physics.newBody(world, 608, 650, "dynamic")
  objects.rock.shape = love.physics.newCircleShape(32)
  objects.rock.fixture = love.physics.newFixture(objects.rock.body, objects.rock.shape, 1)
  objects.rock.fixture:setRestitution(0.9)
  rock = love.graphics.newImage("rock.png")
  love.graphics.setBackgroundColor(25, 25, 25)
  mouse = love.mouse

end

function love.update(dt)
  world:update(dt)
if drag then
      objects.rock.body:setPosition(mouse.getPosition())
   end
end

function love.mousepressed(x,y,button)
   if button == "l" then
      if objects.rock.shape:testPoint(mouse.getPosition()) then
         drag = true
         objects.rock.body:setMass(0,0,0,0)
      end
   end
end
function love.mousereleased()
   if drag then
      drag = false
      ojects.rock.body:setLinearVelocity(0,0)
      objects.rock.body:setMassFromShapes()
   end
end


function love.draw()
  love.graphics.draw(rock, objects.rock.body:getX(), objects.rock.body:getY(), objects.rock.shape:getRadius())
end

Re: trying to make a click and drag curling rock demo

Posted: Mon Jan 14, 2013 8:32 pm
by Yell0w
In the wiki there is a comment that the property you are trying to use is not possible in love 0.8.0
(https://love2d.org/wiki/Shape:testPoint)
maybe you can solve it differently? for example when you click the stone you can select an angle with the mouse, and the power applied is increased the further away from the stone your mouse pointer is?
I added a bit of .love to help you a bit further

Re: trying to make a click and drag curling rock demo

Posted: Mon Jan 14, 2013 10:59 pm
by Sp1k3
Awesome man that really helped and I've made a bit more progress, which I've attached below. So basically now I think that I should be able to set the ice as physics variable to apply a friction joint between it and the rock(s), which will eventually be entities. :)


Code: Select all

function love.load()
--initial graphics and physics setup
  love.graphics.setMode(1280, 720, false, true, 8) --sets the window dimensions to 720p
  love.physics.setMeter(64)
  world = love.physics.newWorld(0, 0, true)
  objects = {}
  objects.ice = {}
  objects.ice.body = love.physics.newBody(world, 0, 0, "static")
  objects.rock = {}
  objects.rock.body = love.physics.newBody(world, 64, 310, "dynamic")
  objects.rock.shape = love.physics.newCircleShape(32)
  objects.rock.fixture = love.physics.newFixture(objects.rock.body, objects.rock.shape, 1)
  objects.rock.fixture:setRestitution(0.9)
  rock = love.graphics.newImage("rock.png")
  ice = love.graphics.newImage("ice.png")
  mouse = love.mouse
  stonepower = 0
  love.graphics.setCaption ("Love Curl Tech Demo")
  curl = 0 --going to make this a sine or cosine (depending on in or out turn) function with a period of 1280
  joint = love.physics.newFrictionJoint( objects.ice.body, objects.rock.body, 0, 0, 1280, 720, true )
  force = joint:setMaxForce(1)
  torque = joint:setMaxTorque(1)

end

function love.update(dt)
  world:update(dt) --updates every frame (important)
	if drag then
      objects.rock.body:setPosition(love.mouse.getX()-32,love.mouse.getY()-64)
   end
   if love.mouse.isDown("l") then
        stonepower = stonepower + (dt*600)  -- increases the variable by 60 for every second the button is held down
    end 
end

function love.mousepressed(x,y,button)
   if button == "l" then
		if drag == false then
			MousePositionSaved = { x= love.mouse.getX(), y=love.mouse.getY() }
		end
	drag = true
	
    --  if objects.rock.shape:testPoint(mouse.getPosition()) then
     --    objects.rock.body:setMass(0,0,0,0)
   --   end
   end
end
function love.mousereleased()
	if drag then
		drag = false
		--	ojects.rock.body:setLinearVelocity(0,0)
		--	objects.rock.body:setMassFromShapes()
	   objects.rock.body:setLinearVelocity(0, 0)
	   objects.rock.body:setAngularVelocity(curl)
	   objects.rock.body:applyForce((stonepower*10)*1, 0 )
	   stonepower = 0
	end
end


function love.draw()
  love.graphics.draw(ice, 0, 0, 0)
  love.graphics.draw(rock, objects.rock.body:getX(), objects.rock.body:getY(), objects.rock.shape:getRadius()) --eventually I want to set a shader to change the colour warmness of the rock based on stonepower
end

Re: trying to make a click and drag curling rock demo

Posted: Tue Jan 15, 2013 12:02 am
by Sp1k3
I'm not sure if I'm using that friction joint correctly, the velocity of the rock doesn't slow

Re: trying to make a click and drag curling rock demo

Posted: Tue Jan 15, 2013 3:22 pm
by Yell0w
Another crude way of doing it is to deaccelerate it over time by applying a force in the opposite direction untill it reaches zero or something.

Re: trying to make a click and drag curling rock demo

Posted: Tue Jan 15, 2013 7:37 pm
by Sp1k3
Yell0w wrote:Another crude way of doing it is to deaccelerate it over time by applying a force in the opposite direction untill it reaches zero or something.

The problem I've had with that is detecting if the stone is in motion, if I do it on l_mouse release it just cancels out some of the force and I'm assuming if I apply it after that function it will keep applying the force until the rock reverses on go past 0 on the x-axis.

*edit*

also if I do force = joint:setMaxForce(50) when I release the mouse the rock then starts to move + or - along the y-axis

friction doesn't seem to work

Code: Select all

function love.load()
--initial graphics and physics setup
  love.graphics.setMode(1280, 720, false, true, 8) --sets the window dimensions to 720p
  love.physics.setMeter(64)
  world = love.physics.newWorld(0, 0, 1280, 720, 100, 100, true )
  objects = {}
  objects.ice = {}
  objects.ice.body = love.physics.newBody(world, 0, 0, "dynamic")
  objects.ice.shape = love.physics.newRectangleShape(1280, 720)
  objects.rock = {}
  objects.rock.body = love.physics.newBody(world, 64, 310, "dynamic")
  objects.rock.shape = love.physics.newCircleShape(32)
  objects.rock.fixture = love.physics.newFixture(objects.rock.body, objects.rock.shape, 1)
  objects.rock.fixture:setRestitution(0)
  rock = love.graphics.newImage("rock.png")
  ice = love.graphics.newImage("ice.png")
  mouse = love.mouse
  stonepower = 0
  love.graphics.setCaption ("Love Curl Tech Demo")
  curl = 0 --going to make this a sine or cosine (depending on in or out turn) function
  joint = love.physics.newFrictionJoint( objects.ice.body, objects.rock.body, 0, 0, 1280, 720, true )
  force = joint:setMaxForce(1)
end

function love.update(dt)
  world:update(dt) --updates every frame (important)
	if drag then
      objects.rock.body:setPosition(love.mouse.getX()-32,love.mouse.getY()-64)
   end
   if love.mouse.isDown("l") then
        stonepower = stonepower + (dt*1000)  -- increases the variable by 100 for every second the button is held down
    end 
end

function love.mousepressed(x,y,button)
   if button == "l" then
		if drag == false then
			MousePositionSaved = { x= love.mouse.getX(), y=love.mouse.getY() }
		end
	drag = true
	
    --  if objects.rock.shape:testPoint(mouse.getPosition()) then
     --    objects.rock.body:setMass(0,0,0,0)
   --   end
   end
end
function love.mousereleased()
	if drag then
		drag = false
		--	ojects.rock.body:setLinearVelocity(0,0)
		--	objects.rock.body:setMassFromShapes()
	   objects.rock.body:setLinearVelocity(0, 0)
	   objects.rock.body:setAngularVelocity(0)
	   objects.rock.body:applyForce((stonepower*10), 0 )
	   stonepower = 0
	end
end  


function love.draw()
  love.graphics.draw(ice, 0, 0, 0)
  love.graphics.draw(rock, objects.rock.body:getX(), objects.rock.body:getY(), objects.rock.shape:getRadius()) --eventually I want to set a shader to change the colour warmness of the rock based on stonepower
end

Re: trying to make a click and drag curling rock demo

Posted: Tue Jan 15, 2013 11:04 pm
by Yell0w
maybe you could do a check on the stones velocity before allowing to drag it (a second time)
https://love2d.org/wiki/Body:getLinearVelocity might be of help

Re: trying to make a click and drag curling rock demo

Posted: Tue Jan 15, 2013 11:37 pm
by Sp1k3
Yell0w wrote:maybe you could do a check on the stones velocity before allowing to drag it (a second time)
https://love2d.org/wiki/Body:getLinearVelocity might be of help

So something like this:

Code: Select all

function love.load()
--initial graphics and physics setup
  love.graphics.setMode(1280, 720, false, true, 8) --sets the window dimensions to 720p
  love.physics.setMeter(64)
  world = love.physics.newWorld(0, 0, 1280, 720, 100, 100, true )
  objects = {}
  objects.ice = {}
  objects.ice.body = love.physics.newBody(world, 0, 0, "dynamic")
  objects.ice.shape = love.physics.newRectangleShape(1280, 720)
  objects.rock = {}
  objects.rock.body = love.physics.newBody(world, 64, 310, "dynamic")
  objects.rock.shape = love.physics.newCircleShape(32)
  objects.rock.fixture = love.physics.newFixture(objects.rock.body, objects.rock.shape, 1)
  objects.rock.fixture:setRestitution(0)
  rock = love.graphics.newImage("rock.png")
  ice = love.graphics.newImage("ice.png")
  mouse = love.mouse
  stonepower = 0
  love.graphics.setCaption ("Love Curl Tech Demo")
  curl = 0 --going to make this a sine or cosine (depending on in or out turn) function
  joint = love.physics.newFrictionJoint( objects.ice.body, objects.rock.body, 0, 0, 1280, 720, true )
  force = joint:setMaxForce(1)
end

function love.update(dt)
  world:update(dt) --updates every frame (important)
	if drag then
      objects.rock.body:setPosition(love.mouse.getX()-32,love.mouse.getY()-64)
   end
   if love.mouse.isDown("l") then
        stonepower = stonepower + (dt*1000)  -- increases the variable by 100 for every second the button is held down
    end 
end

function love.mousepressed(x,y,button)
   if button == "l" then
		if drag == false then
			MousePositionSaved = { x= love.mouse.getX(), y=love.mouse.getY() }
		end
	drag = true
	
    --  if objects.rock.shape:testPoint(mouse.getPosition()) then
     --    objects.rock.body:setMass(0,0,0,0)
   --   end
   end
end
function love.mousereleased()
	if drag then
		drag = false
		--	ojects.rock.body:setLinearVelocity(0,0)
		--	objects.rock.body:setMassFromShapes()
	   objects.rock.body:setLinearVelocity(0, 0)
	   objects.rock.body:setAngularVelocity(0)
	   objects.rock.body:applyForce((stonepower*10), 0 )
	   stonepower = 0
	end
  velocity = objects.rock.body:getLinearVelocity(x)
  while velocity > 0 do
	 objects.rock.body:applyForce(-(stonepower*10), 0 )
end
end  


function love.draw()
  love.graphics.draw(ice, 0, 0, 0)
  love.graphics.draw(rock, objects.rock.body:getX(), objects.rock.body:getY(), objects.rock.shape:getRadius()) --eventually I want to set a shader to change the colour warmness of the rock based on stonepower
end
if it worked?

Re: trying to make a click and drag curling rock demo

Posted: Wed Jan 16, 2013 1:04 am
by Sp1k3
also, if I set

Code: Select all

  objects.ice.fixture = love.physics.newFixture(objects.ice.body, objects.ice.shape, 1)
the rock will spawn 2/3 down the screen and friction still doesn't work

Re: trying to make a click and drag curling rock demo

Posted: Wed Jan 16, 2013 2:14 am
by Sp1k3
okay so if I set the mass of the rock and apply a tiny bit of force it will slow down VERY slowly