Moving meshes

Hi wise guys,

I’m up to rewriting a major bit of my game, which was entirely built with sprites.
I found it suffers several performance issues when there’s only few item on the screen, so now I’m going back to do it again with meshes. However I don’t know how to reiterate a certain object on screen as the following code should be supposed to do.

Can you tell me why does each spawned item leave an infinite trail of itself?
thanks in advance and sorry for bothering :slight_smile:



function setup()
    frame = 0
    foes = {}
    horde_counter = 0
    horde_limit = 5
end

function draw()

    frame = (frame+1)%128
    if frame%64 == 0 and horde_counter < horde_limit then
        table.insert(foes,Foe1m(500,HEIGHT-10))
        horde_counter = horde_counter + 1
    end
    
    for i,v in ipairs(foes) do
        v:draw()
    end
    
end
    
Foe1m = class()

function Foe1m:init(x,y)
    self.position = vec2(x, y)
    self.texture = readImage("Planet Cute:Rock")
    self.spriteSize = vec2(40, 30)
    self.mesh = mesh()
    self.mesh.texture = self.texture
    self.index = self.mesh:addRect(self.position.x, self.position.y, self.spriteSize.x, self.spriteSize.y)    


end

function Foe1m:draw()
    pushMatrix()
    pushStyle()
    self.position.x = self.position.x - 0.3
    self.position.y = self.position.y - 1
    translate(self.position.x,self.position.y)
    self.mesh:setRect(self.index,0, 0, self.spriteSize.x, self.spriteSize.y)    
    self.mesh:draw()    
    popStyle()
    popMatrix()

end




Don’t have my iPad on me at the moment, but some things to try:

  1. I don’t see a mesh:clear() call anywhere - you may need this otherwise each loop of draw you are adding new elements to the mesh

  2. You are creating a new mesh for new instance of your foe rather than adding new foe instances (mesh:addRect) to a single global mesh.

Hi @West, thanks for your help.
You mean something like this ?

function setup()
    frame = 0
    foes = {}
    horde_counter = 0
    horde_limit = 5
    position = vec2(WIDTH/2, HEIGHT-50)
    texture = readImage("Planet Cute:Rock")
    spriteSize = vec2(40, 30)
    mesh = mesh()
    mesh.texture = texture
    index = mesh:addRect(position.x, position.y, spriteSize.x, spriteSize.y)    
end

function draw()

    frame = (frame+1)%128
    if frame%64 == 0 and horde_counter < horde_limit then
        horde_counter = horde_counter + 1
        index = mesh:addRect(position.x, position.y, spriteSize.x, spriteSize.y)    
    end
    
    position.x = position.x - 0.3
    position.y = position.y - 1
    translate(position.x,position.y)
    mesh:setRect(index,0, 0, spriteSize.x, spriteSize.y)    
    mesh:draw()    
    popStyle()
    popMatrix()
    
end

I couldn’t get this working either way, and mesh:clear() makes my item to spawn in the very same place without moving.
I can wait for you to grab your’ Ipad thou :slight_smile:

Thanks again
regards

Put

background(40,40,50)

In the beginning of your draw function to prevent the infinite trail problem. It sets a black background, but you can also set other colors.

How stupid I am :((

Thanks @Staples

ok I got it working now and adding rects to the same mesh ( instead of adding a mesh instance for each spawn ), but how do I keep independent x,y values for each rect I created?

this is my current code :

Foe1mEmitter = class()
local horde_counter = 5
local horde_limit = 5
function Foe1mEmitter:init()
    Foe1m = Foe1m(WIDTH/2,HEIGHT-50)
    self.frame = 0
end

function Foe1mEmitter:draw()
    self.frame = (self.frame+1)%100
    if self.frame%100 == 0 and horde_counter <= horde_limit then
        Foe1m:SpawnFoe()    
        horde_counter = horde_counter + 1
    end
    Foe1m:draw()
end

function Foe1mEmitter:StartSpawingHorde()
    horde_counter = 0
end


Foe1m = class()

function Foe1m:init()
    self.position = vec2(WIDTH/2, HEIGHT-50)
    self.texture = readImage("Dropbox:raptor-cntr")
    self.spriteSize = vec2(30, 30)
    self.mesh = mesh()
    self.mesh.texture = self.texture
    self.index = 0
    --self.index = self.mesh:addRect(self.position.x, self.position.y, self.spriteSize.x, self.spriteSize.y)    


end

function Foe1m:draw()
    if self.index > 0 then
        pushMatrix()
        pushStyle()
        self.position.x = self.position.x - 0.3
        self.position.y = self.position.y - 1
        translate(self.position.x,self.position.y)
        self.mesh:setRect(self.index,0, 0, self.spriteSize.x, self.spriteSize.y)    
        self.mesh:draw()    
        if self.position.y < 0 then
            self.mesh:clear()
        end
        popStyle()
        popMatrix()
    end
end

function Foe1m:SpawnFoe()
    self.index = self.mesh:addRect(self.position.x, self.position.y, self.spriteSize.x, self.spriteSize.y)    
end

ps : background(0) was put in the main, not to appear as a serial noob :slight_smile:

ok got it working using a table containing the x and y values ( along with the added rect index ) of each rect added to mesh and cycled on.

thanks everyone who helped :slight_smile:

Update - the Wiki mesh page moved to https://bitbucket.org/TwoLivesLeft/core/wiki/mesh