Page 1 of 2

Physics, revolute joints, anchors. How does it works?

Posted: Fri Sep 02, 2011 12:24 pm
by knarf
This is the first time I'm using "realistic physics" (box2D and the likes) so it's pretty new to me. I've started by following the small tutorial on the wiki. Btw, some functions of love.physics are not documented on the wiki (newBody?) and the official box2D manual linked on the wiki is down.

So, ultimately I want to make something like this:
KQuoE.png
KQuoE.png (4.22 KiB) Viewed 671 times
But for now, I'm just messing around with physics. I'm trying to make the torso and 2 simple legs (the thighs in the pic). But I can't get it working or I'm doing something wrong?

I also have questions about some stuff in comments...
Source here and .love attached.

Code: Select all

-- box2D new box shape+body
function box(x, y, w, h, m, r)
   local o = {}
   r = r or 0
   o.body  = love.physics.newBody(world, x, y, m, r)
   o.shape = love.physics.newRectangleShape(o.body, 0, 0, w, h)
   o.color = {math.random(256), math.random(256), math.random(256)}
   return o
end

-- draw box
function dr(s)
   love.graphics.setColor(s.color[1], s.color[2], s.color[3])
   love.graphics.polygon("fill", s.shape:getPoints())
end

function love.load()
   width = 650
   height = 650

   world = love.physics.newWorld(-width, -height, width, height)
   world:setGravity(0, 9)
   world:setMeter(64)
   
   -- why do i need to put width*2 to stretch the ground to the entire window?
   -- same for height wich currently doesn't fill the bottom of the window
   ground = box(0, height-20, width*2, 20, 0)
   
   torso = box(width/2, height/2, 20, 100, 15)
   front = box(width/2, height/2+80, 15, 80, 15)
   back  = box(width/2, height/2+80, 15, 80, 15)

   -- one joint for each leg
   -- x,y parameters are coordinates for the axe of rotation i guess?
   j1 = love.physics.newRevoluteJoint(front.body, torso.body, width/2+10, height/2+90)
   j2 = love.physics.newRevoluteJoint(back.body,  torso.body, width/2+10, height/2+90)
   
   -- lets try to rotate a bit
   front.body:setAngle(-math.pi/4)
   back.body:setAngle(math.pi/4)

   love.graphics.setMode(width, height, false, true, 0)
end


function love.update(dt)
   world:update(dt)
end


function love.draw()
   dr(ground)
   dr(torso)
   dr(front)
   dr(back)
end

Re: Physics, revolute joints, anchors. How does it works?

Posted: Fri Sep 02, 2011 12:39 pm
by Robin
I haven't looked to closely yet, but always, always upload a .love, even if all you have is a main.lua. Thank you.

Re: Physics, revolute joints, anchors. How does it works?

Posted: Fri Sep 02, 2011 1:00 pm
by knarf
Ok, done.

Re: Physics, revolute joints, anchors. How does it works?

Posted: Fri Sep 02, 2011 2:50 pm
by GijsB
you need to draw image's, and then rotate them with the rotation of the Body!

Re: Physics, revolute joints, anchors. How does it works?

Posted: Fri Sep 02, 2011 3:02 pm
by knarf
GijsB wrote:you need to draw image's, and then rotate them with the rotation of the Body!
...what? I'm already rotating them with body:setAngle. What's the correct way to rotate a body?

Re: Physics, revolute joints, anchors. How does it works?

Posted: Fri Sep 02, 2011 3:09 pm
by Robin
You need to use body:getAngle() to get the rotation of the body, so you can draw it rotated. Remember, you can't draw physics objects directly, you need to infer from the body's position and rotation how to draw it.

Re: Physics, revolute joints, anchors. How does it works?

Posted: Fri Sep 02, 2011 3:26 pm
by knarf
Robin wrote:You need to use body:getAngle() to get the rotation of the body, so you can draw it rotated. Remember, you can't draw physics objects directly, you need to infer from the body's position and rotation how to draw it.
Shape:getPoints() coordinates are already rotated and I draw a polygon with them. This function is not documented on the wiki btw.
On the .love attached, pressing up rotates the legs but not like I want. See the schema in my first post.

edit: I meant Shape:getPoints()

Re: Physics, revolute joints, anchors. How does it works?

Posted: Fri Sep 02, 2011 9:26 pm
by Robin
When drawing images, one generally doesn't use Shape:getPoints(), but still: good call.

Re: Physics, revolute joints, anchors. How does it works?

Posted: Fri Sep 02, 2011 9:59 pm
by vrld
You need to give the bodies rotational inertia, i.e.:

Code: Select all

torso = box(width/2, height/2,    20, 100, 15, .1)
-- instead of torso = box(width/2, height/2,    20, 100, 15)

Re: Physics, revolute joints, anchors. How does it works?

Posted: Sat Sep 03, 2011 12:58 pm
by knarf
vrld wrote:You need to give the bodies rotational inertia, i.e.:

Code: Select all

torso = box(width/2, height/2,    20, 100, 15, .1)
-- instead of torso = box(width/2, height/2,    20, 100, 15)
This is better. But the legs are not rotating around the right axes :huh:
I've removed collision between the legs with Categories and Masks. Pressing up and down adds some angular velocity.

Also, I still don't understand why I have to set a width of 2*window_width (see code) to actually get a width of window_width for the ground.