Hi, I was messing around the other night practicing my maths work, etc., and I came up with this almost by accident:
Now, I have *no idea* if this is the 'proper' way of simulating this sort of thing, I just worked it out as best I could. I can't help feeling that I can improve on this though. Is there anybody who might be able to give me any pointers as to how I might improve on this?
the biggest problem I have at the moment is that the links 'concertina' up when the gravity is set below a certain amount. I don't want that to happen basically, I just want the rope to move a little more realistically.
EDIT: Just edited out a couple of lines and altered a couple.
EDIT 2: Ha! That's seems to have done the trick. Well, sort of.
Graphics 640, 400, 32, 1 SetBuffer BackBuffer() Type dat Field x#, y# Field h#, v# End Type Global ball.dat ; Create 100 links in the chain For n=0 To 99 ball.dat = New dat ball\x = Rand(GraphicsWidth()) ball\y = Rand(GraphicsHeight()) Next Repeat Cls ; Draw blue oval for the mouse Color 0, 0, 255 Oval MouseX()-5, MouseY()-5, 10, 10 ; set coords for first link in chain to current mouse coords lastx# = MouseX() lasty# = MouseY() lastv# = MouseYspeed() lasth# = MouseXspeed() For ball.dat = Each dat ball\v = ball\v + .5 ; Increase Y velocity by 1 to simulate gravity ; ball\h = ball\h + (lastx - ball\x) * .05 ; Calcuate current velocity by comparing current link to previous links positions ; ball\v = ball\v + (lasty - ball\y) * .05 ball\v = (ball\v + lastv) * .5 ; Average the velocities between the current link and the previous link ball\h = (ball\h + lasth) * .5 ball\v = ball\v * .95 ; Apply friction from air resistance ball\h = ball\h * .95 ball\x = ball\x + ball\h ; Move link in the new direction ball\y = ball\y + ball\v ; Stop chain link from dropping below screen or off to the sides If ball\y > GraphicsHeight() - 10 Then ball\y = GraphicsHeight() - 10: ball\v = -ball\v If ball\x < 0 Then ball\x = 0: ball\h = - ball\h If ball\x > GraphicsWidth() Then ball\x = GraphicsWidth(): ball\h = -ball\h ; Calculate the distance between current & previous links xdiff# = lastx - ball\x ydiff# = lasty - ball\y dist# = Sqr(xdiff * xdiff + ydiff * ydiff) ; Calculate the vector normal of where the link should be If Abs(dist#) > 0 Then xd# = (lastx - ball\x) / dist# yd# = (lasty - ball\y) / dist# Else xd# = 0 yd# = 0 End If ; Set the distance between current link and previous link xd = xd * 5 yd = yd * 5 ; Calculate new position ball\x = (lastx - xd) ball\y = (lasty - yd) ; Draw the link Color 255, 0, 0 Oval ball\x-2, ball\y-2, 4, 4 Line lastx, lasty, ball\x, ball\y ; Save current links coords and velocities lastx# = ball\x lasty# = ball\y lasth# = ball\h lastv# = ball\v Next Flip Until KeyHit(1) End
Now, I have *no idea* if this is the 'proper' way of simulating this sort of thing, I just worked it out as best I could. I can't help feeling that I can improve on this though. Is there anybody who might be able to give me any pointers as to how I might improve on this?
the biggest problem I have at the moment is that the links 'concertina' up when the gravity is set below a certain amount. I don't want that to happen basically, I just want the rope to move a little more realistically.
EDIT: Just edited out a couple of lines and altered a couple.
EDIT 2: Ha! That's seems to have done the trick. Well, sort of.