Make a body slide instead of rotate.

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.
Post Reply
User avatar
Dr.Tyler O.
Citizen
Posts: 58
Joined: Tue Jul 29, 2014 9:17 am
Location: United States

Make a body slide instead of rotate.

Post 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.
Any topic I have ever posted that I felt did not require a .love file, although they were requested, never required one so I would appreciate if you did not ask me to supply one unless you think that I am ignorant for not doing so.
User avatar
Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

Re: Make a body slide instead of rotate.

Post 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?
Last edited by Snuux on Tue Jul 29, 2014 9:53 am, edited 1 time in total.
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31
(Sorry for my english. I learn it myself, and I don't have enough experience)
User avatar
Dr.Tyler O.
Citizen
Posts: 58
Joined: Tue Jul 29, 2014 9:17 am
Location: United States

Re: Make a body slide instead of rotate.

Post by Dr.Tyler O. »

Snuux wrote:Now I creating game with love.physics, and may be this http://love2d.org/wiki/Body:setFixedRotation?
Thanks!
Any topic I have ever posted that I felt did not require a .love file, although they were requested, never required one so I would appreciate if you did not ask me to supply one unless you think that I am ignorant for not doing so.
User avatar
Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

Re: Make a body slide instead of rotate.

Post 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
Attachments
Small Experiment.love
Press space for pause and resume rotation
(1.1 KiB) Downloaded 116 times
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31
(Sorry for my english. I learn it myself, and I don't have enough experience)
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Make a body slide instead of rotate.

Post 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.
Post Reply

Who is online

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