-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysics.cpp
More file actions
49 lines (40 loc) · 1.53 KB
/
Copy pathPhysics.cpp
File metadata and controls
49 lines (40 loc) · 1.53 KB
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
45
46
47
48
49
#include "Physics.h"
#include "PhysicalActor2D.h"
//void PhysicsComponent::integrate(double dt, bool isKinematic) {
// Actor2D& actor = static_cast<Actor2D&>(*owner);
// actor.position += velocity * static_cast<float>(dt);
// actor.rotation += angularVelocity * static_cast<float>(dt);
//
// if (isKinematic) return;
//
// if (friction > 0.0f)
// velocity -= velocity * friction * static_cast<float>(dt);
// if (angularFriction > 0.0f)
// angularVelocity -= angularVelocity * angularFriction * static_cast<float>(dt);
//}
void PhysicsComponent::update(double dt) {
Actor2D& actor = static_cast<Actor2D&>(*owner);
actor.position += velocity * static_cast<float>(dt);
actor.rotation += angularVelocity * static_cast<float>(dt);
if (isKinematic) return;
if (friction > 0.0f)
velocity -= velocity * friction * static_cast<float>(dt);
if (angularFriction > 0.0f)
angularVelocity -= angularVelocity * angularFriction * static_cast<float>(dt);
}
void PhysicsComponent::applyForce(const glm::vec2& force, double dt) {
velocity += force * static_cast<float>(dt) / mass;
}
void PhysicsComponent::applyTorque(float torque, double dt) {
angularVelocity += torque * static_cast<float>(dt) / mass;
}
void PhysicsComponent::applyImpulse(const glm::vec2& impulse) {
velocity += impulse / mass;
}
void PhysicsComponent::applyAngularImpulse(float impulse) {
angularVelocity += impulse / mass;
}
void PhysicsComponent::reset() {
velocity = glm::vec2(0.0f);
angularVelocity = 0.0f;
}