Help Me

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
headjd
Prole
Posts: 1
Joined: Thu Jan 21, 2016 3:58 am

Help Me

Post by headjd »

why sometimes this together and sometimes not?

GooD

Image

Bug?

Image

Code: Select all

function love.load ()
  -- body...
  love.physics.setMeter(30)
  world = love.physics.newWorld(0, 0, true)

  objeto = {}

  objeto.block1 = {}
  objeto.block1.x = love.graphics.getWidth()/2
  objeto.block1.y = love.graphics.getHeight()/2
  objeto.block1.body = love.physics.newBody(world, objeto.block1.x, objeto.block1.y,"dynamic")
  objeto.block1.shape = love.physics.newRectangleShape(0, 0,30,30)
  objeto.block1.fixture = love.physics.newFixture(objeto.block1.body, objeto.block1.shape,0)

  objeto.block2 = {}
  objeto.block2.x =150
  objeto.block2.y = 150
  objeto.block2.body = love.physics.newBody(world, objeto.block2.x, objeto.block2.y)
  objeto.block2.shape = love.physics.newRectangleShape(0, 0,30,200)
  objeto.block2.fixture = love.physics.newFixture(objeto.block2.body, objeto.block2.shape)



end
function love.update (dt)
  -- body...
  world:update(dt)
  if love.keyboard.isDown("right") then
    objeto.block1.body:setLinearVelocity(200,0)
  elseif love.keyboard.isDown("left") then
    objeto.block1.body:setLinearVelocity(-200,0)
  elseif love.keyboard.isDown("up") then
    objeto.block1.body:setLinearVelocity(0,-200)
  elseif love.keyboard.isDown("down") then
    objeto.block1.body:setLinearVelocity(0,200)
  else
      objeto.block1.body:setLinearVelocity(0,0)
  end


end
function love.draw ()
  -- body...
  love.graphics.setColor(50, 50, 50)
  love.graphics.polygon("fill", objeto.block1.body:getWorldPoints(objeto.block1.shape:getPoints()))

  love.graphics.setColor(50, 50, 50)
  love.graphics.polygon("fill", objeto.block2.body:getWorldPoints(objeto.block2.shape:getPoints()))
end
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Help Me

Post by Jasoco »

First, welcome to the forum. :awesome:

Second, please use more descriptive thread titles in the future. :nyu:

Third, this is something people have a problem with all the time. Not a bug. It's because your camera or objects are drawing in-between pixels. Simply math.floor the X and Y position of all drawing calls. Unfortunately in your case it will require more work since you'll have to convert the worldPoints of the objects to a table full of X and Y values, run through the table flooring every value, and then draw the polygon using that table instead. If you ever switch to using a simple image for every object, it'll be much simpler with just one X and Y. Until then you'll have to do something like the following: :oops:

Code: Select all

local t = objeto.block1.body:getWorldPoints(objeto.block1.shape:getPoints())
for i = 1, #t do
    t[i] = math.floor(t[i])
end
love.graphics.polygon("fill", t)
No guarantees that this won't add extra minuscule time to your rendering time.
Last edited by Jasoco on Thu Jan 21, 2016 6:43 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help Me

Post by zorg »

love.graphics.polygon("fill", objeto.block1.body:getWorldPoints(objeto.block1.shape:getPoints()))
love.graphics.polygon("fill", objeto.block2.body:getWorldPoints(objeto.block2.shape:getPoints()))
Instead of these, you could do:

Code: Select all

local x,y,w,h
x,y,w,h = objeto.block1.body:getWorldPoints(objeto.block1.shape:getPoints())
x,y,w,h = math.floor(x), math.floor(y), math.floor(w), math.floor(h)
love.graphics.rectangle("fill", x, y, w, h)
x,y,w,h = objeto.block2.body:getWorldPoints(objeto.block2.shape:getPoints())
x,y,w,h = math.floor(x), math.floor(y), math.floor(w), math.floor(h)
love.graphics.rectangle("fill", x, y, w, h)
In short, you should floor the coordinates, and the width/height too if necessary.
Also, Jasoco ninja'd me, but to add to that, if you only use rectangle shapes, then my solution is a bit more concise.
Oh, and i wouldn't name something "table" or any of the already used lua table names, but that's just me. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Help Me

Post by Jasoco »

You're right. it was a placeholder name that I didn't quite think through.

Instead name it something sure to not be used already like "math" or "love". Those aren't used yet, right? :joker:
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests