HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

IK

07-28-2007, 03:15 AM#1
PipeDream
Grim's demo inspired me to write a quick 2D IK sample.
Don't run with war3err as it bugs out lightnings (D'oh.)

Collapse JASS:
//Jacobian Transpose 2D Inverse Kinematics demo
//PipeDream
//7/27/2007
//Public domain

library IK
    struct vector2
        real x
        real y
    
        static method CreateXY takes real x, real y returns vector2
            local vector2 v = vector2.create()
            set v.x = x
            set v.y = y
            return v
        endmethod
    endstruct

    globals
        real endx
        real endy
        real enddx
        real enddy
    endglobals

    struct link
        real x0
        real y0
        real r
        real a
        real a_update
    
        lightning zap
    
        link next
    
        static method Create takes real x0, real y0, real r, real a, link next returns link
            local link l = link.create()
            set l.x0 = x0
            set l.y0 = y0
            set l.r = r
            set    l.a = a
            set l.zap = AddLightningEx("DRAL",true,x0,y0,0.,x0+r*Cos(a),y0+r*Sin(a),0.)
            set l.next = next
            return l
        endmethod
        
        method FK takes real x, real y returns nothing
            local real x1
            local real y1
            set .x0 = x
            set .y0 = y            
            
            set x1 = x + .r*Cos(.a)
            set y1 = y + .r*Sin(.a)
            
            call MoveLightningEx(.zap,true,x,y,0.,x1,y1,0.)

            if .next != 0 then
                call .next.FK(x1,y1)
            endif
        endmethod
    
        method IK takes real tx, real ty, real h returns nothing
            local real rcosa
            local real rsina
            local real r
            local real dx
            local real dy
            if .next == 0 then

                set rcosa = .r*Cos(.a)
                set rsina = .r*Sin(.a)
                
                set endx = .x0 + rcosa
                set endy = .y0 + rsina

                set enddx = tx - endx
                set enddy = ty - endy
                    
            else
                call .next.IK(tx,ty,h)
            
                set dx = endx - .x0
                set dy = endy - .y0
                set r = SquareRoot(dx*dx + dy*dy)
                
                set rcosa = r*Cos(.a)
                set rsina = r*Sin(.a)
            endif
            set .a = .a + h*(rcosa*enddy - rsina*enddx)
        endmethod
    endstruct
endlibrary

globals
    link chain
    integer n = 0
    real dt = 0.03
    unit ub
    unit ue
    real h = 0.000025
endglobals

function test_periodic takes nothing returns nothing
    local real x0 = 0.
    local real y0 = 500*Sin(0.3*dt*n)
    local real x1 = 500.*Cos(1.0*dt*n)
    local real y1 = 500.*Sin(1.0*dt*n)

    call chain.IK(x1,y1,h*dt)            // Reach towards target
    call chain.FK(x0,y0)                    // Set base position
    call SetUnitX(ub,x0)
    call SetUnitY(ub,y0)
    call SetUnitX(ue,x1)
    call SetUnitY(ue,y1)
    
    set n = n + 1
endfunction

function test takes nothing returns nothing
    local integer i
    
    call BJDebugMsg("Testing")
    
    set ub = CreateUnit(Player(0),'hfoo',0.,0.,0.)
    set ue = CreateUnit(Player(0),'hfoo',0.,0.,0.)

    set i = 0
    set chain = 0
    loop
        exitwhen i >= 10
        set chain = link.Create(0.,0.,80.,GetRandomReal(-0.5,0.5),chain)
        set i = i + 1
    endloop
    
    call BJDebugMsg("Lightning created")

    call chain.FK(0.,0.)
    
    call BJDebugMsg("Running IK loop")
    
    call TimerStart(CreateTimer(),dt,true,function test_periodic)
endfunction

Paste into a map and call test().

Edit: Attached map.
Attached Files
File type: w3xjtik.w3x (18.1 KB)
07-28-2007, 03:55 AM#2
darkwulfv
Umm, what is this and what's it supposed to do? "IK" doesn't really explain it. Whatever it is, it looks complicated... I think.
07-28-2007, 04:05 AM#3
Earth-Fury
Quote:
Originally Posted by darkwulfv
Umm, what is this and what's it supposed to do? "IK" doesn't really explain it. Whatever it is, it looks complicated... I think.

An articulated figure consists of a set of rigid segments connected with joints. Varying angles of the joints yields an indefinite number of configurations. The solution to the forward kinematic animation problem, given these angles, is the pose of the figure. The more difficult solution to the inverse kinematics problem is to find the joint angles given the desired configuration of the figure (i.e., end effector). In the general case there is no analytic solution for the inverse kinematics problem. However, inverse kinematics may be solved via nonlinear programming techniques. Certain special kinematic chains—those with a spherical wrist—permit kinematic decoupling. This treats the end effector's orientation and position independently and permits an efficient closed-form solution.

Are you stupid or something?


..... But yeah, without the will to download and test it, whats the effect of all this wonderfull and complex language i stole?
07-28-2007, 04:06 AM#4
Naakaloh
Inverse Kinematics; it's used to evaulate the shape of things like flowing cloth, or chains.

And his indenting is fine; it's just that tabs are 8 spaces instead of 4. It's still completely readable.

Edit: It looks pretty cool in-game too, though I'm not exactly sure if it's connected correctly to the second footman.
07-28-2007, 04:30 AM#5
PipeDream
This demo animates a linear skeleton of ten fixed length lightning bolts to follow a moving endpoint.

The lag behind the second footman was added by scaling down the step size. Increase global real h by a factor of ten or so for a clean follow.
07-28-2007, 10:40 AM#6
Toadcop
=\ one after other are realysing my ideas xD well it's extreme funny to look at this =)

// PS i got this idea long ago but powned it cause there is no really practical usage for it =\ but for fun it's cool ^^

btw currently it's bugged. but it doesnt matters. the feature is to find a place there it can be used ;)
07-28-2007, 03:51 PM#7
Naakaloh
It's not bugged, the length of the chain is fixed and the position of the endpoints is not dependent on the length of the chain, so it appears like it's not functioning correctly. If the middle footman were stationary in the center of the revolving one it would probably appear correctly, but it probably wouldn't be very exciting.
07-28-2007, 04:36 PM#8
Vexorian
Me thinks the clicherious hook spell would get a boost with this. Either way good work it is good to have nice demo maps that you can run.
07-28-2007, 04:58 PM#9
moyack
Quote:
Originally Posted by Naakaloh
It's not bugged, the length of the chain is fixed and the position of the endpoints is not dependent on the length of the chain, so it appears like it's not functioning correctly. If the middle footman were stationary in the center of the revolving one it would probably appear correctly, but it probably wouldn't be very exciting.
The system doesn't have any issue, and the reason is simple, if you have a mechanic arm formed with several joints, there are many (we can say infinite) possible joint positions where the arm can reach a point, so the system is designed to only move the necessary joints to catch the target point, and most of the time with changing the position of two or three of them is enough to obtain the required target.
07-28-2007, 05:06 PM#10
Toadcop
anyway such a link is unusable in some spell for example. (currently) it looks ugly =\