Page 1 of 1

My code couldn't handle more than 30 bouncy rectangles

Posted: Fri May 30, 2014 2:08 pm
by edwardander
Hello,

i wrote a code which is using physics of löve. but unfortunately it goes very laggy after 30th rectangle. do i make a mistake at somewhere in code or this is the borders of the LÖVE?

Code: Select all

--flux = require "libs/flux"

function love.load()

  love.physics.setMeter(100)
  world=love.physics.newWorld(0,9.81*100,true)

  objects = {}

  objects.ground = {}
  objects.ground.body = love.physics.newBody(world, 400, 550)
  objects.ground.shape = love.physics.newRectangleShape(600, 20) 
  objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape) 

  objects.ground2 = {}
  objects.ground2.body = love.physics.newBody(world, 400, 50)
  objects.ground2.shape = love.physics.newRectangleShape(600, 20) 
  objects.ground2.fixture = love.physics.newFixture(objects.ground2.body, objects.ground2.shape) 

  objects.ground3 = {}
  objects.ground3.body = love.physics.newBody(world, 140, 200)
  objects.ground3.shape = love.physics.newRectangleShape(20, 800) 
  objects.ground3.fixture = love.physics.newFixture(objects.ground3.body, objects.ground3.shape) 


  objects.ground4 = {}
  objects.ground4.body = love.physics.newBody(world, 700, 200)
  objects.ground4.shape = love.physics.newRectangleShape(20, 800) 
  objects.ground4.fixture = love.physics.newFixture(objects.ground4.body, objects.ground4.shape) 

  soldiers={}
end


function love.mousepressed(x,y,button)
  
  if button=="r" then
    table.insert(soldiers,{x=x,y=y,health=100,armor=100,xp=0,tweening={active=false,targetX=0,targetY=0},body=love.physics.newBody(world,x,y,"dynamic"),shape=love.physics.newRectangleShape(10,10)})
    for v,soldier in ipairs(soldiers) do
    soldier.fixture=love.physics.newFixture(soldier.body,soldier.shape,math.random(0.8,1.2))
    soldier.fixture:setRestitution(math.random(0,0.9))
    end

    
  end

end

  

function love.draw()
  for v,soldier in ipairs(soldiers) do
    love.graphics.setColor(255,255,255)
    love.graphics.polygon("fill", soldier.body:getWorldPoints(soldier.shape:getPoints()))
  end
  
  love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
  love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates
  love.graphics.polygon("fill", objects.ground2.body:getWorldPoints(objects.ground2.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates
  love.graphics.polygon("fill", objects.ground3.body:getWorldPoints(objects.ground3.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates
  love.graphics.polygon("fill", objects.ground4.body:getWorldPoints(objects.ground4.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates

end

function love.update(dt)
  world:update(dt)
  
end


Re: My code couldn't handle more than 30 bouncy rectangles

Posted: Fri May 30, 2014 6:36 pm
by fxva

Code: Select all

if button=="r" then
	local soldier = {}
	soldier.x = x
	soldier.y = y
	soldier.health = 100
	soldier.armor = 100
	soldier.xp = 0
	soldier.tweening = {active=false,targetX=0,targetY=0}
	soldier.body = love.physics.newBody(world, x, y, 'dynamic')
	soldier.shape = love.physics.newRectangleShape(10,10)
	soldier.fixture = love.physics.newFixture(soldier.body,soldier.shape,math.random(0.8,1.2))
	soldier.fixture:setRestitution(math.random(0,0.9))
	table.insert(soldiers, soldier)
end

:emo:

Re: My code couldn't handle more than 30 bouncy rectangles

Posted: Fri May 30, 2014 7:20 pm
by davisdude
This shouldn't really be in Projects and Demos, by the way. Also, more code would be useful (a .love would be even better)

Re: My code couldn't handle more than 30 bouncy rectangles

Posted: Sat May 31, 2014 8:47 am
by Robin
Or maybe that was what solved it. Looking at fxva's code, it looks like the original version created a new fixture for every extant soldier when a new one was created. Which means that after 30 soldiers, you'd have almost 900 fixtures, which would slow things down.

fxva, please communicate more clearly in the future, and that includes posting a .love when asking for help, and being clear when you fixed something yourself. ;)