Backward Kinematic

Advanced game related topics
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Backward Kinematic

Post by Fig »

Feel free to change #nbBones and #lengthbone.
Dispite the look, it has nothing to do with the double pendulum. For exemple, "this" is use to move a robotic arms by its extremity.

Code: Select all

#x=600:#y=400
#nbBones=5
#lengthBone=50
#FixX=#X/2:#FixY=#Y/2

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0 Or OpenWindow(0, 0, 0, #X, #Y, "Backward kinematic", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)=0 Or OpenWindowedScreen(WindowID(0),0,0,#X,#Y,0,0,0,#PB_Screen_WaitSynchronization)=0
    MessageRequester("Error", "Can't open the sprite system", 0)
    End
EndIf
Structure Bone
    x1.i
    y1.i
    x2.i
    y2.i
    length.i
    Angle.f
EndStructure
NewList Bone.Bone()

Procedure AddBone(List Bone.Bone(),length.i,angle.f)
    If ListIndex(Bone())=-1
        AddElement(Bone())
        Bone()\x1=#FixX
        Bone()\y1=#FixY
    Else
        xold.i=Bone()\x2
        yold.i=Bone()\y2
        AddElement(Bone())
        Bone()\x1=xold
        Bone()\y1=yold
    EndIf
    Bone()\Angle=angle:Bone()\length=length
    Bone()\x2=Bone()\x1+Bone()\length*Cos(angle)
    Bone()\y2=Bone()\y1+Bone()\length*Sin(angle)
EndProcedure

Procedure follow(List Bone.Bone(),targetX.i,targetY.i)
    Bone()\Angle=ATan2(targetX-Bone()\x1,targetY-Bone()\y1)
    Bone()\x2=Bone()\x1+Bone()\length*Cos(Bone()\Angle)
    Bone()\y2=Bone()\y1+Bone()\length*Sin(Bone()\Angle)
    a.i=Bone()\x2-Bone()\x1
    b.i=Bone()\y2-Bone()\y1
    Bone()\x2=targetx
    Bone()\y2=targety
    Bone()\x1=targetx-a
    Bone()\y1=targety-b
EndProcedure

Procedure Update(List Bone.Bone(),xold.i,yold.i)
    If LastElement(Bone())
        Repeat
            follow(Bone(),xold,yold)
            xold=Bone()\x1
            yold=Bone()\y1
        Until PreviousElement(Bone())=0
        ;shift back to fix point
        deltax.i=Bone()\x1-#FixX
        deltay.i=Bone()\y1-#FixY
        ForEach Bone()
            Bone()\x1-deltax
            Bone()\y1-deltay
            Bone()\x2-deltax
            Bone()\y2-deltay
        Next
    EndIf
EndProcedure

;create some bones...
For i=1 To #nbBones
    AddBone(Bone(),#lengthBone,Radian(-90))
Next i

Dx.i=2:Dy.i=2:x.i=Random(#X):y.i=Random(#Y)
Repeat
    FlipBuffers()
    ClearScreen(RGB(0,0,0))
    ExamineKeyboard()
    ;bouncing box
    If x>#x Or x<0:Dx=-Dx:EndIf
    If y>#y Or y<0:Dy=-Dy:EndIf
    x+Dx:y+Dy
    
    Update(Bone(),x,y)
    StartDrawing(ScreenOutput())
    Circle(x,y,10,#Red)
    ForEach Bone()
        LineXY(Bone()\x1,Bone()\y1,Bone()\x2,Bone()\y2,#Blue)
        Circle(Bone()\x2,Bone()\y2,2,#White)
    Next    
    Circle(#FixX,#FixY,3,#Green)
    StopDrawing()
    Repeat:Event = WindowEvent():If event= #PB_Event_CloseWindow:End:EndIf:Until event=0
Until KeyboardPushed(#PB_Key_Escape)
Last edited by Fig on Sat Jul 07, 2018 11:49 am, edited 5 times in total.
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: Backward Kinematic

Post by VB6_to_PBx »

Awesome !!! thanks Fig

just 1 question :

is there any way to close the Window
by clicking the Close Button ???

i see the Close Button reacts to my MouseOver
but i've never seen a Post or Thread about closing a Window by clicking the Close Button for a 3D Screen ??

what's causing this ??
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Backward Kinematic

Post by Fig »

No, there is no way... ^^ Please be nice and press [Escape] :P

Just kidding, it has been done. :wink:

Edits: Few edits to change some graphic's details.
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
zefiro_flashparty
User
User
Posts: 74
Joined: Fri Mar 04, 2005 7:46 pm
Location: argentina

Re: Backward Kinematic

Post by zefiro_flashparty »

I have to learn to do that, :)
to be able to simulate a robot arm or foot
Arming the skeleton, fixing the maximum angles of the joints.
and see what kind of movements are layers to perform
then I arm a mascot with arduino :D
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 8)
Post Reply