Page 1 of 1

Make a body slide instead of rotate.

Posted: Tue Jul 29, 2014 9:24 am
by Dr.Tyler O.
I've searched on the forums and didn't manage to find an answer to this question and I apologize if I missed it.
I feel that this question does not require a .love file to be answered.

Basically I have a rectangular body that is moved with applyForce and when it moves it rotates but I want it to just slide. I need to know how to stop the body from rotating entirely.

Re: Make a body slide instead of rotate.

Posted: Tue Jul 29, 2014 9:39 am
by Snuux
Now I creating game with love.physics (and every part of physics interesting for me too), and may be this http://love2d.org/wiki/Body:setFixedRotation?

Re: Make a body slide instead of rotate.

Posted: Tue Jul 29, 2014 9:42 am
by Dr.Tyler O.
Snuux wrote:Now I creating game with love.physics, and may be this http://love2d.org/wiki/Body:setFixedRotation?
Thanks!

Re: Make a body slide instead of rotate.

Posted: Tue Jul 29, 2014 9:55 am
by Snuux
I try to applyForce for Rectangle shape, but it don't rotate! What I do wrong?

Code: Select all

function createRectangle(x, y, w, h)
  local o = {}
  o.b = love.physics.newBody(world, x, y, "dynamic")
  o.s = love.physics.newRectangleShape(0, 0, w, h)
  o.f = love.physics.newFixture(o.b, o.s)
  
  return o
end

function love.load()
  world = love.physics.newWorld(0, 0)
  object = createRectangle(260, 260, 100, 100)
end

function love.update(dt)
  world:update(dt)
  object.b:applyForce(1000, 1500)
end

function love.draw()
  debugWorldDraw(world)
end

-----------DEBUG
function debugWorldDraw(world)
  local bodies = world:getBodyList()
  
  for b=#bodies,1,-1 do
    local body = bodies[b]
    local bx,by = body:getPosition()
    local bodyAngle = body:getAngle()
    love.graphics.push()
    love.graphics.translate(bx,by)
    love.graphics.rotate(bodyAngle)
    
    math.randomseed(1) --for color generation
    
    local fixtures = body:getFixtureList()
    for i=1,#fixtures do
      local fixture = fixtures[i]
      local shape = fixture:getShape()
      local shapeType = shape:getType()
      local isSensor = fixture:isSensor()
      
      if (isSensor) then
        love.graphics.setColor(0,0,255,96)
      else
        love.graphics.setColor(math.random(32,200),math.random(32,200),math.random(32,200),96)
      end
      
      love.graphics.setLineWidth(1)
      if (shapeType == "circle") then
        local x,y = fixture:getMassData() --0.9.0 missing circleshape:getPoint()
        local x,y = shape:getPoint() --0.9.1
        local radius = shape:getRadius()
        love.graphics.circle("fill",x,y,radius,15)
        love.graphics.setColor(0,0,0,255)
        love.graphics.circle("line",x,y,radius,15)
        local eyeRadius = radius/4
        love.graphics.setColor(0,0,0,255)
        love.graphics.circle("fill",x+radius-eyeRadius,y,eyeRadius,10)
      elseif (shapeType == "polygon") then
        local points = {shape:getPoints()}
        love.graphics.polygon("fill",points)
        love.graphics.setColor(0,0,0,255)
        love.graphics.polygon("line",points)
      elseif (shapeType == "edge") then
        love.graphics.setColor(0,0,0,255)
        love.graphics.line(shape:getPoints())
      elseif (shapeType == "chain") then
        love.graphics.setColor(0,0,0,255)
        love.graphics.line(shape:getPoints())
      end
    end
    love.graphics.pop()
  end
  
  local joints = world:getJointList()
  for index,joint in pairs(joints) do
    love.graphics.setColor(0,255,0,255)
    local x1,y1,x2,y2 = joint:getAnchors()
    if (x1 and x2) then
      love.graphics.setLineWidth(3)
      love.graphics.line(x1,y1,x2,y2)
    else
      love.graphics.setPointSize(3)
      if (x1) then
        love.graphics.point(x1,y1)
      end
      if (x2) then
        love.graphics.point(x2,y2)
      end
    end
  end
  
  local contacts = world:getContactList()
  for i=1,#contacts do
    love.graphics.setColor(255,0,0,255)
    love.graphics.setPointSize(3)
    local x1,y1,x2,y2 = contacts[i]:getPositions()
    if (x1) then
      love.graphics.point(x1,y1)
    end
    if (x2) then
      love.graphics.point(x2,y2)
    end
  end
end

Re: Make a body slide instead of rotate.

Posted: Tue Jul 29, 2014 11:30 am
by Plu
ApplyForce by default pushes against the center of mass. Unless there's something else that should force the object to rotate, it won't, because the force is distributed evenly against the object.

If you send two more arguments for the x and y coordinates inside the object that you want to push against, it should start rotating.