Physics, revolute joints, anchors. How does it works?
Posted: Fri Sep 02, 2011 12:24 pm
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:
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.
So, ultimately I want to make something like this:
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