Problems with "dynamic" bodies - love.physics

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
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Problems with "dynamic" bodies - love.physics

Post by Doctory »

I'm developing a platformer game and I'm having trouble using "dynamic" bodies.
I experience severe lag, and the play seems to hit some invisible circle when trying to touch the "dynamic" body.
Hope everyone can help. ;)
Attachments
platformer.love
broken
(1.61 KiB) Downloaded 88 times
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Problems with "dynamic" bodies - love.physics

Post by Ranguna259 »

It's laggy because you are creating hundred and hundres of shapes a bodies in your draw loop, instead of creating the bodies inside the draw function create them in the load function.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Problems with "dynamic" bodies - love.physics

Post by Doctory »

Ranguna259 wrote:It's laggy because you are creating hundred and hundres of shapes a bodies in your draw loop, instead of creating the bodies inside the draw function create them in the load function.
It lagged before I made the dynamic body.
And what about the actual problem?
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: Problems with "dynamic" bodies - love.physics

Post by OmarShehata »

Doctory wrote:
Ranguna259 wrote:It's laggy because you are creating hundred and hundres of shapes a bodies in your draw loop, instead of creating the bodies inside the draw function create them in the load function.
It lagged before I made the dynamic body.
And what about the actual problem?
They're the same problem.

Dynamic or not, you're recreating every single body you're using, every frame. Nothing is being destroyed, so you're getting invisible objects everywhere.

Fix that and everything will work as it should. Create your bodies once only in love.load
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: Problems with "dynamic" bodies - love.physics

Post by Doctory »

I meant after I made the dynamic body
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Problems with "dynamic" bodies - love.physics

Post by Ranguna259 »

What do you mean after ?
Look at your loop inside the draw function, it's creating new rectangles every single frame and you only need to create a body once.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Problems with "dynamic" bodies - love.physics

Post by Helvecta »

To break it down further, when you define bodies and shapes and stuff inside love.draw, you're effectively making a huge pile of invisible brown boxes, creating this "circle" effect. WhatCHA should do is define all that in love.load, like placing this at the end of load.load:

Code: Select all

	-- go through the map ONCE, defining all those pretty bodies and fixtures.
	for y = 1, #map do
		for x = 1, #map[y] do
			if map[y][x] == 1 then
				rect = {}
				rect.body = love.physics.newBody(world, x * 20, y * 20, "static")
				rect.shape = love.physics.newRectangleShape(20, 20)
				rect.fixture = love.physics.newFixture(rect.body, rect.shape)
			elseif map[y][x] == 2 then
				box = {}
				box.body = love.physics.newBody(world, x * 20, y * 20, "dynamic")
				box.shape = love.physics.newRectangleShape(20, 20)
				box.fixture = love.physics.newFixture(box.body, box.shape)
			end
		end
	end
Then in love.draw you take out the definitions and draw the representations of the bodies defined in love.load:

Code: Select all

	for y = 1, #map do
		for x = 1, #map[y] do
			if map[y][x] == 1 then
				love.graphics.setColor(100, 100, 255)
				love.graphics.polygon("fill", rect.body:getWorldPoints(rect.shape:getPoints()))
			elseif map[y][x] == 0 then
				love.graphics.setColor(105, 172, 239)
				love.graphics.rectangle("fill", x * 20 - 10, y * 20 - 10, 20, 20)
			end
		end
	end
	
	love.graphics.setColor(100, 60, 5)
	love.graphics.polygon("fill", box.body:getWorldPoints(box.shape:getPoints()))
This is only a part of fixing the sneaky problems, because if you do this you will see that tiles defined as "2" will leave black spots on the map since they aren't defined as "0", and that you overwrite every single wall-type tile (rect = {}) so that only the last defined block actually has a visible representation (the table "box" suffers from the same thing, but there's only one box thingy defined so far so I guess that's okay for now), and that the dynamic shape has an odd resistance to being pushed to the right (I have no clue what's going on there :rofl:) - I'm sure you're getting to that though!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 40 guests