-
Notifications
You must be signed in to change notification settings - Fork 1
/
force.cpp
44 lines (30 loc) · 1.43 KB
/
force.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define GLM_FORCE_RADIANS
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/norm.hpp>
#include "force.hpp"
Force::Force(ForceType type) : force_type(type) {
}
glm::vec3 Force::get_strength(const glm::vec3 & position) const {
return get_strength_sub_class(position);
}
ConstantForce::ConstantForce(ForceType type, glm::vec3 force_vector) : Force(type),force_vector(force_vector) {
}
glm::vec3 ConstantForce::get_strength_sub_class(const glm::vec3 & position) const {
return force_vector;
}
PointForce::PointForce(ForceType type,glm::vec3 force_position,GLfloat field_strength,GLfloat max_strength) : Force(type), force_position(force_position), field_strength(field_strength),max_strength(max_strength) {
}
glm::vec3 PointForce::get_strength_sub_class(const glm::vec3 & position) const {
return glm::normalize(force_position-position)*field_strength/glm::distance2(position,force_position);
}
CenterOfMassForce::CenterOfMassForce(ForceType type, const Movable * object,GLfloat field_strength) : Force(type), object(object),field_strength(field_strength) {
}
glm::vec3 CenterOfMassForce::get_strength_sub_class(const glm::vec3 & position) const {
glm::vec3 com = object->get_center_of_mass() + glm::vec3(object->get_position());
GLfloat distance2 = glm::distance2(position,com);
if(distance2 > 0.00001) {
return glm::normalize(com-position)*field_strength/glm::distance2(position,com);
} else {
return glm::vec3(0,0,0);
}
}