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