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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
knarf
Prole
Posts: 9
Joined: Fri Sep 02, 2011 10:39 am

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

Post 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 667 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
Attachments
qwop.love
(1.7 KiB) Downloaded 167 times
Last edited by knarf on Fri Sep 02, 2011 3:05 pm, edited 3 times in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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.
Help us help you: attach a .love.
knarf
Prole
Posts: 9
Joined: Fri Sep 02, 2011 10:39 am

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

Post by knarf »

Ok, done.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

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

Post by GijsB »

you need to draw image's, and then rotate them with the rotation of the Body!
knarf
Prole
Posts: 9
Joined: Fri Sep 02, 2011 10:39 am

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

Post 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?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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.
Help us help you: attach a .love.
knarf
Prole
Posts: 9
Joined: Fri Sep 02, 2011 10:39 am

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

Post 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()
Attachments
qwop.love
press up to rotate
(1.74 KiB) Downloaded 155 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

When drawing images, one generally doesn't use Shape:getPoints(), but still: good call.
Help us help you: attach a .love.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

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

Post 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)
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
knarf
Prole
Posts: 9
Joined: Fri Sep 02, 2011 10:39 am

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

Post 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.
Attachments
qwop.love
(2 KiB) Downloaded 158 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 4 guests