0

I'm trying to simulate unscrewing a nut from a bolt. To me, this means tracking the y-rotation and moving the nut on the y-axis depending on the direction the nut is rotating. The problem is that euler angles loop between 0 and 360 and quaternions between -1 and 1. This causes my nut to only be able to move up and down within a single rotation.

I've considered trying to catch the jumps (ie detecting rotation.y jumped from 359 to 1, rotating clockwise) and compensate for them, but there were a lot of edge cases this wasn't good. What's the best way for me to go about this? Any help appreciated, thanks.

turboslut
  • 1
  • 1
  • I tend to find that math for continuous rotations/simulations are easiest to code from Time 0 to Time N, in which case you don't tend to hit axis locks with your angles. So basically over time, you could base your offset along the shaft as frame-offset-delta multipled by T and rotation as frame-angle-delta multiplied by T. Your scenario might be more complicated, but this I think is the simplest way to model that animation. – Mark Rabjohn Oct 09 '20 at 14:05
  • Thank you for the input. I think what you're saying is to make an animation to handle rotation and movement together? Only problem is that the user will be manually rotating the nut as an input and the nut will have to move based on that user input. – turboslut Oct 09 '20 at 14:15
  • What i suggest in this case is to use the torque values intead of rotation values, add torque on the axis that you want the rotation to follow. – Skin_phil Oct 09 '20 at 14:23
  • Thanks @Skin_phil, but not sure if that's doable in my use case, or at least I haven't figured it out. Specifically, users will be turning the nut with hand tracking in VR and I don't know how to derive torque values in this system if there are any to begin with. – turboslut Oct 09 '20 at 14:28
  • Well i dont know the full context but you could say that if the hand touches it you add a set torque, i am suggesting that because i know the pain of working with rotation and euler angles – Skin_phil Oct 09 '20 at 14:30

1 Answers1

0

I knew it must be more complex. In the past I always used to handle translation and rotation seperately - but not using Unity I'm afraid. All I can say is because this is a nut + bolt, the rotation of the nut is 1:1 linked to the offset along the bolts shaft. You need to standardize your math on the amount of offset. This means that the input rotation can be calculated back to shaft offset (ratio of twist to offset) and then worked forward to get final rotation of the nut (you can't go whacky doing this because you're working out angle of rotation afresh each time).

I moved this from a comment, because I didn't think that I was clear. Basically it doesn't matter if your rotation is at 20 degrees, and you want to apply -50 degrees. You would basically apply 50 degrees worth of offset either up or down the shaft and then work forward to get the angle of the nut - so you'd still end up with a 330 degree rotation, but that rotation wouldn't act to clip the position on the shaft.

Following this model, your shaft could be 40mm long or 1m long and you'd be able to wind the nut all the way along - you'd just need the stopping condition at the head, and the event to say that it's undone at the end.

Mark Rabjohn
  • 1,643
  • 14
  • 30