VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
void AVR.Avatar.AVR_PoseProvider.SetBody ( )
inlineprivate

Definition at line 288 of file AVR_PoseProvider.cs.

288  {
289  // Pre-processing
290  {
291  // Calculation of lean_conf, which indicates our confidence of whether the palyer is leaning forwards or standing upright.
293  {
294  lean_factor = Mathf.Lerp(lean_factor, 1.0f, Time.deltaTime * lean_blend_speed);
295  }
296  else
297  {
298  lean_factor = Mathf.Lerp(lean_factor, 0.0f, Time.deltaTime * lean_blend_speed);
299  }
300  lean_conf = lean_factor * playerRig.isLeaningForwardsConfidence();
301 
302 
303  // We apply inertia to the main cameras position, to avoid unnecessary micro-movement. Note: We do not apply this to the lookat-target, so the head rotation will be fully responsive.
304  eyeTransform.position = ApplyInertia(eyeTransform.position, playerRig.MainCamera.transform.position);
305 
306  // Here we apply clamping bounds on the pitch and roll angles of the head. We deal with yaw in a separate function.
307  //NOTE / TODO: This could lead to problems, as we use localRotation of the camera here. If, say, the camera was in a child object of the GenericXRDevice object, the local rotation would be zero.
308  Quaternion r = playerRig.MainCamera.transform.localRotation;
309  r = AVR.Core.Utils.Geom.ClampQuaternionRotation(r, new Vector3(max_head_pitch, 360, max_head_roll));
310  eyeTransform.localRotation = r;
311  }
312 
313 
314 
315  //No we get to the actual body transform. First we reset its rotation:
316  bodyTransform.up = Vector3.up;
317 
318  // Get the rotation and position if the player is standing upright:
319  GetStandingTransform(out Quaternion stout, out Vector3 stpos);
320  // Get the rotation and position if the player is leaning forwards:
321  GetLeanTransform(out Quaternion bdout, out Vector3 bdpos);
322 
323  // We designate an "unsafe position/rotation", which corresponds to our leaning transform, depending on our leaning-confidence value.
324  Vector3 unsafe_pos = Vector3.Lerp(stpos, bdpos, lean_conf);
325  Quaternion unsafe_rot = Quaternion.Lerp(stout, bdout, lean_conf);
326 
327  // The unsafe position corresponds to the position/rotation we believe the body has *based on our lean_conf value*.
328  // PROBLEM: we may end up with the players feet hovering in the air. Here we correct for this.
329  // We interpolate between our "unsafe" (the higher) position and the regular, standing-upright (lower) position, based on the distance of the body to the ground (pivot)
330  {
331  float sh = stpos.y - pivotTransform.position.y; // safe height
332  float uh = unsafe_pos.y - pivotTransform.position.y;// unsafe height
333 
334  float lamb = Mathf.Clamp((default_body_height - sh) / (uh - sh), 0.0f, 1.0f);
335 
336  unsafe_pos = Vector3.Lerp(stpos, unsafe_pos, lamb);
337  unsafe_rot = Quaternion.Lerp(stout, unsafe_rot, lamb);
338  }
339 
340  // Apply pos and rot to transform
341  bodyTransform.position = unsafe_pos;
342  bodyTransform.rotation = unsafe_rot;
343 
344  // Update pivot based on new body position
345  UpdatePivot();
346 
347  // Clamp the distance top the ground to be sure
348  bodyTransform.position = new Vector3(
349  bodyTransform.position.x,
350  pivotTransform.position.y + Mathf.Clamp(bodyTransform.position.y - pivotTransform.position.y, min_body_height, max_body_height),
351  bodyTransform.position.z
352  );
353  }
Camera MainCamera
Camera that represents the HMD
float min_body_height
Minimum height of the body/torso fromt he ground
float default_body_height
Default height of the body/torso
float max_body_height
Maximum height of the body/torso fromt he ground
float max_head_roll
Maximum roll angle of the head in relation of the body
void GetLeanTransform(out Quaternion rot, out Vector3 pos)
float isLeaningForwardsConfidence()
Returns a confidence value of how much the player is leaning forwards / bowing.
float max_head_pitch
Maximum pitch angle of the head in relation of the body
Vector3 ApplyInertia(Vector3 current, Vector3 target)
void GetStandingTransform(out Quaternion rot, out Vector3 pos)
AVR_PlayerRig playerRig
The current AVR_PlayerRig instance in the scene
float lean_blend_speed
Blending speed between a leaning and standing position