-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysics.c
199 lines (156 loc) · 5.07 KB
/
physics.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "physics.h"
void PhysicsVector_init(PhysicsVector * vector, GLfloat x, GLfloat y, GLfloat z)
{
vector->x = x;
vector->y = y;
vector->z = z;
}
void PhysicsObject_init(PhysicsObject * obj, float mass, GLfloat x, GLfloat y,
GLfloat z)
{
obj->mass = mass;
PhysicsVector_init(&(obj->position), x, y, z);
PhysicsVector_init(&(obj->acceleration), 0, 0, 0);
PhysicsVector_init(&(obj->velocity), 0, 0, 0);
}
void PhysicsVector_add(PhysicsVector * dest, PhysicsVector * op1,
PhysicsVector * op2)
{
dest->x = op1->x + op2->x;
dest->y = op1->y + op2->y;
dest->z = op1->z + op2->z;
}
void PhysicsVector_sub(PhysicsVector * dest, PhysicsVector * op1,
PhysicsVector * op2)
{
dest->x = op1->x - op2->x;
dest->y = op1->y - op2->y;
dest->z = op1->z - op2->z;
}
int PhysicsVector_eq(PhysicsVector * op1, PhysicsVector * op2,
float tollerance)
{
if(fabs(op1->x - op2->x) > tollerance)
return 0;
if(fabs(op1->y - op2->y) > tollerance)
return 0;
if(fabs(op1->z - op2->z) > tollerance)
return 0;
return 1;
}
void PhysicsVector_mult(PhysicsVector * dest, PhysicsVector * op1,
PhysicsVector * op2)
{
dest->x = op1->x * op2->x;
dest->y = op1->y * op2->y;
dest->z = op1->z * op2->z;
}
void PhysicsVector_multScalar(PhysicsVector * dest, PhysicsVector * op1,
float op2)
{
PhysicsVector scalarVector;
PhysicsVector * scalarVectorPtr = &scalarVector;
PhysicsVector_init(scalarVectorPtr, op2, op2, op2);
PhysicsVector_mult(dest, op1, scalarVectorPtr);
}
void PhysicsVector_div(PhysicsVector * dest, PhysicsVector * op1,
PhysicsVector * op2)
{
dest->x = op1->x / op2->x;
dest->y = op1->y / op2->y;
dest->z = op1->z / op2->z;
}
void PhysicsVector_divScalar(PhysicsVector * dest, PhysicsVector * op1,
float op2)
{
PhysicsVector scalarVector;
PhysicsVector * scalarVectorPtr = &scalarVector;
PhysicsVector_init(scalarVectorPtr, op2, op2, op2);
PhysicsVector_div(dest, op1, scalarVectorPtr);
}
float PhysicsVector_length(PhysicsVector * target)
{
double total = 0;
total += pow(target->x, 2);
total += pow(target->y, 2);
total += pow(target->z, 2);
return (float)sqrt(total);
}
void PhysicsVector_distVector(PhysicsVector * dest, PhysicsVector * op1,
PhysicsVector * op2)
{
dest->x = op1->x - op2->x;
dest->y = op1->y - op2->y;
dest->z = op1->z - op2->z;
}
float PhysicsVector_dist(PhysicsVector * op1, PhysicsVector * op2)
{
PhysicsVector distanceVector;
PhysicsVector * distanceVectorPtr = &distanceVector;
PhysicsVector_distVector(distanceVectorPtr, op1, op2);
return PhysicsVector_length(distanceVectorPtr);
}
void PhysicsVector_normalize(PhysicsVector * target, PhysicsVector * op)
{
float magnitude = PhysicsVector_length(op);
PhysicsVector_divScalar(target, op, magnitude);
}
void PhysicsVector_limit(PhysicsVector * target, PhysicsVector * op,
float limit)
{
float curMagnitude = PhysicsVector_length(op);
if(curMagnitude <= limit)
return;
PhysicsVector_normalize(target, op);
PhysicsVector_multScalar(target, target, limit);
}
float PhysicsVector_dot(PhysicsVector * op1, PhysicsVector * op2)
{
float xComponent;
float yComponent;
float zComponent;
xComponent = op1->x * op1->x;
yComponent = op1->y * op1->y;
zComponent = op1->z * op1->z;
return xComponent + yComponent + zComponent;
}
float PhysicsVector_angle(PhysicsVector * op1, PhysicsVector * op2)
{
float dot = PhysicsVector_dot(op1, op2);
float len1 = PhysicsVector_length(op1);
float len2 = PhysicsVector_length(op2);
return acos(dot / (len1 * len2));
}
void PhysicsObject_addForce(PhysicsObject * physicsObject,
PhysicsVector * force)
{
PhysicsVector deltaAcceleration;
PhysicsVector * deltaAccelerationPtr = &deltaAcceleration;
PhysicsVector_multScalar(deltaAccelerationPtr, force, physicsObject->mass);
PhysicsVector * sumAccelerationPtr = &(physicsObject->acceleration);
PhysicsVector_add(sumAccelerationPtr, sumAccelerationPtr,
deltaAccelerationPtr);
}
void PhysicsObject_updateVelocity(PhysicsObject * physicsObject, float timeDur)
{
PhysicsVector * accelerationPtr;
PhysicsVector deltaVelocity;
PhysicsVector * deltaVelocityPtr = &deltaVelocity;
PhysicsVector * velocityPtr = &(physicsObject->velocity);
// Calculate change in velocity
accelerationPtr = &(physicsObject->acceleration);
PhysicsVector_multScalar(deltaVelocityPtr, accelerationPtr, timeDur);
// Add change in velocity
PhysicsVector_add(velocityPtr, velocityPtr, deltaVelocityPtr);
}
void PhysicsObject_updatePosition(PhysicsObject * physicsObject, float timeDur)
{
PhysicsVector deltaPos;
PhysicsVector * deltaPosPtr = &deltaPos;
PhysicsVector * velocityPtr = &(physicsObject->velocity);
PhysicsVector * positionPtr = &(physicsObject->position);
// Calculate change in velocity
PhysicsVector_multScalar(deltaPosPtr, velocityPtr, timeDur);
// Add change in velocity
PhysicsVector_add(positionPtr, positionPtr, deltaPosPtr);
}