-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRigidBody.h
71 lines (58 loc) · 1.58 KB
/
RigidBody.h
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
#include "Vector2.h"
#include "Coords2.h"
#include "Geom.h"
struct RigidBody
{
RigidBody(){}
RigidBody(Coords2f coords, Vector2f size, float density)
{
this->coords = coords;
displacingVelocity = Vector2f(0.0f, 0.0f);
displacingAngularVelocity = 0.0f;
acceleration = Vector2f(0.0f, 0.0f);
angularAcceleration = 0.0f;
velocity = Vector2f(0.0f, 0.0f);
angularVelocity = 0.0f;
geom.size = size;
float mass = density * (size.x * size.y);
float inertia = mass * (size.x * size.x + size.y * size.y);
invMass = 1.0f / mass;
invInertia = 1.0f / inertia;
UpdateGeom();
}
void IntegrateVelocity(float dt)
{
velocity += acceleration * dt;
acceleration = Vector2f(0.0f, 0.0f);
angularVelocity += angularAcceleration * dt;
angularAcceleration = 0.0f;
}
void IntegratePosition(float dt)
{
coords.pos += displacingVelocity + velocity * dt;
coords.Rotate(-displacingAngularVelocity);
coords.Rotate(-angularVelocity * dt);
displacingVelocity = Vector2f(0.0f, 0.0f);
displacingAngularVelocity = 0.0f;
UpdateGeom();
}
Vector2f GetGlobalPointVelocity(Vector2f point)
{
Vector2f relpos = point - coords.pos;
return Vector2f(-relpos.y, relpos.x) * angularVelocity + velocity;
}
void UpdateGeom()
{
geom.coords = coords;
geom.RecomputeAABB();
}
Geom geom;
Vector2f velocity, acceleration;
Vector2f displacingVelocity;
float angularVelocity, angularAcceleration;
float displacingAngularVelocity;
float invMass, invInertia;
Coords2f coords;
char padding[1024];
};