My code couldn't handle more than 30 bouncy rectangles

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
edwardander
Prole
Posts: 10
Joined: Tue May 20, 2014 10:08 am

My code couldn't handle more than 30 bouncy rectangles

Post 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

fxva
Prole
Posts: 8
Joined: Sat Jul 13, 2013 4:54 am

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

Post 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:
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

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

Post 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)
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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. ;)
Help us help you: attach a .love.
Post Reply

Who is online

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