hx3d  1
2D/3D Simple Game Framework
collider.cpp
1 /*
2  2D Collider.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/physics/2d/collider.hpp"
22 
23 #include "hx3d/math/vector_utils.hpp"
24 
25 namespace hx3d {
26 namespace physics2d {
27 
28 Collider::Definition::Definition():
29  unitCoef(1.f/30.f),
30  material(Material()),
31  mask(0),
32  category(0) {}
33 
34 Collider::Collider(const Shape shapeType, const Type colliderType):
35  position(0, 0),
36  velocity(0, 0),
37  force(0, 0),
38  angularVelocity(0),
39  torque(0),
40  orientation(0),
41  fixedRotation(false),
42  mask(0),
43  category(0),
44  type(colliderType),
45  shape(shapeType)
46  {}
47 
48 Collider::~Collider() {}
49 
51  position = def.unitCoef * def.position;
52  material = def.material;
53  mask = def.mask;
54  category = def.category;
55 }
56 
57 void Collider::applyForce(const glm::vec2& amount) {
58  force += amount;
59 }
60 
61 void Collider::applyImpulse(const glm::vec2& amount, const glm::vec2& contact) {
62  velocity += massData.invMass * amount;
63  angularVelocity += massData.invInertia * math::cross(contact, amount);
64 }
65 
66 void Collider::setDensity(float density) {
67  if (type == Type::Static || type == Type::Kinematic) {
68  massData.setMass(0);
70  } else {
71  computeMass(density);
72  }
73 }
74 
75 } /* physics2d */
76 } /* hx3d */
glm::vec2 position
Zone position.
Static: no forces or velocity.
Mass massData
Mass data.
Definition: collider.hpp:175
void applyForce(const glm::vec2 &amount)
Apply a force.
Definition: collider.cpp:57
void useDefinition(const Definition &def)
Apply a definition on the collider.
Definition: collider.cpp:50
unsigned int category
Category.
Definition: collider.hpp:83
void setDensity(float density)
Set collider density.
Definition: collider.cpp:66
glm::vec2 position
Position.
Definition: collider.hpp:141
unsigned int category
Category.
Definition: collider.hpp:165
Kinematic: no forces but velocity.
float invInertia
Current inverted inertia.
Definition: mass.hpp:61
Type
Collider type.
Definition: collider.hpp:46
hx3d framework namespace
Definition: audio.hpp:26
Collider(const Shape shapeType, const Type colliderType=Type::Dynamic)
Create a collider.
Definition: collider.cpp:34
float unitCoef
Physical ratio.
Definition: collider.hpp:72
float angularVelocity
Angular velocity.
Definition: collider.hpp:153
glm::vec2 velocity
Velocity.
Definition: collider.hpp:143
virtual void computeMass(float density)=0
Compute the collider mass.
Shape
Collider shape.
Definition: collider.hpp:58
void setInertia(float amount)
Set the current inertia.
Definition: mass.cpp:19
void applyImpulse(const glm::vec2 &amount, const glm::vec2 &contact)
Apply an impulse.
Definition: collider.cpp:61
unsigned int mask
Mask.
Definition: collider.hpp:163
glm::vec2 velocity
Zone velocity.
glm::vec2 cross(glm::vec2 vec, float v)
Calculate the cross product vector between a 2D vector and a scalar.
void setMass(float amount)
Set the current mass.
Definition: mass.cpp:14
glm::vec2 force
Force.
Definition: collider.hpp:145
Material material
Material.
Definition: collider.hpp:173
Type type
Attractor type.
Definition: attractor.hpp:49
float invMass
Current inverted mass.
Definition: mass.hpp:56