Just something I just coded :-)
Try to catch the box with the rod.
Graphics 800,600,16,2
SetBuffer BackBuffer()
num=25
l#=200
dl#=l/num
x#=400
For n=1 To num
If n=1
startpoint.point=pointnew(x,100,True)
p1.point=startpoint
Else
If n=num
endpoint.point=pointnew(x,100,False,5)
p1.point=endpoint
Else
p1.point=pointnew(x,100)
EndIf
contraintnew p1,p2.point
EndIf
p2.point=p1
x#=x#+dl
Next
p1.point=PointNew(200-25,100)
p2.point=PointNew(250-25,100)
p3.point=PointNew(250-25,150)
p4.point=PointNew(200-25,150)
boxhandle.point=pointnew(225-25,75,False,5)
ContraintNew p1,p2
ContraintNew p2,p3
ContraintNew p3,p4
ContraintNew p4,p1
ContraintNew p1,p3
ContraintNew p2,p4
ContraintNew p1,boxhandle
ContraintNew p2,boxhandle
rodx=400
rody=300
Repeat
Cls
If pick=0
Text 400,20,"Catch the box with the fising rod",1
Else
Text 400,20,"Box caught",1
Text 400,40,"Press mouse button to release box",1
EndIf
PointUpdate
ContraintUpdate
mdx=(MouseX()-rodx)
mdy=(MouseY()-rody)
angle=ATan2(mdy,mdx)
px=rodx+Cos(angle)*200
py=rody+Sin(angle)*200
Line rodx,rody,px,py
PointMove startpoint,px,py
Select pick
Case 0
dx#=endpoint\x-boxhandle\x
dy#=endpoint\y-boxhandle\y
If Sqr(dx*dx+dy*dy)<8
boxcontraint.contraint=contraintnew( endpoint,boxhandle)
pick=1
EndIf
Case 1
If MouseDown(1)
Delete boxcontraint
pick=2
EndIf
Case 2
dx#=endpoint\x-boxhandle\x
dy#=endpoint\y-boxhandle\y
If Sqr(dx*dx+dy*dy)>16
pick=0
EndIf
End Select
PointDraw
ContraintDraw
Flip
Until KeyDown(1)
End
Type point
Field x#,y#
Field vx#,vy#
Field lock
Field size
End Type
Function PointNew.point(x#,y#,lock=False,size=0)
p.point=New point
PointMove(p,x#,y#)
p\lock=lock
p\size=size
Return p
End Function
Function PointMove(p.point,x#,y#)
p\x=x
p\y=y
End Function
Function PointDraw()
For p.point=Each point
If p\size>0
Oval p\x-p\size,p\y-p\size,p\size+p\size,p\size+p\size,True
EndIf
Next
End Function
Function PointUpdate(dt#=0.1,gravity#=5)
For p.point=Each point
p\vy=p\vy+gravity*dt
p\vx=p\vx*0.975 ;-Sgn(p\vx)*0.5*dt
p\vy=p\vy*0.975 ;-Sgn(p\vy)*0.5*dt
If p\lock=True
p\vx=0
p\vy=0
EndIf
p\x=p\x+p\vx*dt
p\y=p\y+p\vy*dt
If p\y>GraphicsHeight()
p\vy=-p\vy*0.5
p\y=GraphicsHeight()
EndIf
Next
End Function
Function PointForce(p.point,fx#,fy#)
p\vx=p\vx+fx
p\vy=p\vy+fy
End Function
Type contraint
Field s.point
Field e.point
Field l# ;relax length of contraint
Field stiffness# ; stiffness of contraint
Field damping# ; damping of contraint
End Type
Function ContraintNew.contraint(s.point,e.point,stiffness#=18,damping#=20)
c.contraint=New contraint
c\s=s
c\e=e
dx#=(c\e\x-c\s\x)
dy#=(c\e\y-c\s\y)
c\l=Sqr(dx*dx+dy*dy)
c\stiffness#=stiffness#
c\damping#=damping#
Return c
End Function
Function ContraintUpdate(dt#=0.01,iterations=50)
For i=1 To iterations
For c.contraint=Each contraint
dx#=(c\e\x-c\s\x)
dy#=(c\e\y-c\s\y)
currentlength#=Sqr(dx*dx+dy*dy)
displacement#=currentlength-c\l
nx#=dx#/currentlength#
ny#=dy#/currentlength#
fx#=nx#*(displacement*c\stiffness)
fy#=ny#*(displacement*c\stiffness)
fz#=nz#*(displacement*c\stiffness)
fx#=fx#+nx#*c\damping#
fy#=fy#+ny#*c\damping#
PointForce c\s,fx*0.5*dt,fy*0.5*dt
PointForce c\e,-fx*0.5*dt,-fy*0.5*dt
Next
Next
End Function
Function ContraintDraw()
For c.contraint=Each contraint
Line c\s\x,c\s\y,c\e\x,c\e\y
Next
End Function