Ragdoll Help

I’m getting familiar with codea, but I have a question about physics. I have tried to make a ragdoll class, which works well with making the ragdoll and drawing him, but the collisions seem to be off. I don’t know if I am not making the physics bodies correctly or what. So I was wondering, what would be the best way to go about making a ragdoll. I can post my code if needed. Thanks in advance

I replaced my rects with line to get a more accurate rendering, and as I suspected the physics bodies were bigger than the the rects (or the rects were smaller than the physics bodies). Do you see where in my code I am going wrong?

You could use rects instead of lines. It was just easier to use the lines for a quick example.

I mean collisions that look better than mine, how the ragdoll hits the ground before he should. the head hits but it looks like the body floats

@captsmitty77 Here’s a version that uses real polygons not like my last one… You can drag it to hit the circle and fall to the floor. I think this is what you’re after.


displayMode(FULLSCREEN)

function setup()
    physics.continuous=true
    
    b=physics.body(CIRCLE,10)
    b.x=350
    b.y=400
    b.type=STATIC
    
    e=physics.body(EDGE,vec2(0,100),vec2(WIDTH,100))
    
    p1=physics.body(POLYGON,vec2(0,0),vec2(100,0),vec2(100,10),vec2(0,10))
    p1.x=200
    p1.y=600
    
    p2=physics.body(POLYGON,vec2(0,0),vec2(100,0),vec2(100,10),vec2(0,10))
    p2.x=300
    p2.y=600
    
    p3=physics.body(POLYGON,vec2(0,0),vec2(50,0),vec2(50,10),vec2(0,10))
    p3.x=400
    p3.y=600
    
    j1=physics.joint(REVOLUTE,p1,p2,p2.position-vec2(0,-5))
    j2=physics.joint(REVOLUTE,p2,p3,p3.position-vec2(0,-5))
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
    
    ellipse(b.x,b.y,20)
    line(0,100,WIDTH,100)
    
    pushMatrix()
    translate(p1.x,p1.y)
    rotate(p1.angle)   
    line(0,0,100,0)
    line(100,0,100,10)
    line(100,10,0,10)
    line(0,10,0,0)
    popMatrix()
    
    pushMatrix()
    translate(p2.x,p2.y)
    rotate(p2.angle)   
    line(0,0,100,0)
    line(100,0,100,10)
    line(100,10,0,10)
    line(0,10,0,0)
    popMatrix()
    
    pushMatrix()
    translate(p3.x,p3.y)
    rotate(p3.angle)   
    line(0,0,50,0)
    line(50,0,50,10)
    line(50,10,0,10)
    line(0,10,0,0)
    popMatrix()    
end

function touched(t)
    if t.state==MOVING then
        p1.linearVelocity=vec2(t.deltaX*50,t.deltaY*50)
    end
end

What do you mean “with good collisions”.

So would I need to use lines instead of rects?

I use rects too so its ok. Did you come up with anything with good collisions?

@captsmitty77 I might be cheating a little since I’m using rects instead of polygons. It would probably take me a lot longer to add true polygons to the code.

@captsmitty77 I updated the code above.

Thanks for that code, but I seem to be having problems with the polygon physics bodies

Let me try to add polygons to this and see what happens.

i am not familar with joints, but in the doc physics.joint(REVOLUTE,a,b,c) has 3 parameters besides revolute, but you pass 4… are you sure it is correct?

I think I see what you’re talking about. The body remains above the line, but the head makes contact with it. Is that what you don’t like.

@dave1707 You got it. I don’t like the bodys (and maybe the arms and legs if the are doing this too) collisions. That is what I want to fix. Thanks for saying you like it and how natural it looks, that was my main goal.

@Jmv38 Oh that’s right. That may be the issue. I’ll look at that

The thing I don’t like though is the collisions (except for the head)

@Jmv38 I think I am passing the correct values for the revolute, but the points I calculate within the vec2 anchor look confusing due to the parentheses and commas

@captsmitty77 You have a lot of code to look thru, so I thought I’d throw a small version together. As you can see, the points make contact with the floor which is what I think you want. Maybe this will help you see why your joints aren’t.

EDIT: updated code to add rectangles.


displayMode(FULLSCREEN)
--supportedOrientations(LANDSCAPE_ANY)

function setup()
    physics.continuous=true
    e=physics.body(EDGE,vec2(-200,100),vec2(WIDTH+200,100))
    p1=physics.body(CIRCLE,30)
    p1.x=300
    p1.y=600
    p2=physics.body(CIRCLE,10)
    p2.x=500
    p2.y=600
    p3=physics.body(CIRCLE,10)
    p3.x=600
    p3.y=600
    jnt1=physics.joint(REVOLUTE,p1,p2,p1.position)
    jnt2=physics.joint(REVOLUTE,p2,p3,p2.position)
end

function draw()
    background(40, 40, 50)
    fill(255)
    ellipse(p1.x,p1.y,60)
    stroke(255)
    strokeWidth(2)
    line(p1.x,p1.y,p2.x,p2.y)
    line(p2.x,p2.y,p3.x,p3.y)
    line(0,100,WIDTH,100)
    
    pushMatrix()
    fill(0,0,255)
    translate(p2.x,p2.y)
    ellipse(0,0,10)
    rotate(p2.angle)
    rect(-200,-10,200,20)
    popMatrix()
    
    pushMatrix()
    translate(p3.x,p3.y)
    ellipse(0,0,10)
    rotate(p3.angle)
    rect(-100,-10,110,20)
    popMatrix()
end

function touched(t)
    if t.state==MOVING then
        p1.linearVelocity=vec2(t.deltaX*50,t.deltaY*50)
    end
end

@captsmitty77 I like the way it moves. What don’t you like about the collisions, I think it looks natural.

Thanks for those links, but I already looked at them after I had attempted to build the ragdoll. Could you take a look at this code and see what I am doing wrong? I probably should’ve looked into this before writing this long script.

http://pastebin.com/vu3Gp5W2

Try here also.

http://codea.io/talk/discussion/2902/ragdoll-physics-without-box2d/p1