Skip to content

Commit

Permalink
Implement 3D rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
plweegie committed Jun 21, 2020
1 parent f3aef59 commit 37701cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
28 changes: 18 additions & 10 deletions app/src/main/java/com/plweegie/magmolecular/ar/MagMolFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.ViewGroup
import com.google.ar.sceneform.math.Quaternion
import com.google.ar.sceneform.math.Vector3
import com.google.ar.sceneform.ux.*
import kotlin.math.abs


class MagMolFragment : ArFragment() {
Expand Down Expand Up @@ -50,29 +51,36 @@ class MagMolFragment : ArFragment() {
var endPosition = Vector3.zero()

it.setGestureEventListener(object : BaseGesture.OnGestureEventListener<DragGesture> {
override fun onUpdated(gesture: DragGesture?) {
endPosition = gesture?.position
val angle = getRotationAngle(startPosition, endPosition)
override fun onUpdated(gesture: DragGesture) {
endPosition = gesture.position

gesture?.targetNode?.parent?.localRotation = Quaternion.multiply(
gesture?.targetNode?.parent?.localRotation,
Quaternion.axisAngle(Vector3.up().scaled(4.0f), angle)
gesture.targetNode?.parent?.localRotation = Quaternion.multiply(
gesture.targetNode?.parent?.localRotation,
getRotation(startPosition, endPosition)
)
}

override fun onFinished(gesture: DragGesture?) {}
override fun onFinished(gesture: DragGesture) {}
})
}

transformationSystem.apply {
addGestureRecognizer(dragGestureRecognizer)
//addGestureRecognizer(twistGestureRecognizer)
selectionVisualizer = NullVisualizer()
}
return transformationSystem
}

private fun getRotationAngle(startPosition: Vector3, endPosition: Vector3): Float {
val diff = endPosition.x - startPosition.x
return (10 * diff) / displayMetrics.widthPixels
private fun getRotation(startPosition: Vector3, endPosition: Vector3): Quaternion {
val diffX = (endPosition.x - startPosition.x)
val diffY = (endPosition.y - startPosition.y)

return if (abs(diffX) >= abs(diffY)) {
Quaternion.axisAngle(Vector3.up().scaled(40.0f), diffX / displayMetrics.widthPixels)
} else {
Quaternion.axisAngle(Vector3.right().scaled(40.0f), diffY / displayMetrics.heightPixels)
}
}

class NullVisualizer : SelectionVisualizer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Modifications (C) 2020 Jan K Szymanski
*/
package com.google.ar.sceneform.ux;

Expand All @@ -25,7 +27,7 @@
public class RotationController extends BaseTransformationController<TwistGesture> {

// Rate that the node rotates in degrees per degree of twisting.
private float rotationRateDegrees = 2.5f;
private float rotationRateDegrees = 1.0f;

public RotationController(
BaseTransformableNode transformableNode, TwistGestureRecognizer gestureRecognizer) {
Expand All @@ -42,13 +44,13 @@ public float getRotationRateDegrees() {

@Override
public boolean canStartTransformation(TwistGesture gesture) {
return getTransformableNode().isSelected();
return true;
}

@Override
public void onContinueTransformation(TwistGesture gesture) {
float rotationAmount = -gesture.getDeltaRotationDegrees() * rotationRateDegrees;
Quaternion rotationDelta = new Quaternion(Vector3.up(), rotationAmount);
Quaternion rotationDelta = new Quaternion(Vector3.forward(), rotationAmount);
Quaternion localrotation = getTransformableNode().getLocalRotation();
localrotation = Quaternion.multiply(localrotation, rotationDelta);
getTransformableNode().setLocalRotation(localrotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Modifications (C) 2020 Jan K Szymanski
*/
package com.google.ar.sceneform.ux;

Expand Down Expand Up @@ -87,29 +89,7 @@ public boolean isTransforming() {

@Override
public boolean canStartTransformation(DragGesture gesture) {
Node targetNode = gesture.getTargetNode();
if (targetNode == null) {
return false;
}

BaseTransformableNode transformableNode = getTransformableNode();
if (targetNode != transformableNode && !targetNode.isDescendantOf(transformableNode)) {
return false;
}

if (!transformableNode.isSelected() && !transformableNode.select()) {
return false;
}

Vector3 initialForwardInWorld = transformableNode.getForward();
Node parent = transformableNode.getParent();
if (parent != null) {
initialForwardInLocal.set(parent.worldToLocalDirection(initialForwardInWorld));
} else {
initialForwardInLocal.set(initialForwardInWorld);
}

return true;
return false;
}

@Override
Expand Down

0 comments on commit 37701cf

Please sign in to comment.