How to track the velocities of multiple objects?

Thanks for the help.

I test the both method on the simulator and on my device (huawei y6), and it’s often 3 times longer if I give a runtime to each objects.

With 1000 balls, it’s like 25 seconds vs 8 seconds.

Code Efficiency

The primary cause of lag is usually expensive calculations which is why, in this case, I used the squared-length trick, thus avoiding the costly square root calculation for true length.

Nice, it’s a programmer’s trick that I don’t know. Thanks lol.

@riku.kukonen

After posting I realized we could make this much faster by skipping the getLinearVelocity() call entirely.  

Instead measure change in position from frame to frame.

The following is a combination of this idea and @yvandotet’s single listener approach.

local minDist = 1 local tab={} tab.enterFrame=function(self,event) if #self==0 then Runtime:removeEventListener( "enterFrame", self ) self.enterFrame=nil return end for k=#self,1,-1 do local o=self[k] local dx = o.x - o.lx local dy = o.y - o.ly o.lx = o.x o.ly = o.y if( ( dx\*dx + dy\*dy ) \<= o.minDist ) then table.remove( self,k ) display.remove(o) end end end Runtime:addEventListener( "enterFrame", tab ) -- local function myDummyObjectBuilder( x, y, vx, vy, minRate ) local obj = display.newCircle( x, y, 10 ) physics.addBody( obj ) obj:setLinearVelocity( vx, vy ) obj.minDist = minDist obj.lx = obj.x obj.ly = obj.y obj.linearDamping = 1 tab[#tab+1]=obj return obj end physics.setGravity(0,0) for k=1,500 do local x,y,vx,vy=math.random(w),math.random(h),math.random(-100,100),math.random(-100,100) myDummyObjectBuilder(x,y,vx,vy,1) end

The biggest performance gains come with precalculation always. Never do something per frame when you can do it once!

interesting topic, very helpful. good stuff.

any idea the best way to check if a physics object is stationary when standing on a moving platform, such as an elevator ?

so the platform (floor object) is always moving about the screen (up,down, left, right…), and the check is to determine if the physics object (player) is stationary or moving about (walking, jumping upon … etc) relative to the moving platform.

cheers in advance for any advice.

@leemacread,

Hi.  You should probably start a new post and refer back to this one if you think it is relevant.

Adding a new question to an existing/idle thread is seen as ‘thread hijacking’ and won’t get you good answers.

no worries, I sorted it