History

rework worldToLocal, start on allowing scripts entity position/rotation to always be in world space

Seth Alves committed 10 years ago

Commit 4add3e21 parent 9548b0e7 files · +95 −15Browse files

Showing the changes to libraries/entities/src/EntityScriptingInterface.cpp (1 of 7 files). Show all

Unified · Split

libraries/entities/src/EntityScriptingInterface.cpp+48−0
@@ -64,6 +64,54 @@ void EntityScriptingInterface::setEntityTree(EntityTreePointer elementTree) { · 63 unmodified lines
6464 }
6565 }
6666
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+
67115 QUuid EntityScriptingInterface::addEntity(const EntityItemProperties& properties) {
68116 EntityItemProperties propertiesWithSimID = properties;
69117 propertiesWithSimID.setDimensionsInitialized(properties.dimensionsChanged());