Rope code

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
User avatar
Raz
Prole
Posts: 3
Joined: Thu Jul 22, 2010 1:57 pm

Rope code

Post by Raz »

So, I've quickly thrown together the following code, I am expecting the rope to hang from 20,20, which it tries to do, but it appears the weight of the rope becomes too much for the joint between circle 0 and 1.

I'm very new to this, so can anyone tell me what I am missing? :)

Thanks
(code below)

Code: Select all

function love.load()
  world = love.physics.newWorld(650, 650) --create a world for the bodies to exist in with width and height of 2,000
  world:setGravity(0, 700) --the x component of the gravity will be 0, and the y component of the gravity will be 700
  world:setMeter(64) --the height of a meter in this world will be 64px
  
  bodies = {} --create tables for the bodies and shapes so that the garbage collector doesn't delete them
  shapes = {}
  joints = {}

  max_balls = 40

	for i=0,max_balls,1 do
		bodies[i] = love.physics.newBody(world, 20+(i*20), 20, 1, 0) --remember, the body anchors from the center of the shape
		shapes[i] = love.physics.newCircleShape(bodies[i], 0, 0, 5) --the ball has a shape of 20
		if i > 0 then
			joints[i] = love.physics.newDistanceJoint( bodies[(i-1)], bodies[(i)], 20+((i-1)*20), 20, 20+((i)*20), 20 )
		end

	end

	joints[0] = love.physics.newMouseJoint( bodies[0], 20, 20 ) -- We set the first joint as a set point

  --initial graphics setup
  love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
  love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650
end

function love.update(dt)
	world:update(dt) --this puts the world into motion
end

function love.draw()
	for i=0,max_balls,1 do
		love.graphics.circle("fill", bodies[i]:getX(), bodies[i]:getY(), shapes[i]:getRadius(), 5)
	end
end
User avatar
bartoleo
Party member
Posts: 118
Joined: Wed Jul 14, 2010 10:57 am
Location: Savigliano

Re: Rope code

Post by bartoleo »

you can set zero mass to first body...
bodies[0]:setMass(0,0,0,0)

and clear:
joints[0]
Bartoleo
User avatar
Raz
Prole
Posts: 3
Joined: Thu Jul 22, 2010 1:57 pm

Re: Rope code

Post by Raz »

Much appreciated :) thank you
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest