It is currently Thu Jul 02, 2020 7:49 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Backward Kinematic
PostPosted: Fri Jul 06, 2018 8:51 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Apr 30, 2009 5:23 pm
Posts: 328
Location: Côtes d'Azur, France
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:
#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)

_________________
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS


Last edited by Fig on Sat Jul 07, 2018 11:49 am, edited 5 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Backward Kinematic
PostPosted: Sat Jul 07, 2018 6:04 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon May 09, 2011 9:36 am
Posts: 507
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Backward Kinematic
PostPosted: Sat Jul 07, 2018 6:11 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Apr 30, 2009 5:23 pm
Posts: 328
Location: Côtes d'Azur, France
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Backward Kinematic
PostPosted: Fri Sep 07, 2018 9:02 pm 
Offline
User
User
User avatar

Joined: Fri Mar 04, 2005 7:46 pm
Posts: 73
Location: argentina
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)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye