Add Vec3 function to convert vec3 to/from euler angles

This commit is contained in:
bwent 2015-07-20 10:59:55 -07:00
parent cf88bce082
commit 89eed589e2
3 changed files with 66 additions and 45 deletions
examples
libraries/script-engine/src

View file

@ -1211,49 +1211,6 @@ PropertiesTool = function(opts) {
webView.setVisible(visible);
};
vecToPolar = function(direction) {
var x = direction.x;
var y = direction.y;
var z = direction.z;
var pitch, yaw;
pitch = -Math.asin(y);
var c = Math.cos(-pitch);
if (Math.abs(pitch) > (Math.PI / 2.0 - epsilon)) {
//handle gymbal lock
if (pitch > 0) {
pitch = Math.PI / 2.0;
} else {
pitch = -Math.PI / 2.0;
}
yaw = 0.0;
} else {
if (z < 0) {
if(x > 0 && x < 1) {
yaw = Math.PI - Math.asin(x / c);
} else {
yaw = -Math.asin(x / c) - Math.PI;
}
} else {
yaw = Math.asin(x / c);
}
}
return {
x: pitch * RADIANS_TO_DEGREES,
y: yaw * RADIANS_TO_DEGREES,
z: 0.0 //discard roll component
};
};
polarToVec = function(orientation) {
var pitch = orientation.x * DEGREES_TO_RADIANS;
var yaw = orientation.y * DEGREES_TO_RADIANS;
return {
x: Math.cos(pitch) * Math.sin(yaw),
y: Math.sin(-pitch),
z: Math.cos(pitch) * Math.cos(yaw)
};
}
selectionManager.addEventListener(function() {
data = {
type: 'update',
@ -1267,7 +1224,7 @@ PropertiesTool = function(opts) {
entity.properties.rotation = Quat.safeEulerAngles(entity.properties.rotation);
}
if (entity.properties.keyLightDirection !== undefined) {
entity.properties.keyLightDirection = vecToPolar(entity.properties.keyLightDirection);
entity.properties.keyLightDirection = Vec3.toPolar(entity.properties.keyLightDirection);
}
selections.push(entity);
}
@ -1297,7 +1254,7 @@ PropertiesTool = function(opts) {
data.properties.rotation = Quat.fromPitchYawRollDegrees(rotation.x, rotation.y, rotation.z);
}
if (data.properties.keyLightDirection !== undefined) {
data.properties.keyLightDirection = polarToVec(data.properties.keyLightDirection);
data.properties.keyLightDirection = Vec3.fromPolar(data.properties.keyLightDirection.x, data.properties.keyLightDirection.y);
}
Entities.editEntity(selectionManager.selections[0], data.properties);
if (data.properties.name != undefined) {

View file

@ -14,6 +14,7 @@
#include <QDebug>
#include "ScriptEngineLogging.h"
#include "NumericalConstants.h"
#include "Vec3.h"
glm::vec3 Vec3::reflect(const glm::vec3& v1, const glm::vec3& v2) {
@ -73,3 +74,64 @@ void Vec3::print(const QString& lable, const glm::vec3& v) {
bool Vec3::equal(const glm::vec3& v1, const glm::vec3& v2) {
return v1 == v2;
}
glm::vec3 Vec3::toPolar(const glm::vec3& v) {
glm::vec3 u = normalize(v);
float pitch, yaw, temp;
pitch = glm::asin(-u.y);
temp = glm::cos(pitch);
if (glm::abs(pitch) >= (PI - EPSILON)) {
yaw = 0.0;
if (pitch > 0) {
pitch = PI_OVER_TWO;
} else {
pitch = -PI_OVER_TWO;
}
} else {
if (u.z < 0.0) {
if (u.x > 0.0 && u.x < 1.0) {
yaw = PI - glm::asin(u.x / temp);
} else {
yaw = -PI - glm::asin(u.x / temp);
}
} else {
yaw = glm::asin(u.x / temp);
}
}
// Round small values to 0
if (glm::abs(pitch) < EPSILON) {
pitch = 0.0;
}
if (glm::abs(yaw) < EPSILON) {
yaw = 0.0;
}
// Neglect roll component
return glm::vec3(glm::degrees(pitch), glm::degrees(yaw), 0.0);
}
glm::vec3 Vec3::fromPolar(float pitch, float yaw) {
pitch = glm::radians(pitch);
yaw = glm::radians(yaw);
float x = glm::cos(pitch) * glm::sin(yaw);
float y = glm::sin(-pitch);
float z = glm::cos(pitch) * glm::cos(yaw);
// Round small values to 0
if (glm::abs(x) < EPSILON) {
x = 0.0;
}
if (glm::abs(y) < EPSILON) {
y = 0.0;
}
if (glm::abs(z) < EPSILON) {
z = 0.0;
}
return glm::vec3(x, y, z);
}

View file

@ -40,6 +40,8 @@ public slots:
glm::vec3 mix(const glm::vec3& v1, const glm::vec3& v2, float m);
void print(const QString& lable, const glm::vec3& v);
bool equal(const glm::vec3& v1, const glm::vec3& v2);
glm::vec3 toPolar(const glm::vec3& v);
glm::vec3 fromPolar(float pitch, float yaw);
};