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.
Problems with "dynamic" bodies - love.physics
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Problems with "dynamic" bodies - love.physics
- Attachments
-
- platformer.love
- broken
- (1.61 KiB) Downloaded 88 times
- 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
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.
Re: Problems with "dynamic" bodies - love.physics
It lagged before I made the dynamic body.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.
And what about the actual problem?
- OmarShehata
- Party member
- Posts: 259
- Joined: Tue May 29, 2012 6:46 pm
- Location: Egypt
- Contact:
Re: Problems with "dynamic" bodies - love.physics
They're the same problem.Doctory wrote:It lagged before I made the dynamic body.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.
And what about the actual 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
Re: Problems with "dynamic" bodies - love.physics
I meant after I made the dynamic body
- 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
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.
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.
Re: Problems with "dynamic" bodies - love.physics
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:
Then in love.draw you take out the definitions and draw the representations of the bodies defined in love.load:
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 ) - I'm sure you're getting to that though!
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
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()))
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 40 guests