Bullet Collision Detection & Physics Library: btDeformableMousePickingForce.h Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btDeformableMousePickingForce.h
Go to the documentation of this file.
1/*
2 Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
3
4 Bullet Continuous Collision Detection and Physics Library
5 Copyright (c) 2019 Google Inc. http://bulletphysics.org
6 This software is provided 'as-is', without any express or implied warranty.
7 In no event will the authors be held liable for any damages arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it freely,
10 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16#ifndef BT_MOUSE_PICKING_FORCE_H
17#define BT_MOUSE_PICKING_FORCE_H
18
19#include "btDeformableLagrangianForce.h"
20
21 class btDeformableMousePickingForce : public btDeformableLagrangianForce
22{
23 // If true, the damping force will be in the direction of the spring
24 // If false, the damping force will be in the direction of the velocity
25 btScalar m_elasticStiffness, m_dampingStiffness;
26 const btSoftBody::Face& m_face;
27 btVector3 m_mouse_pos;
28 btScalar m_maxForce;
29
30public:
31 typedef btAlignedObjectArray<btVector3> TVStack;
32 btDeformableMousePickingForce(btScalar k, btScalar d, const btSoftBody::Face& face, const btVector3& mouse_pos, btScalar maxForce = 0.3) : m_elasticStiffness(k), m_dampingStiffness(d), m_face(face), m_mouse_pos(mouse_pos), m_maxForce(maxForce)
33 {
34 }
35
36 virtual void addScaledForces(btScalar scale, TVStack& force)
37 {
38 addScaledDampingForce(scale, force);
39 addScaledElasticForce(scale, force);
40 }
41
42 virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
43 {
44 addScaledElasticForce(scale, force);
45 }
46
47 virtual void addScaledDampingForce(btScalar scale, TVStack& force)
48 {
49 for (int i = 0; i < 3; ++i)
50 {
51 btVector3 v_diff = m_face.m_n[i]->m_v;
52 btVector3 scaled_force = scale * m_dampingStiffness * v_diff;
53 if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
54 {
55 btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
56 scaled_force = scale * m_dampingStiffness * v_diff.dot(dir) * dir;
57 }
58 force[m_face.m_n[i]->index] -= scaled_force;
59 }
60 }
61
62 virtual void addScaledElasticForce(btScalar scale, TVStack& force)
63 {
64 btScalar scaled_stiffness = scale * m_elasticStiffness;
65 for (int i = 0; i < 3; ++i)
66 {
67 btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
68 btVector3 scaled_force = scaled_stiffness * dir;
69 if (scaled_force.safeNorm() > m_maxForce)
70 {
71 scaled_force.safeNormalize();
72 scaled_force *= m_maxForce;
73 }
74 force[m_face.m_n[i]->index] -= scaled_force;
75 }
76 }
77
78 virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
79 {
80 btScalar scaled_k_damp = m_dampingStiffness * scale;
81 for (int i = 0; i < 3; ++i)
82 {
83 btVector3 local_scaled_df = scaled_k_damp * dv[m_face.m_n[i]->index];
84 if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
85 {
86 btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
87 local_scaled_df = scaled_k_damp * dv[m_face.m_n[i]->index].dot(dir) * dir;
88 }
89 df[m_face.m_n[i]->index] -= local_scaled_df;
90 }
91 }
92
93 virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack& diagA) {}
94
95 virtual double totalElasticEnergy(btScalar dt)
96 {
97 double energy = 0;
98 for (int i = 0; i < 3; ++i)
99 {
100 btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
101 btVector3 scaled_force = m_elasticStiffness * dir;
102 if (scaled_force.safeNorm() > m_maxForce)
103 {
104 scaled_force.safeNormalize();
105 scaled_force *= m_maxForce;
106 }
107 energy += 0.5 * scaled_force.dot(dir);
108 }
109 return energy;
110 }
111
112 virtual double totalDampingEnergy(btScalar dt)
113 {
114 double energy = 0;
115 for (int i = 0; i < 3; ++i)
116 {
117 btVector3 v_diff = m_face.m_n[i]->m_v;
118 btVector3 scaled_force = m_dampingStiffness * v_diff;
119 if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
120 {
121 btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
122 scaled_force = m_dampingStiffness * v_diff.dot(dir) * dir;
123 }
124 energy -= scaled_force.dot(m_face.m_n[i]->m_v) / dt;
125 }
126 return energy;
127 }
128
129 virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
130 {
131 btScalar scaled_stiffness = scale * m_elasticStiffness;
132 for (int i = 0; i < 3; ++i)
133 {
134 btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
135 btScalar dir_norm = dir.norm();
136 btVector3 dir_normalized = (dir_norm > SIMD_EPSILON) ? dir.normalized() : btVector3(0, 0, 0);
137 int id = m_face.m_n[i]->index;
138 btVector3 dx_diff = dx[id];
139 btScalar r = 0; // rest length is 0 for picking spring
140 btVector3 scaled_df = btVector3(0, 0, 0);
141 if (dir_norm > SIMD_EPSILON)
142 {
143 scaled_df -= scaled_stiffness * dir_normalized.dot(dx_diff) * dir_normalized;
144 scaled_df += scaled_stiffness * dir_normalized.dot(dx_diff) * ((dir_norm - r) / dir_norm) * dir_normalized;
145 scaled_df -= scaled_stiffness * ((dir_norm - r) / dir_norm) * dx_diff;
146 }
147 df[id] += scaled_df;
148 }
149 }
150
151 void setMousePos(const btVector3& p)
152 {
153 m_mouse_pos = p;
154 }
155
156 virtual btDeformableLagrangianForceType getForceType()
157 {
158 return BT_MOUSE_PICKING_FORCE;
159 }
160};
161
162#endif /* btMassSpring_h */
btDeformableLagrangianForce.h
btDeformableLagrangianForceType
btDeformableLagrangianForceType
Definition btDeformableLagrangianForce.h:24
BT_MOUSE_PICKING_FORCE
@ BT_MOUSE_PICKING_FORCE
Definition btDeformableLagrangianForce.h:30
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
SIMD_EPSILON
#define SIMD_EPSILON
Definition btScalar.h:543
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition btAlignedObjectArray.h:46
btDeformableLagrangianForce
Definition btDeformableLagrangianForce.h:39
btDeformableMousePickingForce
Definition btDeformableMousePickingForce.h:22
btDeformableMousePickingForce::addScaledDampingForceDifferential
virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack &dv, TVStack &df)
Definition btDeformableMousePickingForce.h:78
btDeformableMousePickingForce::m_maxForce
btScalar m_maxForce
Definition btDeformableMousePickingForce.h:28
btDeformableMousePickingForce::addScaledDampingForce
virtual void addScaledDampingForce(btScalar scale, TVStack &force)
Definition btDeformableMousePickingForce.h:47
btDeformableMousePickingForce::totalElasticEnergy
virtual double totalElasticEnergy(btScalar dt)
Definition btDeformableMousePickingForce.h:95
btDeformableMousePickingForce::TVStack
btAlignedObjectArray< btVector3 > TVStack
Definition btDeformableMousePickingForce.h:31
btDeformableMousePickingForce::m_dampingStiffness
btScalar m_dampingStiffness
Definition btDeformableMousePickingForce.h:25
btDeformableMousePickingForce::getForceType
virtual btDeformableLagrangianForceType getForceType()
Definition btDeformableMousePickingForce.h:156
btDeformableMousePickingForce::m_elasticStiffness
btScalar m_elasticStiffness
Definition btDeformableMousePickingForce.h:25
btDeformableMousePickingForce::buildDampingForceDifferentialDiagonal
virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack &diagA)
Definition btDeformableMousePickingForce.h:93
btDeformableMousePickingForce::m_mouse_pos
btVector3 m_mouse_pos
Definition btDeformableMousePickingForce.h:27
btDeformableMousePickingForce::totalDampingEnergy
virtual double totalDampingEnergy(btScalar dt)
Definition btDeformableMousePickingForce.h:112
btDeformableMousePickingForce::m_face
const btSoftBody::Face & m_face
Definition btDeformableMousePickingForce.h:26
btDeformableMousePickingForce::addScaledExplicitForce
virtual void addScaledExplicitForce(btScalar scale, TVStack &force)
Definition btDeformableMousePickingForce.h:42
btDeformableMousePickingForce::btDeformableMousePickingForce
btDeformableMousePickingForce(btScalar k, btScalar d, const btSoftBody::Face &face, const btVector3 &mouse_pos, btScalar maxForce=0.3)
Definition btDeformableMousePickingForce.h:32
btDeformableMousePickingForce::addScaledForces
virtual void addScaledForces(btScalar scale, TVStack &force)
Definition btDeformableMousePickingForce.h:36
btDeformableMousePickingForce::addScaledElasticForce
virtual void addScaledElasticForce(btScalar scale, TVStack &force)
Definition btDeformableMousePickingForce.h:62
btDeformableMousePickingForce::setMousePos
void setMousePos(const btVector3 &p)
Definition btDeformableMousePickingForce.h:151
btDeformableMousePickingForce::addScaledElasticForceDifferential
virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack &dx, TVStack &df)
Definition btDeformableMousePickingForce.h:129
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:82
btVector3::safeNorm
btScalar safeNorm() const
Return the norm (length) of the vector.
Definition btVector3.h:269
btVector3::safeNormalize
btVector3 & safeNormalize()
Definition btVector3.h:286
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition btVector3.h:229
btVector3::norm
btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
btVector3::normalized
btVector3 normalized() const
Return a normalized version of this vector.
Definition btVector3.h:949
btSoftBody::Face
Definition btSoftBody.h:306
btSoftBody::Face::m_n
Node * m_n[3]
Definition btSoftBody.h:307
btSoftBody::Node::m_x
btVector3 m_x
Definition btSoftBody.h:269
btSoftBody::Node::m_v
btVector3 m_v
Definition btSoftBody.h:271
btSoftBody::Node::m_q
btVector3 m_q
Definition btSoftBody.h:270
btSoftBody::Node::index
int index
Definition btSoftBody.h:280