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

**URL:** <https://forums.solar2d.com/t/how-to-set-a-constant-velocity-that-its-not-affected-when-an-objects-hits-something/353107>\
**Category:** Games\
**Created:** [February 5, 2021, 3:39pm UTC](https://forums.solar2d.com/t/how-to-set-a-constant-velocity-that-its-not-affected-when-an-objects-hits-something/353107 "2021-02-05T15:39:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon77699500](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@anon77699500](https://forums.solar2d.com/u/anon77699500)\
**Post date:** [February 5, 2021, 3:39pm UTC](https://forums.solar2d.com/t/how-to-set-a-constant-velocity-that-its-not-affected-when-an-objects-hits-something/353107/1 "2021-02-05T15:39:46Z")

</div>

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
	cell = display.newImageRect( "cell.png", 70, 100 )
	cell.x=math.random(display.contentWidth)
	cell.y=math.random(display.contentHeight)
	

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

--virus

local virus = display.newImageRect("virus.png", 90,90)
virus.x=display.contentCenterX
virus.y=display.contentCenterY
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.

---

<div class="post-metadata">

**Author:** ![famousdoggstudios](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/f/94ad74/32.png) [@famousdoggstudios](https://forums.solar2d.com/u/famousdoggstudios)\
**Post date:** [February 6, 2021, 5:31pm UTC](https://forums.solar2d.com/t/how-to-set-a-constant-velocity-that-its-not-affected-when-an-objects-hits-something/353107/2 "2021-02-06T17:31:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon77699500](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@anon77699500](https://forums.solar2d.com/u/anon77699500)\
**Post date:** [February 8, 2021, 4:48pm UTC](https://forums.solar2d.com/t/how-to-set-a-constant-velocity-that-its-not-affected-when-an-objects-hits-something/353107/3 "2021-02-08T16:48:50Z")

</div>

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.name=“cell”  
end  
Runtime:addEventListener(“enterFrame”,moveCell)

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

---

<div class="post-metadata">

**Author:** ![jedihenryk](https://forums.solar2d.com/user_avatar/forums.solar2d.com/jedihenryk/32/426_2.png) [@jedihenryk](https://forums.solar2d.com/u/jedihenryk)\
**Post date:** [February 10, 2021, 6:40pm UTC](https://forums.solar2d.com/t/how-to-set-a-constant-velocity-that-its-not-affected-when-an-objects-hits-something/353107/4 "2021-02-10T18:40:52Z")

</div>

I [checked the docs](https://docs.coronalabs.com/guide/physics/physicsBodies/index.html#physical-properties), does setting bounce to 1.0 work for you?

```auto
    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](https://docs.coronalabs.com/api/library/physics/setGravity.html):

```auto
    physics.setGravity(0, 0)

```

---

<div class="post-metadata">

**Author:** ![anon77699500](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@anon77699500](https://forums.solar2d.com/u/anon77699500)\
**Post date:** [February 11, 2021, 5:12pm UTC](https://forums.solar2d.com/t/how-to-set-a-constant-velocity-that-its-not-affected-when-an-objects-hits-something/353107/5 "2021-02-11T17:12:48Z")

</div>

Yes thank you!
