Code archives/Miscellaneous/Tentacle like arm
This code has been declared by its author to be Public Domain code.
Download source code
| Taken from a processing example "Reach 3" witch again is: Based on code from Keith Peters (www.bit-101.com) SuperStrict Framework BRL.GlMax2D Import BRL.Random AppTitle = "racher3 example from processing. Keith Peters (www.bit-101.com)" Global resX:Int=640, resY:Int=480 SetGraphicsDriver GLMax2DDriver() Graphics resX,resY,0,0 Local frames:Int=0 Local fps:Int=0 Local sec:Long = MilliSecs() 'Create a bunch of reachers and set them up: Local reachers:reacher[] = New reacher[7] For Local i%=0 Until 7 Local segments% = Rand( 10, 20 ) Local segLength% = Rand( 20, 40 ) Local tmp:reacher = New reacher tmp.setup( segments, segLength, 100*i, resY ) reachers[i] = tmp Next 'create some bounce balls Local myball1:ball = New ball myball1.dirX = 1 Local myball2:ball = New ball myball1.dirX = -1 SetBlend( LIGHTBLEND ) SetClsColor 127,127,127 SetAlpha .5 While(Not KeyHit(KEY_ESCAPE) And Not AppTerminate() ) SetColor 255,0,0 myball1.draw() SetColor 255,255,0 myball2.draw() myball1.update() myball2.update() 'reach mouse SetColor 0,200,0 For Local i%=0 Until 5 reachers[i].setTarget( MouseX(), MouseY() ) reachers[i].update() Next 'reach ball1 SetColor 100,0,0 reachers[5].setTarget( myball1.x, myball1.y ) reachers[5].update() 'reach ball2 SetColor 100,100,0 reachers[6].setTarget( myball2.x, myball2.y ) reachers[6].update() DrawText("fps "+fps,resX- 100,0) If(sec < MilliSecs() ) sec = MilliSecs() + 1000 fps = frames frames = 0 EndIf frames :+1 Flip Cls Delay 16 ' frame limit Wend End Type ball Field x:Float, y:Float Field dirX:Float, dirY:Float Field size:Float Method New() x=100 y=100 dirX = 5 dirY = -5 size = 30 End Method Method draw() DrawOval x-size/2,y-size/2,size, size End Method Method update() x = x + 1.0 * dirX y = y + 0.8 * dirY If( x > (resX - size) Or x < size ) Then dirX :* -1 If( y > (resY - size) Or y < size ) Then dirY :* -1 End Method End Type Type reacher Field numSegments:Int Field x:Float[] Field y:Float[] Field angle:Float[] Field segLength:Float Field targetX:Float, targetY:Float Method setup( setNumSegments:Int, setSeglength#, xpos#, ypos# ) numSegments = setNumSegments x = New Float[numSegments] y = New Float[numSegments] angle = New Float[numSegments] segLength = setSeglength x[x.length-1] = xpos y[x.length-1] = ypos End Method Method setTarget( tx:Float, ty:Float ) targetX = tx targetY = ty End Method Method update() For Local i:Int=0 Until numSegments reachSegment(i, targetX, targetY ) Next For Local i:Int=x.length-1 Until 0 Step -1 positionSegment(i, i-1 ) Next For Local i:Int=0 Until x.length segment( x[i], y[i], angle[i], (i+1)*2 ) Next End Method Method positionSegment( a:Int, b:Int ) x[b] = x[a] + Cos(angle[a]) * segLength y[b] = y[a] + Sin(angle[a]) * segLength End Method Method reachSegment( i:Int, xin:Float, yin:Float) Local dx:Float = xin - x[i] Local dy:Float = yin - y[i] angle[i] = ATan2(dy, dx) targetX =( xin - Cos(angle[i]) * segLength ) targetY =( yin - Sin(angle[i]) * segLength ) End Method Method segment( x:Float, y:Float, angle:Float, sw:Float ) SetLineWidth sw SetOrigin x, y SetRotation angle DrawLine 0, 0, segLength, 0 SetRotation 0 SetOrigin 0,0 End Method End Type |
above in codebox |