Most efficient way to move several objects

Hi! I’m building a game and I need some advice since I’m new to Solar2d. I need to move several circles in a random way inside the edges of the screen. My first question is, should I create a function that creates the circles or is it better if i create every single circle myself? Mind that I only need 10 of them so there is no need of new circles spawning. And to move them I wrote this code to give the the initial “push”.

function moveVirus()
Runtime:addEventListener( “enterFrame”, moveVirus)

Is this a good method?

This really depends on how you plan on moving the objects.

If they are physics objects, then you’d be better off with assigning them a linear velocity. If the objects wouldn’t be affected by gravity and/or damping, then they wouldn’t lose any speed unless they collide with objects. Applying linear impulse leads to unpredictable speeds and doing so within an enterFrame listener will probably result in your objects flying out of the screen pretty quickly.

If you aren’t dealing with physics objects, then the most efficient way is to use the translate method (

Further optimisations would really come down to the specifics of how you’ve created the objects and how you are handling them in your game.

1 Like

Yes, they are physics objects. There are two types of these objects (both physics) that i need to move randomly. And I’m going to apply an event listener for when one type of object collide with the other. I tried to set LinearVelocity but it doesn’t move at all within an enterFrame listener. Am i missing something? My code is the same but instead of applyLinearImpulse i put setLinearVelocity.

Linear velocity probably has no impact because you aren’t assigning a high enough velocity for the object. The difference between impulse and velocity is that impulse adds to the existing physics forces whereas velocity will completely overwrite the existing forces. This is why giving a small nudge 60 times a second can move your objects and why constantly assigning the same velocity to an object (if it isn’t strong enough to move it) won’t do a thing.

Generally, you should avoid applying forces to physics objects inside enterFrame loops because they will likely interfere with the other physics simulations. Think of a ball hitting a wall, but at the same time they are being given a new impulse to some random direction every 17ms. The collisions would likely look off.

Also, remember that just because you have a physics object doesn’t necessarily mean it needs to be moved like one.

1 Like

You might want to just use for loop to keep code cleaner.

local physics = require "physics"
physics.start ()
physics.setGravity( 0,0 )
math.randomseed( os.time() )

for i = 1, 10, 1 do
    local virus = display.newCircle ( 60*i, 100, 50 )
    physics.addBody ( virus, { radius=50, bounce=1 } ) --dont loose energy on collisions
    local randomX, randomY = math.random ( -100, 100 ), math.random ( -100, 100 )
    virus:setLinearVelocity ( randomX, randomY )

One problem with this code is that if randomX and randomY are too small, object wont move until others colide with it but this could be solved. Also there are no bounderies so circles will escape screen pretty quickly…

1 Like

Thanks! This gave me an idea on how to do it. I already have walls, now I have to figure out how to keep my circles moving. :slight_smile:

Thanks! It works if I set an higher value!