# An exception occurred when getting the rotation property of an object

**URL:** <https://discourse.threejs.org/t/an-exception-occurred-when-getting-the-rotation-property-of-an-object/21444>\
**Category:** Questions\
**Tags:** rotation, physics, ammo-ammojs\
**Created:** [December 10, 2020, 4:01am UTC](https://discourse.threejs.org/t/an-exception-occurred-when-getting-the-rotation-property-of-an-object/21444 "2020-12-10T04:01:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![OliverXH](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/oliverxh/32/13263_2.png) [@OliverXH](https://discourse.threejs.org/u/OliverXH)\
**Post date:** [December 10, 2020, 4:01am UTC](https://discourse.threejs.org/t/an-exception-occurred-when-getting-the-rotation-property-of-an-object/21444/1 "2020-12-10T04:01:19Z")

</div>

An exception occurred when getting the rotation property of an object.

I’m using Ammo.js as the physics engine and creating a vehicle,. I’m trying to get the body rotation attributes around the y-axis and output them in the console.

I found that the output value is between-PI/2~PI/2, and the value of the rotation property decreases when the PI/2 is reached, and the value of the rotation property increases when the-PI/2 is reached.

To test, I created another mesh, and assigned a value to it using the rotation of vehicle’s body. The exception is shown in the following video:

[Video\_20201210115858.wmv](https://discourse.threejs.org/uploads/short-url/tTKVp8Zoc6PFxCPb2UGVFlVwcid.wmv) (1.7 MB)

I’m not sure why this happened. I hope someone can help me.

Thanks!

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/yannick/32/8539_2.png) [@yannick](https://discourse.threejs.org/u/yannick)\
**Post date:** [December 10, 2020, 12:14pm UTC](https://discourse.threejs.org/t/an-exception-occurred-when-getting-the-rotation-property-of-an-object/21444/2 "2020-12-10T12:14:03Z")

</div>

_I could not view your video on my phone, so I converted it._

[https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/2/2e34811f79e8b9020573ca95c849800234001587.webm](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/2/2e34811f79e8b9020573ca95c849800234001587.webm)

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/yannick/32/8539_2.png) [@yannick](https://discourse.threejs.org/u/yannick)\
**Post date:** [December 10, 2020, 3:42pm UTC](https://discourse.threejs.org/t/an-exception-occurred-when-getting-the-rotation-property-of-an-object/21444/3 "2020-12-10T15:42:54Z")

</div>

Can you share some of your code? I think I once had the same issue, but can’t reproduce it anymore 😕

---

<div class="post-metadata">

**Author:** ![OliverXH](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/oliverxh/32/13263_2.png) [@OliverXH](https://discourse.threejs.org/u/OliverXH)\
**Post date:** [December 11, 2020, 3:20am UTC](https://discourse.threejs.org/t/an-exception-occurred-when-getting-the-rotation-property-of-an-object/21444/4 "2020-12-11T03:20:40Z")

</div>

Here is my code:

```javascript
let tmpTrans = new Ammo.btTransform();

let ms = carBody.getMotionState();
ms.getWorldTransform(tmpTrans);

let p = tmpTrans.getOrigin();
let q = tmpTrans.getRotation();

carMesh.position.set(p.x(), p.y(), p.z());
carMesh.quaternion.set(q.x(), q.y(), q.z(), q.w());

```

Then I created another mesh, and assigned a value to it using the rotation of vehicle’s body:

```javascript
newMesh.rotation.y = carMesh.rotation.y;

```

And then there was the situation in the video.

---

<div class="post-metadata">

**Author:** ![mjurczyk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mjurczyk/32/10995_2.png) [@mjurczyk](https://discourse.threejs.org/u/mjurczyk)\
**Post date:** [December 11, 2020, 3:38am UTC](https://discourse.threejs.org/t/an-exception-occurred-when-getting-the-rotation-property-of-an-object/21444/5 "2020-12-11T03:38:36Z")

</div>

I keep forgetting how it worked with `.rotation` - but it’s possible that when you cross `0 / Math.PI`, `rotation.y` resets to once again fit that interval, and `rotation.x / rotation.z` change signs.

In general, use quaternions over rotations.

```javascript
newMesh.quaternion.copy(carMesh.quaternion);
newMesh.quaternion.x = newMesh.quaternion.z = 0; // If you care only about y-axis

```

---

<div class="post-metadata">

**Author:** ![OliverXH](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/oliverxh/32/13263_2.png) [@OliverXH](https://discourse.threejs.org/u/OliverXH)\
**Post date:** [December 11, 2020, 5:46am UTC](https://discourse.threejs.org/t/an-exception-occurred-when-getting-the-rotation-property-of-an-object/21444/6 "2020-12-11T05:46:03Z")

</div>

I don’t understand what you mean by this

cross `0 / Math.PI`,

but I have tried the second method of using quaternions, and the result is still the same as in the video.

---

<div class="post-metadata">

**Author:** ![yannick](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/yannick/32/8539_2.png) [@yannick](https://discourse.threejs.org/u/yannick)\
**Post date:** [December 11, 2020, 1:17pm UTC](https://discourse.threejs.org/t/an-exception-occurred-when-getting-the-rotation-property-of-an-object/21444/7 "2020-12-11T13:17:59Z")

</div>

I assume you are using raycastVehicle?

You can do this:

```javascript
animate() {  
  let quat = raycastVehicle.getRigidBody().getWorldTransform().getRotation()
  let rad = this.quaternionToRadian(quat)

  newMesh.rotation.y = rad.y
}

quaternionToRadian(ammoQuat) {
  let x, y, z

  let q1 = new THREE.Quaternion(ammoQuat.x(), ammoQuat.y(), ammoQuat.z(), ammoQuat.w())
  if (q1.w > 1) q1 = q1.normalize()

  const angle = 2 * Math.acos(q1.w)
  const s = Math.sqrt(1 - q1.w * q1.w)

  if (s < 0.001) {
    x = q1.x
    y = q1.y
    z = q1.z
  } else {
    x = q1.x / s
    y = q1.y / s
    z = q1.z / s
  }
  return { x: x * angle, y: y * angle, z: z * angle }
}

```
