New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add useAnimatedSensor for Web #3955
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
const config = | ||
interval === -1 | ||
? { referenceFrame: 'device' } | ||
: { frequency: 1 / interval }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be{ frequency: 1000 / interval }
, because the interval
contains milliseconds.
sensor.addEventListener('reading', () => { | ||
if (sensorType === SensorType.ROTATION) { | ||
const [qw, qx, qy, qz] = sensor.quaternion; | ||
|
||
// reference: https://stackoverflow.com/questions/5782658/extracting-yaw-from-a-quaternion | ||
const yaw = Math.atan2( | ||
2.0 * (qy * qz + qw * qx), | ||
qw * qw - qx * qx - qy * qy + qz * qz | ||
); | ||
const pitch = Math.sin(-2.0 * (qx * qz - qw * qy)); | ||
const roll = Math.atan2( | ||
2.0 * (qx * qy + qw * qz), | ||
qw * qw + qx * qx - qy * qy - qz * qz | ||
); | ||
eventHandler({ qw, qx, qy, qz, yaw, pitch, roll }); | ||
} else { | ||
const { x, y, z } = sensor; | ||
eventHandler({ x, y, z }); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this way we can get rid off unnecessary if
condition inside of callback:
let callback;
if (sensorType === SensorType.ROTATION) {
callback = () => {
const [qw, qx, qy, qz] = sensor.quaternion;
// reference: https://stackoverflow.com/questions/5782658/extracting-yaw-from-a-quaternion
const yaw = Math.atan2(
2.0 * (qy * qz + qw * qx),
qw * qw - qx * qx - qy * qy + qz * qz
);
const pitch = Math.sin(-2.0 * (qx * qz - qw * qy));
const roll = Math.atan2(
2.0 * (qx * qy + qw * qz),
qw * qw + qx * qx - qy * qy - qz * qz
);
eventHandler({ qw, qx, qy, qz, yaw, pitch, roll });
};
} else {
callback = () => {
const { x, y, z } = sensor;
eventHandler({ x, y, z });
};
}
sensor.addEventListener('reading', callback);
const config = | ||
interval === -1 | ||
? { referenceFrame: 'device' } | ||
: { frequency: 1 / interval }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if interval is equal 0
?
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please follow the template so that the reviewers can easily understand what the code changes affect. --> ## Summary <!-- Explain the motivation for this PR. Include "Fixes #<number>" if applicable. --> Applied requested changes to #3955 after late code review. - Removed unnecessary if condition inside callback. - Fixed unit conversion bug ## Test plan - Chrome on the android device.
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please follow the template so that the reviewers can easily understand what the code changes affect. --> ## Summary - Added `useAnimatedSensor` for the web implementation ## Test plan - Used chrome on the android device to check if the sensor returns correct data
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please follow the template so that the reviewers can easily understand what the code changes affect. --> ## Summary <!-- Explain the motivation for this PR. Include "Fixes #<number>" if applicable. --> Applied requested changes to software-mansion#3955 after late code review. - Removed unnecessary if condition inside callback. - Fixed unit conversion bug ## Test plan - Chrome on the android device.
Summary
useAnimatedSensor
for the web implementationTest plan