// The following embedded xml is for the editor and describes how the action can be edited: // Supported types are: int, float, string, bool, color, vect3d, scenenode, texture, action /* <action jsname="action_NodeFaceCamera" description="Face active camera"> <property name="nodeToRotate" type="scenenode" default="" /> <property name="rotateOnlyHorizontally" type="bool" default="false" /> </action> */
action_NodeFaceCamera = function () { }
action_NodeFaceCamera.prototype.execute = function (node) { var activeCamPos = ccbGetSceneNodeProperty(ccbGetActiveCamera(), 'Position'); var nodePos = ccbGetSceneNodeProperty(this.nodeToRotate, 'Position'); var nodeRot = ccbGetSceneNodeProperty(this.nodeToRotate, 'Rotation');
var dirVect = nodePos.substract(activeCamPos); var dist = dirVect.getLength();
var horAngle = (Math.atan2(dirVect.x, dirVect.z) * 180 / Math.PI) - 180; var vertAngle = Math.atan2(dirVect.y, dist) * 180 / Math.PI;
ccbSetSceneNodeProperty( this.nodeToRotate, 'Rotation', this.rotateOnlyHorizontally ? nodeRot.x : vertAngle, horAngle, nodeRot.z ); }
nodeToRotate - node which needed to be rotated towards active camera
rotateOnlyHorizontally - should be true in case it's not required to rotate vertically
demo:https://drive.google.com/file/d/...
|