How to set a constant velocity that its not affected when an objects hits something?

Hello, I’m creating a game and I’ve been having some problems with making the objects move the way I want. So basically I have this group of objects that move randomly within the screen borders (I created some walls to avoid them going off screen). I also have another object that moves randomly within the screen but it is not part of the group in fact, it is the “enemy”. I applied linear velocity to all my objects. I’ll paste the code.

–creation of 10 cells
math.randomseed( os.time() )
for i = 1, 10, 1 do

	cell = display.newImageRect( "cell.png", 70, 100 )

--cell physics 
	physics.addBody(cell, "dynamic", {density=1, friction=0.0, bounce=0.9, radius=30});
    cell:setLinearVelocity ( 5000, 5000 )


local virus = display.newImageRect("virus.png", 90,90)
physics.addBody(virus, "dynamic", {density=1, friction=0.0, bounce=0, radius=15});

virus:setLinearVelocity ( 500, 500 )

Everything seems to function properly but… in the long run each objects lose its velocity until it only “floats” on the screen. I set the velocity to higher numbers but it doesn’t work as their behaviors doesn’t change. They only move faster in the beginning (which I don’t like by the way).
i Just want my objects to keep having the same velocity throughout the game even when they hit each other or the walls… what should i do?
thanks in advance.

Defining these as kinematic objects instead of dynamic could help but I’m not sure you’d still get the same kind of response to collisions and gravity that you want. Alternatively, you can add an update routine (enterFrame listener) and set their velocity in each frame and still keep them dynamic. It seems you only set their velocity once right now which will change as they collide and slow down so you need yo constantly override it in each frame

I already tried both these methods but they won’t work… This is the code I’m trying to run right now:

function moveCell(event)
local randomX, randomY = math.random ( -100, 100 ), math.random ( -100, 100 )
cell:setLinearVelocity ( 700, 700 )“cell”

It seems like the cell constantly gets updated in the same position and never really moves except for a fraction of second.

I checked the docs, does setting bounce to 1.0 work for you?

    physics.addBody(cell, "dynamic", {density=1, friction=0.0, bounce=1.0, radius=30});

Also, depending on what you want to achieve, you might want to disable gravity:

    physics.setGravity(0, 0)
1 Like

Yes thank you!