VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
void AVR.Phys.AVR_Grabbable.UpdateRBVelocity ( )
inlineprivate

Definition at line 167 of file AVR_Grabbable.cs.

168  {
169  #if AVR_NET
170  if(IsOnline && !IsOwner) return;
171  #endif
172 
173  // Get pos-rot of hand and item
174  Vector3 targetItemPosition = transform.position;
175  Quaternion targetItemRotation = transform.rotation;
176  Vector3 targetHandPosition = this.getTargetPosition();
177  Quaternion targetHandRotation = this.getTargetRotation();
178 
179  // Set the current objecttype to the regular object type or the nested one
180  _objType = (isGrabbedByMultipleHands && objectType.changeObjectTypeOnTwoHanded) ? objectType.typeOnTwoHanded : objectType;
181 
182  // Break joint if the distance is too far & it is growing.
183  if (Vector3.Distance(targetItemPosition, targetHandPosition) > last_dist && last_dist > _objType.Break_grab_distance)
184  {
185  while (AttachedHands.Count > 0) AttachedHands[0].makeRelease();
186  return;
187  }
188  last_dist = Vector3.Distance(targetItemPosition, targetHandPosition);
189 
190  // Follow aglorithms
191  switch(_objType.followType) {
192  case GrabbableObjectType.FollowType.FREE : {
193  //pos
194  Vector3 pDelta = (targetHandPosition - targetItemPosition);
195  Vector3 vel = pDelta / Time.fixedDeltaTime;
196 
197  rb.velocity = vel * _objType.Lightness;
198  lastVel = rb.velocity;
199 
200  //ang
201  Quaternion rotationDelta = targetHandRotation * Quaternion.Inverse(targetItemRotation);
202 
203  rotationDelta.ToAngleAxis(out float angle, out Vector3 axis);
204  while (angle > 180) angle -= 360; // Prevent object from doing sudden >180° turns instead of negative <180° ones
205 
206  rb.maxAngularVelocity = 99999.0f;
207 
208  Vector3 angvel = (angle * axis * Mathf.Deg2Rad) / Time.fixedDeltaTime;
209  if (!float.IsNaN(angvel.z)) rb.angularVelocity = angvel * _objType.Angular_Lightness;
210  break;
211  }
212  case GrabbableObjectType.FollowType.STATIC : {
213  break;
214  }
215  case GrabbableObjectType.FollowType.CONSTRAINED : {
216  Vector3 pDelta = (targetHandPosition - targetItemPosition);
217  Vector3 vel = pDelta / Time.fixedDeltaTime;
218 
219  // The current world acceleration is rb.velocity - lastVel. However, due to sudden changes in the objects position, we prevent the acceleration from jumping to
220  // dramatically by smoothing out the world acceleration throuhg a simple linear interpolation
221  // Lower the value of 0.5 to make the world acceleration more delayed/elastic, but also lower the jitter an object may experience
222  wacc = Vector3.Lerp(wacc, rb.velocity - lastVel, 0.5f);
223 
224  worldvel = wacc / Time.fixedDeltaTime; //We scale wacc similarly to pDelta, to compare with vel
225 
226  worldvel -= worldvel.normalized * Mathf.Min(worldvel.magnitude, 10); //10 == minimum force applied by hand
227  vel = Vector3.ClampMagnitude(vel, 30); //30 == maximum force applied by hand
228 
229  if (!float.IsNaN(worldvel.x) && !float.IsNaN(worldvel.y) && !float.IsNaN(worldvel.z))
230  {
231  rb.velocity = vel + worldvel;
232  }
233  else
234  {
235  rb.velocity = Vector3.zero;
236  }
237 
238  lastVel = rb.velocity;
239 
240 
241  //ang
242  Quaternion rotationDelta = targetHandRotation * Quaternion.Inverse(targetItemRotation);
243 
244  rotationDelta.ToAngleAxis(out float angle, out Vector3 axis);
245  while (angle > 180) angle -= 360; // Prevent object from doing sudden >180° turns instead of negative <180° ones
246 
247  rb.maxAngularVelocity = 99999.0f;
248 
249  Vector3 angvel = (angle * axis * Mathf.Deg2Rad) / Time.fixedDeltaTime;
250  if (!float.IsNaN(angvel.z)) rb.angularVelocity = (angvel * objectType.Angular_Lightness);
251  break;
252  }
253  case GrabbableObjectType.FollowType.HEAVY : {
254  // Point where object was grabbed
255  Vector3 closestp = AttachedHands[0].getWorldGrabLocation();
256 
257  // Difference between point where object was grabbed at and current hand/palm position (normalized over a few frames)
258  force = Vector3.Lerp(force, AttachedHands[0].grabPoint.position - closestp, 0.25f);
259 
260  // Velocity of rigidbody at grabbed position (normalized)
261  cvel = Vector3.Lerp(cvel, rb.GetPointVelocity(closestp), 0.25f);
262 
263  float k = 0.3f;
264  // delta = the force we want to apply minus the objects current velocity
265  Vector3 delta = force - Vector3.Lerp(Vector3.zero, cvel, k * Vector3.Distance(AttachedHands[0].grabPoint.position, closestp));
266  Vector3 f = delta * Time.fixedDeltaTime * 100.0f * _objType.Heavy_force_multiplier;
267 
268  rb.velocity *= 0.8f;
269  rb.AddForceAtPosition(f, closestp, ForceMode.Impulse);
270  break;
271  }
272  }
273  velocities.Add(rb.velocity);
274  while (velocities.Count > velocity_count) velocities.RemoveAt(0);
275  }
List< AVR_BasicGrabProvider > AttachedHands
List of hands that are currently grabbing this object.
GrabbableObjectType objectType
Type that describes the objects behaviour when grabbed.
bool isGrabbedByMultipleHands
True if the object is being grabbed by 2 or more hands, otherwise false.
GrabbableObjectType _objType
Rigidbody rb
Rigidbody of this grabbable object.
List< Vector3 > velocities
GrabbableObjectType typeOnTwoHanded