# Simple Rotation Limiter Node

Discussion in 'BBNodes' started by Muspar, Oct 5, 2019.

Tags:
1. ### MusparBoxer

Joined:
Sep 27, 2019
Messages:
9
6
Sorry if this is the wrong place for this. In my most recent game, I needed a way to limit a kinematic object's rotation to stay within certain bounds while using the touch rotate control node. There may be a way to do this natively in Buildbox but I couldn't find it. So I modified the Position Limiter node so that it would lock the object's XYZ rotation instead of position and figured other people might find this useful too.

To do this, drag a Position Limiter node into the object's node map, then edit the code and replace it with the code at the end of my post. Then you can enter the minimum and maximum values just like you would in a regular position limiter node and the object won't rotate outside of those bounds.

var _max;
var _min;
var _enabled = false;
function init(){
_max = this.attribute('Max');
_min = this.attribute('Min');
}

function start(){

}

function update(dt){
if(!_enabled){
return;
}

let rot = this.entity().rotation();
if(rot.x > _max.x){
rot.x = _max.x;
}
if(rot.y > _max.y){
rot.y = _max.y;
}
if(rot.z > _max.z){
rot.z = _max.z;
}

if(rot.x < _min.x){
rot.x = _min.x;
}
if(rot.y < _min.y){
rot.y = _min.y;
}
if(rot.z < _min.z){
rot.z = _min.z;
}

this.entity().setRotation(rot.x, rot.y, rot.z);
}

function signal(name, value){
_enabled = value;
}

2. ### blhpBoxer

Joined:
Dec 8, 2015
Messages:
5
0
Thanks a lot for this!

3. ### MarcGBoxer

Joined:
May 10, 2020
Messages:
35
9
Thank you, this solved an issue for me.

4. ### volcankSerious Boxer

Joined:
Oct 8, 2015
Messages:
795
393
I couldn't appreciate this more man! Thank you so much just what I needed at the moment!

5. ### rizwanashrafAvid Boxer

Joined:
Dec 3, 2015
Messages:
345
215
haha okay okay

volcank likes this.
6. ### Josh (Nology Games)Avid Boxer

Joined:
Nov 27, 2017
Messages:
200
155
awesome stuff!

volcank likes this.
7. ### BluemusicBoxer

Joined:
Apr 7, 2020
Messages:
59
10
I just found this thread. It is exactly what I need but if I delete whats inside Position Limiter and replace with this code the object goes crazy and moves all over the place. Does it work with latest Buildbox?

8. ### landofgreendevBoxer

Joined:
Nov 9, 2018
Messages:
85
18
I know this was from a while ago - but did you figure it out? I'm having the same issue

Joined:
Jul 21, 2018
Messages:
612
361
This should work if you properly set it to work for - / + degrees

Code:
```var _max;
var _min;
var _enabled = false;

function init() {
_max = this.attribute('Max');
_min = this.attribute('Min');
}

function update(dt) {
if (!_enabled) {
return;
}

let rot = this.entity().rotation();
if (_max.x != null && rot.x > _max.x) {
rot.x = _max.x;
}
if (_max.y != null && rot.y > _max.y) {
rot.y = _max.y;
}
if (_max.z != null && rot.z > _max.z) {
rot.z = _max.z;
}

if (_min.x != null && rot.x < _min.x) {
rot.x = _min.x;
}
if (_min.y != null && rot.y < _min.y) {
rot.y = _min.y;
}
if (_min.z != null && rot.z < _min.z) {
rot.z = _min.z;
}

this.entity().setRotation(rot.x, rot.y, rot.z);
}

function signal(name, value) {
_enabled = value;
}```

landofgreendev likes this.

Joined:
Jul 21, 2018
Messages:
612
361
In some rare cases when you would like to limit only the positive values of degrees you can use this:

Code:
```var _max;
var _min;
var _enabled = false;

function init() {
_max = this.attribute('Max');
_min = this.attribute('Min');
}

function start() {

}

function update(dt) {
if (!_enabled) {
return;
}

let rot = getPositiveDegreesFromRotation(this.entity().rotation());
if (_max.x != null && rot.x > _max.x) {
rot.x = _max.x;
}
if (_max.y != null && rot.y > _max.y) {
rot.y = _max.y;
}
if (_max.z != null && rot.z > _max.z) {
rot.z = _max.z;
}

if (_min.x != null && rot.x < _min.x) {
rot.x = _min.x;
}
if (_min.y != null && rot.y < _min.y) {
rot.y = _min.y;
}
if (_min.z != null && rot.z < _min.z) {
rot.z = _min.z;
}

this.entity().setRotation(rot.x, rot.y, rot.z);
}

function signal(name, value) {
_enabled = value;
}

function getPositiveDegreesFromRotation(rotation) {
const x = (rotation.x + 360) % 360;
const y = (rotation.y + 360) % 360;
const z = (rotation.z + 360) % 360;
return new THREE.Vector3(x, y, z);
}```

landofgreendev likes this.