Here’s an example of a physics ROPE joint that I have. Touch near the rope ends to move them. You’re not exactly moving the rope ends, you’re just altering their speed and direction. So depending on the tension of the rope, sometimes the end will move easily, sometimes it takes more force to move it where you want. Once you lift your finger, the rope end will stay at that position. It’s kind of interesting to loop the rope and watch it react.
displayMode(FULLSCREEN)
function setup()
t1,t2=0,0 -- id for rope ends
tab={} -- table of physics bodies
jnt={} -- table of joints
j=200 -- number of joints
len=(WIDTH-20)/j -- length between joints
for z=1,j do -- create bodies
a = physics.body(CIRCLE,0)
a.x = 10+z*len
a.y = HEIGHT-100
if z==1 or z==j then
a.type=STATIC -- lock end points of rope
end
table.insert(tab,a) -- add bodies to table
if z>1 then -- create joint information and put in table
table.insert(jnt,physics.joint(ROPE,tab[z-1],tab[z],tab[z-1].position,
tab[z].position,5))
end
end
end
function draw()
background(40, 40, 50)
fill(255)
text("Physics Joint (Rope)",WIDTH/2,HEIGHT-50)
text("Touch near a rope end to move it.",WIDTH/2,HEIGHT-80)
strokeWidth(2)
for z=2,#tab do
stroke(255)
if z%2==0 then -- alternate white and red colors
stroke(255,0,0)
end
line(tab[z].x,tab[z].y,tab[z-1].x,tab[z-1].y) -- draw line between joints
end
noStroke()
fill(255,0,0)
ellipse(tab[1].x,tab[1].y,10) -- draw circles for rope end
fill(0,255,0)
ellipse(tab[j].x,tab[j].y,10)
end
function touched(t)
if t.state==BEGAN then
-- find which rope end is closest to the touch
v1=vec2(t.x,t.y)
d1=v1:dist(vec2(tab[1].x,tab[1].y)) -- distance to red end
d2=v1:dist(vec2(tab[j].x,tab[j].y)) -- distance to green end
if d2<d1 then
t2=t.id -- set to move green end
elseif t1==0 then
t1=t.id -- set to move red end
end
end
if t.state==MOVING then -- move red or green end
if t.id==t1 then -- red end
tab[1].type=DYNAMIC -- make end movable
-- change linear velocity of red end
tab[1].linearVelocity=vec2(t.deltaX*200,t.deltaY*200)
end
if t.id==t2 then -- green end
tab[j].type=DYNAMIC -- make end movable
-- change linear velocity of green end
tab[j].linearVelocity=vec2(t.deltaX*200,t.deltaY*200)
end
end
if t.state==ENDED then -- lock ends to current positions
if t.id==t1 then
tab[1].type=STATIC -- make end stop
t1=0
else
tab[j].type=STATIC -- make end stop
t2=0
end
end
end