Wheels and Belt

I had nothing better to do so I thought I’d try this. It’s 3 wheels connected by a belt. The red wheel is the drive wheel and it’s rotation speed can be controlled by pressing Angular velocity + or - . You can also change the size of the drive wheel using the parameter slide. This doesn’t have any real use other than I wanted to see if I could create a belt that would drive the other wheels.

function setup()
    parameter.integer("dr",20,100,50,chg)
    chg()
    val=0
    tab={}
    j={}    
    for z=1,360,5 do
        x=math.sin(math.rad(z))*180+390
        y=math.cos(math.rad(z))*180+500
        table.insert(tab,rope(x,y))
    end    
    for z=1,#tab-1 do
        j[z] = physics.joint(ROPE,tab[z].a,tab[z+1].a,
            vec2(tab[z].a.x,tab[z].a.y),
            vec2(tab[z+1].a.x,tab[z+1].a.y),11)
    end 
    jj=#j+1
    j[jj] = physics.joint(ROPE,tab[1].a,tab[jj].a,
        vec2(tab[1].a.x,tab[1].a.y),
        vec2(tab[jj].a.x,tab[jj].a.y),11)  
    
    c1=physics.body(CIRCLE,5)
    c1.x=300
    c1.y=600
    c1.type=STATIC
    
    c2=physics.body(CIRCLE,5)
    c2.x=480
    c2.y=600
    c2.type=STATIC
    
    p1=physics.body(CIRCLE,50)
    p1.x=300
    p1.y=600
    p1.friction=20
 
    p2=physics.body(CIRCLE,60)
    p2.x=480
    p2.y=600
    p2.friction=20
    
    j1=physics.joint(REVOLUTE,c1,p1,c1.position,p1.position)
    j2=physics.joint(REVOLUTE,c2,p2,c2.position,p2.position)    
end

function chg()
    if p3 then
        p3:destroy()
        c3:destroy()
    end
    p3=physics.body(CIRCLE,dr)
    p3.x=390
    p3.y=400
    p3.friction=20  

    c3=physics.body(CIRCLE,5)
    c3.x=390
    c3.y=400
    c3.type=STATIC
    j3=physics.joint(REVOLUTE,c3,p3,c3.position,p3.position)    
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
    noFill()
    
    pushMatrix()
    translate(p1.x,p1.y)
    rotate(p1.angle)
    line(0,0,0,50)
    ellipse(0,0,100)
    popMatrix()

    pushMatrix() 
    translate(p2.x,p2.y)
    rotate(p2.angle)
    line(0,0,0,-60)
    ellipse(0,0,120)
    popMatrix() 
    
    pushMatrix() 
    stroke(255,0,0)
    translate(p3.x,p3.y)
    rotate(p3.angle)
    line(0,0,0,-dr)
    ellipse(0,0,dr*2)
    popMatrix() 

    for z=1,#tab do
        tab[z]:draw(z)
    end
    
    p3.angularVelocity=p3.angularVelocity+val
    
    fill(255)
    text(p1.angularVelocity//1,200,600)
    text(p2.angularVelocity//1,580,600)
    text(p3.angularVelocity//1,390,250)
    text("Angular velocity",390,280)
    fill(255,0,0)
    text("Angular velocity -",390-200,300)
    text("Angular velocity +",390+200,300)
    text("Drive wheel",390,480)
end

function touched(t)
    if t.state==BEGAN then
        if t.x>WIDTH/2 then
            val=5
        else
            val=-5
        end
    end
    if t.state==ENDED then
        val=0
    end
end

rope=class()

function rope:init(x,y,z)
    self.a = physics.body(CIRCLE,1)
    self.a.x = x
    self.a.y = y
    self.a.friction=10
end

function rope:draw(z)
    strokeWidth(4)
    if z>1 then
        stroke(255)
        line(self.a.x,self.a.y,tab[z-1].a.x,tab[z-1].a.y)
    else
        stroke(255,0,0)
        line(tab[1].a.x,tab[1].a.y,tab[jj].a.x,tab[jj].a.y)
    end
end

very heath robinson. :slight_smile:

nothing better to do!! how about a 3D physics engine!

@piinthesky I had to lookup Heath Robinson. The whole point was just to see if I could create the belt and if it would actually work. As for the 3D engine, more than I’d want to get into.

Heath Robinson is the best…

Hi @dave1707,

Excellent little physics demo. I’m lost when it comes to physics - something I need to pursue. Loaded this into Love 2D with LoveCodea and it basically ran - er sort of. The system was set up but then proceeded to drop off screen and partially bounce back. Digging into this further.

Basically writing this to query the lines as follows:


    text(p1.angularVelocity//1,200,600)
    text(p2.angularVelocity//1,580,600)
    text(p3.angularVelocity//1,390,250)

I seem to remember that the // is a mod function in some languages but can’t find a reference to it. Can you explain? - causes an error in the Love 2D port.

Thanks,

Bri_G

:confused:

@Bri_G The // is an integer divide that was added to Codea a release or 2 ago. When I want a simple math display I’ll use the // instead of the string.format function. Basically // just returns an integer answer from a divide.

@Bri_G A quick explanation that might help. c1, c2, and c3 are anchor points. p1, p2, and p3 are the wheels that are joined to the anchor points with the REVOLUTE joint. The wheels are then able to rotate on the anchor. The belt is just a ROPE joint made up of 72 points with the last point connected with the first. Since the belt is free flowing, if it comes off the wheels, it falls. I guess I could have set gravityScale for everything to 0 since gravity isn’t used. I had to play with setting the points around the 3 wheels so the belt would shrink to fit.

Hi @dave1707,

Thanks for the feedback - added a world zero gravity and everything stays in place - but it’s a start. Thought you might like to look at this - might help with Box2D:

Love2D Box2D tool

Thanks,

Bri_G

:slight_smile:

@Bri_G Here’s another example I made awhile back.

https://codea.io/talk/discussion/4929/physics-drive-example

Hi @dave1707,

Neat - going to use them on the physics tool to see how it works.

Thanks,

Bri_g

:slight_smile:

@Bri_G Here’s another one you can try.

supportedOrientations(PORTRAIT_ANY)

function setup()
    parameter.integer("pressure",1,200,10)
    speed=10
    p1=physics.body(CIRCLE,0)
    p1.x=WIDTH/2-50
    p1.y=HEIGHT
    p1.type=STATIC   
    r1=physics.body(CIRCLE,10)
    r1.x=WIDTH/2-50
    r1.y=HEIGHT-300
    p3=physics.body(CIRCLE,0)
    p3.x=WIDTH/2
    p3.y=300
    p3.type=STATIC
    p4=physics.body(CIRCLE,152)
    p4.x=WIDTH/2
    p4.y=300
    p6=physics.body(CIRCLE,0)
    p6.x=WIDTH/2-100
    p6.y=200
    j1=physics.joint(PRISMATIC,p1,r1,p1.position,vec2(0,1))
    j2=physics.joint(REVOLUTE,p3,p4,vec2(p3.x,p3.y))  
    j4=physics.joint(REVOLUTE,p4,p6,vec2(p4.x-100,p4.y-100))   
    j5=physics.joint(DISTANCE,r1,p6,r1.position,p6.position)
    r1.linearVelocity=vec2(0,limit)
end

function draw()
    background(0)
    fill(255)
    text("Angular velocity  "..p4.angularVelocity//1,200,100)
    text("Pressure  "..pressure,300,900)
    if r1.linearVelocity.y<0then
        speed=speed-pressure
        r1.linearVelocity=vec2(0,speed)
    else
        speed=r1.linearVelocity.y
    end
    stroke(255)
    strokeWidth(2)
    rect(r1.x-26,r1.y-10,50,20)
    noFill()
    rect(WIDTH/2-75,670,50,310)    
    if speed<0 then
        fill(255,0,0,pressure+55)
        rect(r1.x-25,r1.y+9,50,970-r1.y)
    end
    pushMatrix()
    noFill()
    translate(p3.x,p3.y)
    rotate(p4.angle)
    ellipse(0,0,p4.radius*2)
    ellipse(0,0,20)
    ellipse(-100,-100,20)
    line(-150,0,150,0)
    line(0,-150,0,150)
    popMatrix()
    line(j5.bodyA.x,j5.bodyA.y,j5.bodyB.x,j5.bodyB.y)
end

Hi @dave1707,

Sorry about delayed reply - away from kit!!! Another example to learn with - great.

Thanks again.

Bri_G

:slight_smile: