Simple Rotation Limiter Node

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

  1. Muspar

    Muspar Boxer

    Joined:
    Sep 27, 2019
    Messages:
    9
    Likes Received:
    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. blhp

    blhp Boxer

    Joined:
    Dec 8, 2015
    Messages:
    5
    Likes Received:
    0
    Thanks a lot for this!
     
  3. MarcG

    MarcG Boxer

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

    volcank Serious Boxer

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

    rizwanashraf Avid Boxer

    Joined:
    Dec 3, 2015
    Messages:
    345
    Likes Received:
    215
    :D haha okay okay :p
     
    volcank likes this.
  6. Josh (Nology Games)

    Josh (Nology Games) Avid Boxer

    Joined:
    Nov 27, 2017
    Messages:
    200
    Likes Received:
    155
    awesome stuff!
     
    volcank likes this.
  7. Bluemusic

    Bluemusic Boxer

    Joined:
    Apr 7, 2020
    Messages:
    59
    Likes Received:
    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. landofgreendev

    landofgreendev Boxer

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

    Vlad-NY Serious Boxer

    Joined:
    Jul 21, 2018
    Messages:
    613
    Likes Received:
    362
    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.
  10. Vlad-NY

    Vlad-NY Serious Boxer

    Joined:
    Jul 21, 2018
    Messages:
    613
    Likes Received:
    362
    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.

Share This Page