Less lag in building game.

Hi, I’, trying to make a 2d Building game, but I ran into a problem. With all the terrain and houses, it makes the program slow down A LOT, I think the reason is the loop. I’ve made a loop that goes through a table, and redraws each of them, so if I have 300 squares on the screen, then it has to redraw them every frame. Or maybe its because they are physic bodies. Does anyone have any ideas? Could I use backingmode somehow?

Thanks, Prynok

@Prynok I think the physics.bodies is the problem. Why do they have to be physics.bodies.

Because you are able to walk around in the game. I don’t want the player to walk through things.

@Prynok make a custom collision detection based on the tile he’s on…? or only make physics.bodies from a few tiles around the player…?

@Prynok Could you just use distance calculations to stop the player from walking thru things.

@stevon8ter I thought of that, but I’m not sure that would be any better. Because every time the player walks, it would get laggier creating and removing the physic.bodies

@prynok then i think the best way would be to create a custom collision detection, such as @dave1707 sugests, with distance calculations for example

@Stevon8ter That’s what I tried doing here: http://twolivesleft.com/Codea/Talk/discussion/comment/28642 But I didn’t want to do a grid system, so I changed to physics.

Well if you give me a day or so to work something out… xD

nah i’ll try to work something out in an hour or 2… whenever i go to bed i’ll start working on it, but idk if i’ll manage to do it ;p

I’m relative noob at codea, but i already made some of this games in other languages, especially on my TI84+ calculator xD
But as far as that went, it was with a grid system… but smooth movement, but It should be possible to work something out :wink:

@Prynok I don’t know if this is something like you’re doing, but I’m moving a character around a grid area checking if it’s over a key. I don’t think it would slow down that much if I placed houses or other objects on the screen and checked to keep the character from going thru them. Maybe I’ll try adding squares and see what happens.

http://twolivesleft.com/Codea/Talk/discussion/2166/scrolling-game-starter#Item_2

Could be he means something like this, but i think he means something like terraria, some kind of 2d minecraft…

we’ll have to wait for @Prynok to answer :wink:

@stevon8ter Yup! Like terriaria,

Well @Prynok, i started a little example, and I’m almost there :wink:

@stevon8ter Thx so much! I like little examples! :)>-

@Prynok @stevon8ter I don’t play games, so I had to look up terraria to see what you were talking about. I think the physics.objects are out. When I saw one of the guys digging away at a wall with a pick, the pick was overlapping the wall as he was swinging it, so I think distance calculations would work.

@Prynok - I think it should be a lot faster if you write your own collision code

ok i worked a small example out, tho it’s rather messy and uncommented

I just don’t have alot of time now, but around 4am, when i stop skyping, i’ll try to write it out nicely and commented

also, you might wanna considere using a vec3, and using the z to store blockdata

-- test

-- Use this function to perform your initial setup
function setup()
        
    print("Hello World!")
    
    displayMode(FULLSCREEN)
    
    char = vec2(75, 525)
    
    Blocks = {}
    
    for e = 1, 10 do
        
        Blocks[e] = {}
        
        for i = 1, 60 do
        
            Blocks[e][i] = vec2(i*50 - 25, e*50 - 25)
        
        end
        
    end
    
    Blocks[10][7].y = 9*50 - 25
    Blocks[10][9].y = 9*50 - 25
    Blocks[10][8].y = 9*50 - 25
    jump = false
    
end

-- This function gets called once every frame
function draw()
    
    print(char.x)
    print(char,y)
    background(113, 255, 0, 255)
    
    for i, v in ipairs(Blocks) do
        
        for e, k in ipairs(Blocks[i]) do
            
            sprite("Platformer Art:Block Grass", Blocks[i][e].x, Blocks[i][e].y, 50, 50)
            
        end
        
    end
    
    print(math.ceil((525-50)/50))
    
    if char.y - 25 > Blocks[math.ceil((char.y-50)/50)][math.ceil((char.x+25)/50)].y + 25 and char.y - 25 > Blocks[math.ceil((char.y-50)/50)][math.floor((char.x+25)/50)].y + 25 then
        char.y = char.y - 1
    end
    
    sprite("Platformer Art:Hill Short", char.x, char.y, 50, 50)
    
    if CurrentTouch.state ~= ENDED then
    if CurrentTouch.x < char.x then
        char.x = char.x - 1
    elseif CurrentTouch.x > char.x then
        char.x = char.x + 1
    end
    end
    
end

function touched(touch)
    
    
    
end

@Prynok you don’t wanna use grid system… is that for movement or for blocking as well?

@stevon8ter I don’t want a grid system at all. I’m trying to allow users to place blocks anywhere on the screen, not just a fixed point in space.

Also, your example is great, I understood the hole thing, the problem is its a lot slower than my physic.bodies method, Probably because the loop with physic.bodies is equal to the giant “if” statement.

@Prynok physics, bodies uses acceleration, my example uses a constant movement of - 1 and + 1 etc … :wink:

as for the no grid system, i’d have to take a closer look at it