Looking for a rotate script please


Is there a very simple script to make an object rotate on it’s own axis please.
And one to make the object rotate on an other objects axis.
I want it kept simple so that I can get a better understanding of the language.




Thanks but I am looking for something more simple that I can understand and expand on. The code on that page has no commenting so it is hard to follow. I always comment my coding because commenting makes is easy to understand what is going on if the code does not work as planned.


I think that is as simple worded as it can get without getting overly complex or optimized with tricks:

Let me comment it for you. (and fix some of it for @Richardus.Raymaker as well since I saw a few places that could have been straightened. (havent tested this that yet, but this is all based on theory, but generally the same concepts apply as in Richardus script.)

//  Entity-Door.js V0.03M
//  Created by Richardus Raymaker on 04-09-2015 - Modified  / Commented by Menithal -3.7.15

//  Distributed under the Apache License, Version 2.0.
//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html

(function () {
     initial Parameters:
    // Angle in degrees at which the doors opens
    var doorAngle = 90;
    // How long should we take?
    var animationTime = 1;
    // Is the door open?
    var doorIsOpen = false;
    // has it stopped moving?
    var stopMove = true;
    // Storage for ID
    var doorEntityID;
    // current relative angle
    var i = 0;
    // Corrective rotations
    var pitch = 0.0;
    var yaw = 0.0;
    var roll = 0.0;

    // Function to move the door
    function movedoor(deltaTime) {
        if (!stopMove) // Are we moving?
            // deltaTime should be the difference of time from last call of this method:
            // current time from last update deltaTime, which consist of a certain % of overall time we want the door to make.
            // multiply this with to the target angle, and you have a small angle you can use per frame to meet that target animation Time.
            var deltaAngle = (doorAngle * ( deltaTime/animationTime));
            // If closed, open, if open, close.
            if (!doorIsOpen) {
                i = i + deltaAngle;
                if(i>=doorAngle) i = doorAngle; // make sure angle never goes more than
            } else { 
                i = i - deltaAngle;
                if(i<=0) i = 0; // make sure angle never goes less than
            //  if door is not full open, and is opening OR
            // if door is not fully closed, and is closing, continue rotating
            if (i <= doorAngle && !doorIsOpen || i >= 0 && doorIsOpen) {
                // build a new quaternion from Euler Pitch, Yaw, Roll

                rotation = Quat.fromPitchYawRollDegrees(pitch, yaw + i, roll);
                // store to a property map
                var newProperties =
                    rotation: rotation
                // Apply rotation change
                Entities.editEntity(doorEntityID, newProperties); // set doorId's properties to newProperties
            else {
                // if door angle is larger or equal, then we are technically open and can stop moving.
                // we are now in an opposite state.                    
                doorIsOpen = !doorIsOpen;
                stopMove = true;
                // remove the movedoor method from frame updates.


    // On click on Entity EnitityID
    this.clickDownOnEntity = function (entityID, mouseEvent) {

       // Make sure we do not accidentally animate the door again after pressing.
        if (stopMove) {
            doorEntityID = entityID;

            // Add movedoor function to frame updates (run every time frame is rendered)
            stopMove = false;

I think ill write entirely my own version later as theres a few things missing from this, and it script also has the assumption all doors at the same initial rotation and rotation direction…


Something with that protection failed when i tried it in the time i made the script. that’s the reason i have not put it back. The door stopped responding, or something like that.


The stopMove check specifically had issues?


I just tested your script and am having the problem that i can open the door once.
And then you never can close it anymore.

Same problem that keeped me puzzled so that’s why i changed it. to have at least a working door.


Oh dear I worded this wrong. I really wanted a script that would make an object spin forever.
I have a few door scripts but I doubt they will work with the Interface.


Then simply: (untested, based on theory)

(function () {
  var spinrate = 30; // Rate at which it is going to spin, seconds to complete a full spin
  var id;
  var _counter = 0; // Do not modify this
  // Spin function
  var spin = function(deltaTime){
    var deltaRotation = (360 * (deltaTime/spinrate))

    _counter = _counter + deltaRotation;
    rotation = Quat.fromPitchYawRollDegrees(0, _counter, 0);
    Entities.editEntity(id, {rotation: rotation }); 
  this.enterEntity = function(entityId){
   id = entityId;
  // bind spin function to frame update


Thanks. Next is how do I get the code to work on an object please?


Direct Code Injection
Write the script and copy it
Paste the script text into the Script window of the entity Properties Panel and then click OK.

More with photos here: https://docs.highfidelity.com/v1.0/docs/attaching-a-script-to-an-entity


Thanks I will have to read more.
I do not see Script window of the entity Properties Panel I have used Amazon to host the script but I see no action.
After I reload the Interface stops working
I will have a look at the Planet script and see how that words.
Thanks for the help.


Use the one I have at the top. Kept editing it in quite alot in, and it seems your have the very first version.

I didnt test as was just quickly typing pseudo code. the new one should be more uptodate after reading it over a few times.

again, written entirely by theory (JS Theory) , and I gotta get home to make sure it works, but some who currently has the client loaded up and has more experience in actual working scripts can look at the above and fix it.


ok. I will try that. thanks


Hey I tried the colour changing script and it works fine. But the information on the page is out of date.


Just clicked the test door again, it’s working once, then you need to relog to get it working again.


But I did not ask for a door opening script. I swear I am going mad. but never mind I have Googled for such a code and it does not exist. It is not possible to make an object spin with JavaScript


As said, earlier ill probably look into writing a proper door script on Sunday which should be adjustable using the custom parameters.
I Finally got home an hour ago from work and actually tested the scripts and fixed the issues.

And yes, It is possible to spin objects . and here is the proof:
Fixed both scripts I posted earlier from work which technically were pseudo code (as i said, I did NOT test them when I first posted the scripts, as was expecting it to give an idea of the direction the scripts should be done,) Now i know better not to post incomplete scripts.

Fixed version of Richards door script. Use registration parameter to switch pivot point for the door as described in Richards post here. Its a floating point % value, so 1.0 = 100% of the width, while 0.5, 0.5, 0.5 means dead center of the object…

Here is the spin script:
http://www.norteclabs.com/HF/scripts/spin.js Just add and it should start spinning:

Alternatively, without scripts you can always set an angular velocity to an object if you just want to make something spin, while making sure angular dampening is 0:

As a side note @Richardus.Raymaker the reason it was doing it was due to a logic fault: basically the if clause never completed because it never went to else for both versions. The above version fixes that.

Also @subongo, don’t rely on google for scripts specifically for high fidelity; its better to wait till someone has them solved, or try to dig in your self as of the moment specifically for Hifi: Most conventions are for three.js, and thats where things get a lot more complicated: Similar concepts apply here too though, like with any other 3D scripting:
HF is not big enough for there to be any tutorials out there yet.


yes you can
Its already part of the Entity Properties
.>Edit .>Rez a Cube (Box) .> Entity Properties
0-0 see Angular Velocity & Angular Damping fields
0-0 Angular Velocity 3 fields to set rate of spin for xyz
0-0 Angular Damping - slows the spin in decay to zero, 0.5 will slow it quite quickly

Re you ‘googled’ - the problem is, there are lots of .js script for web development but not much for 3D graphics and what there is gets complicated really quickly. This needs to be adressed and Philip did mnetion this at meeting 26th June

ok i’m not a coder but
… here is a snippet i have for spinning a box

// addEntity()_with angular velocity

// create variable - for position in front of your avatar where you want  to rez the box
var position = Vec3.sum(MyAvatar.position, Quat.getFront(MyAvatar.orientation));

//create a variable for all the values for your Box
var properties = {
  type: “Box”,
// the var you created as to where to rez box
  position: position,
//its color
  color: { red: 0, green: 200, blue: 0 },
//its dimensions
  dimensions: {x: 1.00, y: 2.01, z: 0.02},
// set angular velocity - the spin value for xyz
  angularVelocity:{x:5, y:0, z:0},
// set angular damping -  a value of 0.5 here will slow it quite quickly
  angularDamping: 0.01  
Ent = Entities.addEntity(properties);
//print to log that entity was added
print(“Entity added”);

//here it is again without the comments

var position = Vec3.sum(MyAvatar.position, Quat.getFront(MyAvatar.orientation));  
var properties = {
  type: “Box”,
  position: position,
  color: { red: 0, green: 200, blue: 0 },
  dimensions: {x: 1.00, y: 2.01, z: 0.02},
  angularVelocity:{x:5, y:0, z:0},
  angularDamping: 0.01  
Ent = Entities.addEntity(properties);

print(“Entity added”);

…>> Save file as rotate.js on you HD
.> HiFi .>File .>Open and Run Script File .>navigate to where you saved it


Thank you the spin script works