-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDragForceGenerator.cpp
More file actions
34 lines (25 loc) · 1.12 KB
/
DragForceGenerator.cpp
File metadata and controls
34 lines (25 loc) · 1.12 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
#include "DragForceGenerator.h"
#include "MotionComponent.h"
#include "PhysicsComponent.h"
#include "Entity.h"
void DragForceGenerator::AddForce(Entity *entity)
{
MotionComponent *motionComponent = entity->GetComponent<MotionComponent>();
PhysicsComponent *physicsComponent = entity->GetComponent<PhysicsComponent>();
// add linear drag
XMFLOAT3 velocity = motionComponent->GetVelocity();
float speed = XMVectorGetX(XMVector3Length(XMLoadFloat3(&velocity)));
XMVECTOR direction = XMVector3Normalize(XMLoadFloat3(&velocity));
XMFLOAT3 force;
XMVECTOR linear = -direction * speed * mTranslationDrag / 10.0f;
XMVECTOR quadratic = -direction * speed * speed * mTranslationDrag;
XMStoreFloat3(&force, linear + quadratic);
physicsComponent->AddForce(force);
// add rotational drag
XMFLOAT3 angularVelocity = motionComponent->GetAngularVelocity();
float spin = XMVectorGetX(XMVector3Length(XMLoadFloat3(&angularVelocity)));
XMVECTOR rotationAxis = XMVector3Normalize(XMLoadFloat3(&angularVelocity));
XMFLOAT3 torque;
XMStoreFloat3(&torque, -rotationAxis * spin * spin * mRotationDrag);
physicsComponent->AddTorque(torque);
}