Strings / Ropes, Etc.

Blitz3D Forums/Blitz3D Programming/Strings / Ropes, Etc.

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hey All,

I'm working on a rope / string simulation based on hugo elias's model for a string, found here...

[link]http://freespace.virgin.net/hugo.elias/models/m_string.htm[/link]

It seems to be working o.k., but there are a few things about it which i don't like, mainly the fact that it seems to continuously relax from gravity. It also is a little bit too flexible. Does anyone have any ideas for improvement, or other models?

thanks,

Roland

Graphics3D 640,480,0,2
Global cam = CreateCamera()

PositionEntity cam,0,10,-40
l=CreateLight()


Type Node
	Field entity
	Field x#
	Field y#
	Field xspeed#
	Field yspeed#
	Field num
	Field newx#
	Field newy#
	Field dragging
End Type

Global gravity# = -.15
Global damping# = .9
Global length# = 2
Global endnode
Global startnode

Global mag1#
Global mag2#
Global extension1#
Global extension2#
Global xvector1#
Global xvector2#
Global yvector1#
Global yvector2#
Global xv#
Global yv#

Global picker = CreateCube()
ScaleEntity picker,100,100,.2
EntityAlpha picker,.2
EntityPickMode picker,2


For x=0 To 31

	n.Node = New Node
	n\entity = CreateSphere(3)
	ScaleEntity n\entity,.75,.75,.75
	n\x = 0
	n\y = Float(x)
	n\num = x
	
	PositionEntity n\entity,n\x,n\y,0
	
	If x = 0 Then endnode = Handle(n)
	
	If x = 31 Then startnode = Handle(n)
	
	EntityPickMode n\entity,2

Next



While Not KeyHit(1)


	updatenodes()

	If MouseHit(1)
		CameraPick cam,MouseX(),MouseY()
		For n.Node = Each Node
		
			If PickedEntity() = n\entity
				n\dragging = 1
				EntityColor n\entity,255,0,0
			Else
				n\dragging = 0
				EntityColor n\entity,255,255,255
			EndIf
		Next
	EndIf
	
	If MouseDown(1)
		CameraPick cam,MouseX(),MouseY()
		For n.Node = Each Node
			If n\dragging = 1
				n\x = PickedX()
				n\y = PickedY()
			EndIf
		
		Next
	EndIf



UpdateWorld
 
RenderWorld

n.Node = First Node
Text 0,0,n\x

Flip



Wend


End


Function dist#(xlen#,ylen#)

	Return Sqr((xlen^2)+(ylen^2))


End Function



Function updatenodes()

	For n.Node = Each Node
	
		hasno1 = 0
		hasno2 = 0
	
		For no.Node = Each Node
		
			If (no\num = n\num-1)
			
				xvector1 = no\x-n\x
				yvector1 = no\y-n\y
				mag1 = dist#(xvector1,yvector1)
				extension1 = mag1-length
			
				hasno1 = 1
			
			Else If (no\num = n\num+1)
	
				xvector2 = no\x-n\x
				yvector2 = no\y-n\y
				mag2 = dist#(xvector2,yvector2)
				extension2 = mag2-length
				hasno2 = 1
			
			EndIf
		
		Next
		
		If hasno1 <> 1
		
			xvector1 = 0
			yvector1 = 0
			mag1 = 1
			extension1 = 0			
		
		EndIf

		If hasno2 <> 1
		
			xvector2 = 0
			yvector2 = 0
			mag2 = 1
			extension2 = 0			
		
		EndIf

		
		xv = (xvector1/mag1*extension1)+(xvector2/mag2*extension2)	

		yv = (yvector1/mag1*extension1)+(yvector2/mag2*extension2)+gravity
		
		n\xspeed = n\xspeed*damping+(xv*.07)
		n\yspeed = n\yspeed*damping+(yv*.07)

		n\newx = n\x+n\xspeed
		n\newy = n\y+n\yspeed

		If Handle(n) = startnode
			n\newx = 0
			n\newy = 31
		EndIf

		
	Next


	For n.Node = Each Node
		
		n\x = n\newx
		n\y = n\newy
		

		
		If Handle(n) = startnode
			n\x = 0
			n\y = 31
		EndIf
		
		PositionEntity n\entity,n\x,n\y,0	
	
	Next


End Function