ants tripod gait

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

ants tripod gait

Post by BasicCoder2 »

When I ran Boromir's Marabunta: 2D Ant Game I wondered if I could make more realistic looking ants. From reading how they moved I learned that they use a tripod gait. The front and back legs on one side and the center leg on the other side would be on the ground while the other legs floated forward above the ground. In other words the blue legs form one tripod and the red legs form the other tripod. If you look carefully you will see either the blue or the red legs fixed on the ground.

Code: Select all

'some useful defines
Const Pi = 4 * Atn(1)
Dim Shared As Double TwoPi = 8 * Atn(1)
Dim Shared As Double RtoD = 180 / Pi   ' radians * RtoD = degrees
Dim Shared As Double DtoR = Pi / 180   ' degrees * DtoR = radians

screenres 800,480,32
color rgb(0,0,0),rgb(255,255,255):cls

dim as integer x,y,d,f,dd,ss

ss = 50
dd = 10
d = 10     ' leg stretch
x = 20     ' x center of ant
y = 100    ' y center of ant
f = -1     ' swing direction back and forth

do
    screenlock
    cls
    
    'draw lines to see position of end of legs
    for i as integer = 0 to 600 step 10
        line (i,0)-(i,480),rgb(200,200,0)
    next i
    
    'draw ant segments
    circle (x-40,y),15,rgb(0,0,0),,,.6,f
    circle (x-20,y),8,rgb(0,0,0),,,,f
    circle (x,y),12,rgb(0,0,0),,,,f
    circle (x+20,y),8,rgb(0,0,0),,,,f
    circle (x+40,y),12,rgb(0,0,0),,,,f
    
    ' Draws nippers
     Circle (x+63,y), 10, rgb(0,0,0),DtoR * 90, DtoR * 270,.6
     Circle (x+63,y), 9, rgb(0,0,0),DtoR * 90, DtoR * 270,.6
     
     'draw antennae
     Circle (x+63,y), 29, rgb(0,0,0),DtoR * 90, DtoR * 270,.5
     Circle (x+63,y), 28, rgb(0,0,0),DtoR * 90, DtoR * 270,.5
     
     'draw eyes
     circle (x+40,y-8),1,rgb(255,255,0),,,,f
     circle (x+40,y+8),1,rgb(255,255,0),,,,f

    ' *** draw red tripod legs ***
    
    'TOP center leg
    line   (x,y-8)-(x+d,y-ss),rgb(255,0,0)
    circle (x+d,y-ss),3,rgb(255,0,0),,,,f
    circle (x,y-8),3,rgb(255,0,0),,,,f
    
    'bottom front leg
    line   (x+20,y+8)-(x+d+20+dd,y+ss),rgb(255,0,0)
    circle (x+d+20+dd,y+ss),3,rgb(255,0,0),,,,f
    circle (x+20,y+8),3,rgb(255,0,0),,,,f
    
    'bottom back leg
    line   (x-20,y+8)-(x+d-20-dd,y+ss),rgb(255,0,0)
    circle (x+d-20-dd,y+ss),3,rgb(255,0,0),,,,f
    circle (x-20,y+8),3,rgb(225,0,0),,,,f
    
    
    ' ***  draw blue tripod legs ***
    
    'BOTTOM CENTER leg
    line (x,y+8)-(x-d,y+ss),rgb(0,0,255)
    circle (x-d,y+ss),3,rgb(0,0,255),,,,f
    circle (x,y+8),3,rgb(0,0,255),,,,f
    
    'TOP BACK LEG
    line (x-20,y-8)-(x-20-d-dd,y-ss),rgb(0,0,255)
    circle (x-20-d-dd,y-ss),3,rgb(0,0,255),,,,f
    circle (x-20,y-8),3,rgb(0,0,255),,,,f    
    
    'TOP FRONT LEG
    line (x+20,y-8)-(x+20-d+dd,y-ss),rgb(0,0,255)
    circle (x+20-d+dd,y-ss),3,rgb(0,0,255),,,,f
    circle (x+20,y-8),3,rgb(0,0,255),,,,f 
    
    screenunlock
    
    d = d + f
    if d >= 10 or d <= -10 then
        f = -f  'flip direction of legs
    end if

    'move ant one pixel right wrap around if > 480
    x = x + 1
    if x >= 480 then x = 20
    
    sleep 20
loop until multikey(&H01)

dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: ants tripod gait

Post by dodicat »

Very menacing looking creature basiccoder2.
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Re: ants tripod gait

Post by BasicCoder2 »

@dodicat
It was just a quick piece of code to illustrate the tripod gait. I need the legs to be "objects" and their joints to be handled as angles to the body. The idea is to have a central pattern generator to control the legs which would be modulated by a higher control system as in real organisms. Then the direction and speed of the ant (or any 2d simulated insect) can be controlled with the keyboard or they can control themselves in response to their environment (including the presence of other ants).
This is getting closer to making legs objects.

Code: Select all

'some useful defines
Const Pi = 4 * Atn(1)
Dim Shared As Double TwoPi = 8 * Atn(1)
Dim Shared As Double RtoD = 180 / Pi   ' radians * RtoD = degrees
Dim Shared As Double DtoR = Pi / 180   ' degrees * DtoR = radians

const RED = rgb(255,0,0)
const BLUE = rgb(0,0,255)

screenres 800,480,32
color rgb(0,0,0),rgb(255,255,255):cls

type ANT
    as integer x
    as integer y
end type

dim as ANT ant1

ant1.x = 100
ant1.y = 170


type LEG
    as integer x,y      'start postion on body
    as integer xd,yd
    as integer f        'phase
    as single  angle    'angle of shoulder
    as single  minAngle
    as single  maxAngle
    as single  length
    as ulong   c
end type

dim shared as LEG legs(1 to 6)

legs(1).x =  - 24
legs(1).y =  - 15
legs(1).f = 1
legs(1).angle = 250.0
legs(1).minAngle = 230.0
legs(1).maxAngle = 270.0
legs(1).length = 70
legs(1).c = RED

legs(2).x = 0
legs(2).y = - 15
legs(2).f = -1
legs(2).angle = 270.0
legs(2).minAngle = 250.0
legs(2).maxAngle = 290.0
legs(2).length = 60
legs(2).c = BLUE

legs(3).x =  + 24
legs(3).y =  - 15
legs(3).f = 1
legs(3).angle = 290.0
legs(3).minAngle = 270.0
legs(3).maxAngle = 310.0
legs(3).length = 55
legs(3).c = RED

legs(4).x =  - 24
legs(4).y =  + 15
legs(4).f = 1
legs(4).angle = 110.0
legs(4).minAngle = 90.0
legs(4).maxAngle = 130.0
legs(4).length = 70
legs(4).c = BLUE

legs(5).x = 0
legs(5).y = + 15
legs(5).f = -1
legs(5).angle = 90.0
legs(5).minAngle = 70.0
legs(5).maxAngle = 110.0
legs(5).length = 60
legs(5).c = RED

legs(6).x =  + 24
legs(6).y =  + 15
legs(6).f = 1
legs(6).angle = 70.0
legs(6).minAngle = 50.0
legs(6).maxAngle = 90.0
legs(6).length = 55
legs(6).c      = BLUE


dim as integer f
f = 1

do
    

    for i as integer = 1 to 6
        'compute end of legs
        legs(i).xd = cos(legs(i).angle * DtoR) * legs(i).length
        legs(i).yd = sin(legs(i).angle * DtoR) * legs(i).length
        'change phase
        if legs(i).angle <= legs(i).minAngle then legs(i).f = -legs(i).f
        if legs(i).angle >= legs(i).maxAngle then legs(i).f = -legs(i).f
        'change angle of legs
        if legs(i).f = 1 then
            legs(i).angle = legs(i).angle - 1
        else
            legs(i).angle = legs(i).angle + 1
        end if
    next i
    
    screenlock
    cls
    for i as integer = 0 to 800 step 10
        line (i,0)-(i,470),rgb(200,150,0)
    next i
    
    'back segment
    circle (ant1.x-60,ant1.y),15,rgb(255,0,0),,,.6,f
    
    'leg segments
    circle (ant1.x-24,ant1.y),20,rgb(0,0,0),,,.55,f
    circle (ant1.x,ant1.y),20,rgb(0,0,0),,,.55,f
    circle (ant1.x+24,ant1.y),20,rgb(0,0,0),,,.55,f
    
    'head segment
    circle (ant1.x + 60,ant1.y),16,rgb(0,0,0),,,,f
     'draw antennae
     Circle (ant1.x+93,ant1.y), 29, rgb(0,0,0),DtoR * 90, DtoR * 270,.4
     Circle (ant1.x+93,ant1.y), 28, rgb(0,0,0),DtoR * 90, DtoR * 270,.4
    'eyes
    circle (ant1.x + 65,ant1.y-5),3,rgb(255,255,0),,,,f
    circle (ant1.x + 65,ant1.y+5),3,rgb(255,255,0),,,,f
    

    
    for i as integer = 1 to 6
        line (legs(i).x + ant1.x, legs(i).y + ant1.y)-(legs(i).x+legs(i).xd + ant1.x,legs(i).y+legs(i).yd + ant1.y ),legs(i).c
        circle (legs(i).x + ant1.x, legs(i).y + ant1.y),3,rgb(0,0,0),,,,f
        circle (legs(i).xd+legs(i).x + ant1.x ,legs(i).yd+legs(i).y + ant1.y),3,legs(i).c,,,,f
    next i
    
    screenunlock
    
    ant1.x = ant1.x + 1
    if ant1.x = 700 then
        ant1.x = 100
    end if

    sleep 20
    
loop until multikey(&H01)
Post Reply