Showing the changes to libraries/entities/src/EntityScriptingInterface.cpp (1 of 7 files). Show all
Unified · Split
| @@ -64,6 +64,54 @@ void EntityScriptingInterface::setEntityTree(EntityTreePointer elementTree) { · 63 unmodified lines | ||
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | +EntityItemProperties convertLocationToScriptSemantics(EntityItemProperties entitySideProperties) { | |
| 68 | + // In EntityTree code, properties.position and properties.rotation are relative to the parent. In javascript, | |
| 69 | + // they are in world-space. The local versions are put into localPosition and localRotation and position and | |
| 70 | + // rotation are converted from local to world space. | |
| 71 | + EntityItemProperties scriptSideProperties = entitySideProperties; | |
| 72 | + scriptSideProperties.setLocalPosition(entitySideProperties.getPosition()); | |
| 73 | + scriptSideProperties.setLocalRotation(entitySideProperties.getRotation()); | |
| 74 | + | |
| 75 | + glm::vec3 worldPosition = SpatiallyNestable::localToWorld(entitySideProperties.getPosition(), | |
| 76 | + entitySideProperties.getParentID(), | |
| 77 | + entitySideProperties.getParentJointIndex()); | |
| 78 | + glm::quat worldRotation = SpatiallyNestable::localToWorld(entitySideProperties.getRotation(), | |
| 79 | + entitySideProperties.getParentID(), | |
| 80 | + entitySideProperties.getParentJointIndex()); | |
| 81 | + scriptSideProperties.setPosition(worldPosition); | |
| 82 | + scriptSideProperties.setRotation(worldRotation); | |
| 83 | + | |
| 84 | + return scriptSideProperties; | |
| 85 | +} | |
| 86 | + | |
| 87 | + | |
| 88 | +EntityItemProperties convertLocationFromScriptSemantics(EntityItemProperties scriptSideProperties) { | |
| 89 | + // convert position and rotation properties from world-space to local, unless localPosition and localRotation | |
| 90 | + // are set. If they are set, they overwrite position and rotation. | |
| 91 | + EntityItemProperties entitySideProperties = scriptSideProperties; | |
| 92 | + | |
| 93 | + if (scriptSideProperties.localPositionChanged()) { | |
| 94 | + entitySideProperties.setPosition(scriptSideProperties.getLocalPosition()); | |
| 95 | + } else if (scriptSideProperties.positionChanged()) { | |
| 96 | + glm::vec3 localPosition = SpatiallyNestable::worldToLocal(entitySideProperties.getPosition(), | |
| 97 | + entitySideProperties.getParentID(), | |
| 98 | + entitySideProperties.getParentJointIndex()); | |
| 99 | + entitySideProperties.setPosition(localPosition); | |
| 100 | + } | |
| 101 | + | |
| 102 | + if (scriptSideProperties.localRotationChanged()) { | |
| 103 | + entitySideProperties.setRotation(scriptSideProperties.getLocalRotation()); | |
| 104 | + } else if (scriptSideProperties.rotationChanged()) { | |
| 105 | + glm::quat localRotation = SpatiallyNestable::worldToLocal(entitySideProperties.getRotation(), | |
| 106 | + entitySideProperties.getParentID(), | |
| 107 | + entitySideProperties.getParentJointIndex()); | |
| 108 | + entitySideProperties.setRotation(localRotation); | |
| 109 | + } | |
| 110 | + | |
| 111 | + return entitySideProperties; | |
| 112 | +} | |
| 113 | + | |
| 114 | + | |
| 67 | 115 | QUuid EntityScriptingInterface::addEntity(const EntityItemProperties& properties) { |
| 68 | 116 | EntityItemProperties propertiesWithSimID = properties; |
| 69 | 117 | propertiesWithSimID.setDimensionsInitialized(properties.dimensionsChanged()); |