Cheetah Software  1.0
CollisionMesh.cpp
Go to the documentation of this file.
8 template <typename T>
9 bool CollisionMesh<T>::ContactDetection(const Vec3<T>& cp_pos, T& penetration,
10  Mat3<T>& cp_frame) {
11  // Contact detection
12  Vec3<T> cp_pos_in_height_map = cp_pos - _left_corner_loc;
13  if ((0 < cp_pos_in_height_map[0]) && (cp_pos_in_height_map[0] < _x_max)) {
14  if ((0 < cp_pos_in_height_map[1]) && (cp_pos_in_height_map[1] < _y_max)) {
15  int x_idx = floor(cp_pos_in_height_map[0] / _grid);
16  int y_idx = floor(cp_pos_in_height_map[1] / _grid);
17 
18  Vec3<T> vec1;
19  vec1[0] = _grid;
20  vec1[1] = _grid;
21  vec1[2] = _height_map(x_idx + 1, y_idx + 1) - _height_map(x_idx, y_idx);
22 
23  Vec3<T> vec2;
24  vec2[0] = _grid;
25  vec2[1] = -_grid;
26  vec2[2] = _height_map(x_idx + 1, y_idx) - _height_map(x_idx, y_idx + 1);
27 
28  vec1.normalize();
29  vec2.normalize();
30 
31  Vec3<T> normal = vec2.cross(vec1);
32  // vec2 is x axis
33  Vec3<T> y_axis = normal.cross(vec2);
34 
35  cp_frame.template block<3, 1>(0, 0) = vec2;
36  cp_frame.template block<3, 1>(0, 1) = y_axis;
37  cp_frame.template block<3, 1>(0, 2) = normal;
38 
39  // Vector pointing middle of four points
40  T height_ave = 0.25 * _height_map(x_idx, y_idx) +
41  0.25 * _height_map(x_idx, y_idx + 1) +
42  0.25 * _height_map(x_idx + 1, y_idx) +
43  0.25 * _height_map(x_idx + 1, y_idx + 1);
44 
45  Vec3<T> middle_pt;
46  middle_pt[0] = _grid * x_idx + 0.5 * _grid;
47  middle_pt[1] = _grid * y_idx + 0.5 * _grid;
48  middle_pt[2] = height_ave;
49 
50  Vec3<T> local_diff =
51  cp_frame.transpose() * (cp_pos_in_height_map - middle_pt);
52 
53  if (local_diff[2] < 0.) {
54  penetration = local_diff[2];
55  return true;
56  }
57  }
58  }
59 
60  return false;
61 }
62 
63 template class CollisionMesh<double>;
64 template class CollisionMesh<float>;
virtual bool ContactDetection(const Vec3< T > &cp_pos, T &penetration, Mat3< T > &cp_frame)
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
Collision logic for a mesh.