# Check for entity movement

**URL:** <https://forum.playcanvas.com/t/check-for-entity-movement/5885>\
**Category:** Help & Support\
**Created:** [January 29, 2018, 6:49pm UTC](https://forum.playcanvas.com/t/check-for-entity-movement/5885 "2018-01-29T18:49:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@Ryan](https://forum.playcanvas.com/u/Ryan)\
**Post date:** [January 29, 2018, 6:49pm UTC](https://forum.playcanvas.com/t/check-for-entity-movement/5885/1 "2018-01-29T18:49:35Z")

</div>

I found this code somewhere on a forum and it sounded like it would be what I’m looking for, but it seems to be called way to frequently and always before my entities begin to move.

```javascript
CheckRoll.prototype.update = function(dt)
{   
    for(i=0;i<dice.length;i++)
    {
        this.dice[i].currPos = this.entity.getLocalPosition();
        if(this.manager.script.gameManager.removedCoins === true)
        {
            if(this.dice[i].currPos === this.dice[i].lastPos)
            {
                console.log('Dice' + i + 'stopped');
            }
            this.dice[i].lastPos = this.dice[i].currPos;
        }
    }
};

```

I’m not sure if I should make a new function for this, or add a timer/delay to the update.

---

<div class="post-metadata">

**Author:** ![mcmorry](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mcmorry/32/4845_2.png) [@mcmorry](https://forum.playcanvas.com/u/mcmorry)\
**Post date:** [January 29, 2018, 8:58pm UTC](https://forum.playcanvas.com/t/check-for-entity-movement/5885/2 "2018-01-29T20:58:43Z")

</div>

Instead of a timer or a delay I would skip whole code until you applied the movement to your dices. A ‘started’ flag true/false could suffice.

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [January 29, 2018, 11:27pm UTC](https://forum.playcanvas.com/t/check-for-entity-movement/5885/3 "2018-01-29T23:27:11Z")

</div>

> [@Ryan](#):
>
> `this.dice[i].lastPos = this.dice[i].currPos`

This line doesn’t do a value copy, it just copies the reference so both variables now point to the same `Vec3` object.

Assuming that `this.dice[i].lastPos` is already a `Vec3`, then you would want:

```javascript
this.dice[i].lastPos.copy(this.dice[i].currPos);

```

* * *

This line also doesn’t check by value, it just compares references:

```javascript
this.dice[i].currPos === this.dice[i].lastPos

```

Comparing floating points for equal value generally doesn’t work due to floating point inaccuracy so you should check if the distance between the two points is lower than `x`. eg:

```javascript
var d = new pc.Vec3();
d.sub2(this.dice[i].currPos, this.dice[i].lastPos);
if (d.lengthSq() < 0.01) {
  // Do something
}

```

* * *

Of course, since you are using physics, if you really want to know if something has stopped moving, you can check the rigid body’s velocity and if it’s below a certain value or 0, it has stopped moving.

---

<div class="post-metadata">

**Author:** ![Ryan](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@Ryan](https://forum.playcanvas.com/u/Ryan)\
**Post date:** [January 30, 2018, 3:22pm UTC](https://forum.playcanvas.com/t/check-for-entity-movement/5885/4 "2018-01-30T15:22:21Z")

</div>

> [@yaustar](#):
>
> Of course, since you are using physics, if you really want to know if something has stopped moving, you can check the rigid body’s velocity and if it’s below a certain value or 0, it has stopped moving.

Ok, I understand how this would work. But would I have to write a new function to get the linear velocity or is there already one?

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [January 30, 2018, 3:35pm UTC](https://forum.playcanvas.com/t/check-for-entity-movement/5885/5 "2018-01-30T15:35:31Z")

</div>

[https://developer.playcanvas.com/en/api/pc.RigidBodyComponent.html#linearVelocity](https://developer.playcanvas.com/en/api/pc.RigidBodyComponent.html#linearVelocity)

---

<div class="post-metadata">

**Author:** ![Ryan](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@Ryan](https://forum.playcanvas.com/u/Ryan)\
**Post date:** [January 30, 2018, 3:37pm UTC](https://forum.playcanvas.com/t/check-for-entity-movement/5885/6 "2018-01-30T15:37:27Z")

</div>

Right, that’s how to use it, but is there an inherent getVelocity() method in the engine, or do I

> [@Ryan](#):
>
> write a new function

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [January 30, 2018, 4:13pm UTC](https://forum.playcanvas.com/t/check-for-entity-movement/5885/7 "2018-01-30T16:13:06Z")

</div>

It is a property so you just access it directly.

ie:

```javascript
if (this.entity.rigidbody.linearVelocity.lengthSq() > 0.01) {
  // Do something
}

```
