Need code for an object swinging on a line

Blitz3D Forums/Blitz3D Programming/Need code for an object swinging on a line

Hey guys

Do any of you have some code for an object hanging
on a piece of line and swinging sideways and in circles.

I need this for my next project Adventure fishing 3.
A fly fishing game.

Thanks
3DFish

On the one hand, that's a pretty vague request. Normally I would ask for clarification on exactly what you want. On the other hand, the fact that this is for a fishing game makes me assume the object is a fish pulling on the line and jerking around.

This isn't necessarily an ideal approach, but a simple trick would be to create a pivot at the end of the pole (ie. where the line is coming from) and parent the fish to that pivot. Now if you rotate the pivot, the fish will be orbiting around that point.

Verlet constraints?

Thanks

The line is fly line hanging from the tip of a fly rod.
A fly will be attached to the end of the line.
I need a swinging effect (the fly).

My sin,cos,tan maths are a bit on the shaky side.

"swinging" is actually the part I wanted clarification on. Are you talking about the movement of the fly during casting?

no, only the movement of the fly when holding the rod before a cast.

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	



Wow Jeppe!

That's really neat..

I'm looking for something similar, only in 3D.
A rod tip (pivot).
More line pivots hanging from the rod tip.
Each line pivot should affect the other.

The line pivots should only start moving when the rod tip moves.

The line pivots needs to swing in the direction in which the rod tip moves.

Wow! Really impressive piece of code, i like it! congratulations!

Wow!

Fab stuff, thanks for sharing that!

IPete2.

Thanks for all the input guys.

I have found a solution.

Check out this thread by Mr. Picklesworth
http://www.blitzbasic.com/Community/posts.php?topic=39525&hl=rope