# Pushing object at constant velocity

**URL:** <https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569>\
**Category:** Physics\
**Created:** [August 6, 2013, 5:33pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569 "2013-08-06T17:33:34Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![rxmarccall](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rxmarccall](https://forums.solar2d.com/u/rxmarccall)\
**Post date:** [August 6, 2013, 5:33pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/1 "2013-08-06T17:33:34Z")

</div>

Hey guys,

I have a physics object that I would like to have move at a specific set speed and never go faster or slower. &nbsp;I currently am using applyForce every frame and checking the objects velocity before applying more force.

Just wondering if others have ideas of a better way to do this, or is this the best option? &nbsp;Another option might be to use the “setVelocity” method to keep it at a constant speed.

Any suggestions or comments are appreciated. &nbsp;Wanted to ask becasue this will be the core of my game.

---

<div class="post-metadata">

**Author:** ![aukStudios](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@aukStudios](https://forums.solar2d.com/u/aukStudios)\
**Post date:** [August 6, 2013, 5:48pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/2 "2013-08-06T17:48:08Z")

</div>

Well, in physics (not just the physics engine, but in real-world physics), when you apply a force to a body, it accelerates. &nbsp;(Force = mass x acceleration, so acceleration = Force / mass). &nbsp;If you keep applying a force, it’ll keep accelerating. &nbsp;After you’ve stopped applying a force, it’ll continue moving at the same velocity (inertia) until it encounters another force, which might be a wall, another object, or, importantly, friction.

In the physics engine, there’s a form of friction that’s simulated by the linearDamping property. &nbsp;If you have that property set above 0, then objects won’t maintain a constant velocity as they move through space. &nbsp;They’ll slow down over time, as if there were air resistance. &nbsp;Thus, with linearDamping\>0, you’d have to continue to apply a force to keep the object moving at the same speed, to counteract the damping force of friction. &nbsp;(Note, what I’m describing here is different from the actual “friction” property of a physics object, which has to do with when it’s colliding with another object.)

To get an object to move at a constant velocity, you could just apply a force one time, or set it’s velocity one time, and then just make sure linearDamping is zero.

- Andrew

---

<div class="post-metadata">

**Author:** ![rxmarccall](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rxmarccall](https://forums.solar2d.com/u/rxmarccall)\
**Post date:** [August 6, 2013, 6:04pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/3 "2013-08-06T18:04:30Z")

</div>

Andrew,

Thank you very much for the response, those are some very good points. &nbsp;I will try what you suggest and report back. sounds like it should work out great!

---

<div class="post-metadata">

**Author:** ![aukStudios](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/a/43a26b/32.png) [@aukStudios](https://forums.solar2d.com/u/aukStudios)\
**Post date:** [August 6, 2013, 5:48pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/4 "2013-08-06T17:48:08Z")

</div>

Well, in physics (not just the physics engine, but in real-world physics), when you apply a force to a body, it accelerates. &nbsp;(Force = mass x acceleration, so acceleration = Force / mass). &nbsp;If you keep applying a force, it’ll keep accelerating. &nbsp;After you’ve stopped applying a force, it’ll continue moving at the same velocity (inertia) until it encounters another force, which might be a wall, another object, or, importantly, friction.

In the physics engine, there’s a form of friction that’s simulated by the linearDamping property. &nbsp;If you have that property set above 0, then objects won’t maintain a constant velocity as they move through space. &nbsp;They’ll slow down over time, as if there were air resistance. &nbsp;Thus, with linearDamping\>0, you’d have to continue to apply a force to keep the object moving at the same speed, to counteract the damping force of friction. &nbsp;(Note, what I’m describing here is different from the actual “friction” property of a physics object, which has to do with when it’s colliding with another object.)

To get an object to move at a constant velocity, you could just apply a force one time, or set it’s velocity one time, and then just make sure linearDamping is zero.

- Andrew

---

<div class="post-metadata">

**Author:** ![rxmarccall](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rxmarccall](https://forums.solar2d.com/u/rxmarccall)\
**Post date:** [August 6, 2013, 6:04pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/5 "2013-08-06T18:04:30Z")

</div>

Andrew,

Thank you very much for the response, those are some very good points. &nbsp;I will try what you suggest and report back. sounds like it should work out great!

---

<div class="post-metadata">

**Author:** ![daniel.strmecki](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@daniel.strmecki](https://forums.solar2d.com/u/daniel.strmecki)\
**Post date:** [September 6, 2014, 11:23am UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/6 "2014-09-06T11:23:50Z")

</div>

Here is an example of a ball moving at constant speed:

[lua]

local function moveBall()

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;physics.setGravity( 0, 0 )

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ball.object.gravityScale = 0

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ball.object.linearDamping = 0

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;local x = math.random( -30, 30 )

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;local y = math.random( -30, 30 )

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ball.object:applyForce( x, y, ball.object.x, ball.object.y )

end

local function onBallCollision(self, event)

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if ( event.phase == “began” ) then

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print( self.colorIndex … " collision began with " … event.other.colorIndex )

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;end

end

local bo = display.newCircle( \_W / 2, \_H / 2, ball.size )

bo:setFillColor( 0.5 )

sceneGroup:insert(bo)

–physics.addBody( bo, { density=1.0, friction=0.3, bounce=1, radius=ball.size - 5 } )

physics.addBody( bo, { density=0, friction=0, bounce=1, radius=ball.size } ) --edited

bo.colorIndex = 1

bo.collision = onBallCollision

bo:addEventListener( “collision”, bo )

moveBall()

[/lua]

The trick is to set gravity to 0,0 and ball bounce to 1, so that the ball with not loose its speed after collision.

Daniel

---

<div class="post-metadata">

**Author:** ![davebollinger](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@davebollinger](https://forums.solar2d.com/u/davebollinger)\
**Post date:** [September 6, 2014, 1:46pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/7 "2014-09-06T13:46:55Z")

</div>

if you have a lot of bouncing/collision going on you might still drift off your fixed velocity just due to approximations in box2d (even with damping 0, bounce 1) &nbsp;so depending on how nitpicky you are, you might need to manually correct it every so often

fe, pseudocode:

```
local vx,vy = body:getLinearVelocity() -- velocity components local v = math.sqrt(vx\*vx+vy\*vy) -- velocity magnitude -- print("velocity delta error = ", v-MAX\_VEL) -- optional for debugging local nx,ny = vx/v, vy/v -- normalized local sx,sy = nx\*MAX\_VEL,ny\*MAX\_VEL -- scaled to your constant body:setLinearVelocity(sx,sy) -- reset
```

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [September 6, 2014, 7:51pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/8 "2014-09-06T19:51:32Z")

</div>

Have you tried:&nbsp; setLinearVelocity()?

I’m using it to move my enemies in a game right now at a fixed speed.

---

<div class="post-metadata">

**Author:** ![daniel.strmecki](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@daniel.strmecki](https://forums.solar2d.com/u/daniel.strmecki)\
**Post date:** [September 7, 2014, 11:54am UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/9 "2014-09-07T11:54:35Z")

</div>

Yes, objects can move at a fixed speed using&nbsp;_setLinearVelocity_ or _applyForce_.&nbsp;

But, how to keep that constant speed after collision? I think&nbsp;density was my problem.

This works ok for the example I posted above:

[lua]

physics.addBody( bo, { density=0, friction=0, bounce=1, radius=ball.size } )

[/lua]

Daniel

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [September 7, 2014, 7:13pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/10 "2014-09-07T19:13:46Z")

</div>

You can add collision detection to your object and when you get a collision with an object apply new linearVelocity.&nbsp; Since we don’t know what you’re trying to do, I’m going to make up an example.&nbsp;&nbsp; Lets say you have two walls and you need to move an enemy between the two walls, simply setup a collision filter for the enemy and the walls.&nbsp; When it collides, apply the opposite velocity and you can have an object move back and forth.

Rob

---

<div class="post-metadata">

**Author:** ![daniel.strmecki](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@daniel.strmecki](https://forums.solar2d.com/u/daniel.strmecki)\
**Post date:** [September 7, 2014, 8:55pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/11 "2014-09-07T20:55:52Z")

</div>

Thanks Rob. That’s a good approach. Apply force with density 0 and bounce 1 also works 4 me.

Daniel

---

<div class="post-metadata">

**Author:** ![daniel.strmecki](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@daniel.strmecki](https://forums.solar2d.com/u/daniel.strmecki)\
**Post date:** [September 6, 2014, 11:23am UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/12 "2014-09-06T11:23:50Z")

</div>

Here is an example of a ball moving at constant speed:

[lua]

local function moveBall()

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;physics.setGravity( 0, 0 )

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ball.object.gravityScale = 0

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ball.object.linearDamping = 0

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;local x = math.random( -30, 30 )

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;local y = math.random( -30, 30 )

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ball.object:applyForce( x, y, ball.object.x, ball.object.y )

end

local function onBallCollision(self, event)

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if ( event.phase == “began” ) then

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;print( self.colorIndex … " collision began with " … event.other.colorIndex )

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;end

end

local bo = display.newCircle( \_W / 2, \_H / 2, ball.size )

bo:setFillColor( 0.5 )

sceneGroup:insert(bo)

–physics.addBody( bo, { density=1.0, friction=0.3, bounce=1, radius=ball.size - 5 } )

physics.addBody( bo, { density=0, friction=0, bounce=1, radius=ball.size } ) --edited

bo.colorIndex = 1

bo.collision = onBallCollision

bo:addEventListener( “collision”, bo )

moveBall()

[/lua]

The trick is to set gravity to 0,0 and ball bounce to 1, so that the ball with not loose its speed after collision.

Daniel

---

<div class="post-metadata">

**Author:** ![davebollinger](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@davebollinger](https://forums.solar2d.com/u/davebollinger)\
**Post date:** [September 6, 2014, 1:46pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/13 "2014-09-06T13:46:55Z")

</div>

if you have a lot of bouncing/collision going on you might still drift off your fixed velocity just due to approximations in box2d (even with damping 0, bounce 1) &nbsp;so depending on how nitpicky you are, you might need to manually correct it every so often

fe, pseudocode:

```
local vx,vy = body:getLinearVelocity() -- velocity components local v = math.sqrt(vx\*vx+vy\*vy) -- velocity magnitude -- print("velocity delta error = ", v-MAX\_VEL) -- optional for debugging local nx,ny = vx/v, vy/v -- normalized local sx,sy = nx\*MAX\_VEL,ny\*MAX\_VEL -- scaled to your constant body:setLinearVelocity(sx,sy) -- reset
```

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [September 6, 2014, 7:51pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/14 "2014-09-06T19:51:32Z")

</div>

Have you tried:&nbsp; setLinearVelocity()?

I’m using it to move my enemies in a game right now at a fixed speed.

---

<div class="post-metadata">

**Author:** ![daniel.strmecki](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@daniel.strmecki](https://forums.solar2d.com/u/daniel.strmecki)\
**Post date:** [September 7, 2014, 11:54am UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/15 "2014-09-07T11:54:35Z")

</div>

Yes, objects can move at a fixed speed using&nbsp;_setLinearVelocity_ or _applyForce_.&nbsp;

But, how to keep that constant speed after collision? I think&nbsp;density was my problem.

This works ok for the example I posted above:

[lua]

physics.addBody( bo, { density=0, friction=0, bounce=1, radius=ball.size } )

[/lua]

Daniel

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [September 7, 2014, 7:13pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/16 "2014-09-07T19:13:46Z")

</div>

You can add collision detection to your object and when you get a collision with an object apply new linearVelocity.&nbsp; Since we don’t know what you’re trying to do, I’m going to make up an example.&nbsp;&nbsp; Lets say you have two walls and you need to move an enemy between the two walls, simply setup a collision filter for the enemy and the walls.&nbsp; When it collides, apply the opposite velocity and you can have an object move back and forth.

Rob

---

<div class="post-metadata">

**Author:** ![daniel.strmecki](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@daniel.strmecki](https://forums.solar2d.com/u/daniel.strmecki)\
**Post date:** [September 7, 2014, 8:55pm UTC](https://forums.solar2d.com/t/pushing-object-at-constant-velocity/316569/17 "2014-09-07T20:55:52Z")

</div>

Thanks Rob. That’s a good approach. Apply force with density 0 and bounce 1 also works 4 me.

Daniel
