PhysicsHelpers.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: PhysicsHelpers.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Physics helpers for CubbyFlow.
6 > Created Time: 2017/06/03
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_PHYSICS_HELPERS_H
10 #define CUBBYFLOW_PHYSICS_HELPERS_H
11 
12 #include <Core/Vector/Vector2.h>
13 #include <Core/Vector/Vector3.h>
14 
15 namespace CubbyFlow
16 {
17  inline Vector2D ComputeDragForce(double dragCoefficient, double radius, const Vector2D& velocity)
18  {
19  // Stoke's drag force assuming our Reynolds number is very low.
20  // http://en.wikipedia.org/wiki/Drag_(physics)#Very_low_Reynolds_numbers:_Stokes.27_drag
21  return -6.0 * PI_DOUBLE * dragCoefficient * radius * velocity;
22  }
23 
24  inline Vector3D ComputeDragForce(double dragCoefficient, double radius, const Vector3D& velocity)
25  {
26  // Stoke's drag force assuming our Reynolds number is very low.
27  // http://en.wikipedia.org/wiki/Drag_(physics)#Very_low_Reynolds_numbers:_Stokes.27_drag
28  return -6.0 * PI_DOUBLE * dragCoefficient * radius * velocity;
29  }
30 
31  template <size_t N>
32  inline Vector<double, N> ProjectAndApplyFriction(const Vector<double, N>& vel, const Vector<double, N>& normal, double frictionCoefficient)
33  {
34  Vector<double, N> velt = vel.Projected(normal);
35  if (velt.LengthSquared() > 0)
36  {
37  double veln = std::max(-vel.Dot(normal), 0.0);
38  velt *= std::max(1.0 - frictionCoefficient * veln / velt.Length(), 0.0);
39  }
40 
41  return velt;
42  }
43 
44  inline double ComputePressureFromEos(
45  double density, double targetDensity,
46  double eosScale, double eosExponent,
47  double negativePressureScale)
48  {
49  // Equation of state
50  // (http://www.ifi.uzh.ch/vmml/publications/pcisph/pcisph.pdf)
51  double p = eosScale / eosExponent * (std::pow((density / targetDensity), eosExponent) - 1.0);
52 
53  // Negative pressure scaling
54  if (p < 0)
55  {
56  p *= negativePressureScale;
57  }
58 
59  return p;
60  }
61 }
62 
63 #endif
3-D vector class.
Definition: Vector3.h:26
Vector2D ComputeDragForce(double dragCoefficient, double radius, const Vector2D &velocity)
Definition: PhysicsHelpers.h:17
constexpr double PI_DOUBLE
Double-type PI.
Definition: Constants.h:75
double ComputePressureFromEos(double density, double targetDensity, double eosScale, double eosExponent, double negativePressureScale)
Definition: PhysicsHelpers.h:44
Generic statically-sized N-D vector class.
Definition: Vector.h:33
Vector< double, N > ProjectAndApplyFriction(const Vector< double, N > &vel, const Vector< double, N > &normal, double frictionCoefficient)
Definition: PhysicsHelpers.h:32
T LengthSquared() const
Returns the squared length of the vector.
Definition: Vector-Impl.h:282
Definition: pybind11Utils.h:24
T Dot(const E &v) const
Computes dot product.
Definition: Vector-Impl.h:412
2-D vector class.
Definition: Vector2.h:26
T Length() const
Returns the length of the vector.
Definition: Vector-Impl.h:276