-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbird_physics.c
130 lines (103 loc) · 3.48 KB
/
bird_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
#include "bird_physics.h"
void Bird_physics_init(Bird_physics * bird_physics, float x, float y, float z)
{
Bird_physics_initFull(bird_physics, x, y, z, BIRD_MIN_X, BIRD_MIN_Y, BIRD_MIN_Z,
BIRD_MAX_X, BIRD_MAX_Y, BIRD_MAX_Z);
}
void Bird_physics_initFull(Bird_physics * bird_physics, float x, float y, float z, int minX, int minY,
int minZ, int maxX, int maxY, int maxZ)
{
PhysicsObject_init(&(bird_physics->physicsObject), 1, x, y, z);
bird_physics->r = START_BIRD_ANGLE;
bird_physics->maxForce = MAX_BIRD_FORCE;
bird_physics->maxSpeed = MAX_BIRD_SPEED;
bird_physics->minX = minX;
bird_physics->minY = minY;
bird_physics->minZ = minZ;
bird_physics->maxX = maxX;
bird_physics->maxY = maxY;
bird_physics->maxZ = maxZ;
}
void Bird_physics_setPos(Bird_physics * bird_physics, float x, float y, float z)
{
bird_physics->physicsObject.position.x = x;
bird_physics->physicsObject.position.y = y;
bird_physics->physicsObject.position.z = z;
}
void Bird_physics_step(Bird_physics * bird_physics, float timestep)
{
PhysicsVector * acceleration;
Bird_physics_updatePosition(bird_physics, timestep);
acceleration = &(bird_physics->physicsObject.acceleration);
acceleration->x = 0;
acceleration->y = 0;
acceleration->z = 0;
PhysicsVector_normalize(&(bird_physics->heading), &(bird_physics->physicsObject.velocity));
}
void Bird_physics_updatePosition(Bird_physics * bird_physics, float timestep)
{
PhysicsObject * physicsObject = &(bird_physics->physicsObject);
PhysicsVector * velocity;
PhysicsObject_updateVelocity(physicsObject, timestep);
velocity = &(physicsObject->velocity);
PhysicsVector_limit(velocity, velocity, bird_physics->maxSpeed);
PhysicsObject_updatePosition(physicsObject, timestep);
Bird_physics_wrapOnBorders(bird_physics);
}
void Bird_physics_wrapOnBorders(Bird_physics * bird_physics)
{
PhysicsVector * position = &(bird_physics->physicsObject.position);
PhysicsVector * velocity = &(bird_physics->physicsObject.velocity);
if(position->x < bird_physics->minX)
{
position->x = bird_physics->minX;
velocity->x = -velocity->x;
}
if(position->y < bird_physics->minY)
{
position->y = bird_physics->minY;
velocity->y = -velocity->y;
}
if(position->z < bird_physics->minZ)
{
position->z = bird_physics->minZ;
velocity->z = -velocity->z;
}
if(position->x > bird_physics->maxX)
{
position->x = bird_physics->maxX;
velocity->x = -velocity->x;
}
if(position->y > bird_physics->maxY)
{
position->y = bird_physics->maxY;
velocity->y = -velocity->y;
}
if(position->z > bird_physics->maxZ)
{
position->z = bird_physics->maxZ;
velocity->z = -velocity->z;
}
}
float Bird_physics_getX(Bird_physics * bird_physics)
{
PhysicsVector * position = &(bird_physics->physicsObject.position);
return position->x;
}
float Bird_physics_getY(Bird_physics * bird_physics)
{
PhysicsVector * position = &(bird_physics->physicsObject.position);
return position->y;
}
float Bird_physics_getZ(Bird_physics * bird_physics)
{
PhysicsVector * position = &(bird_physics->physicsObject.position);
return position->z;
}
void Bird_physics_setVel(Bird_physics * bird_physics, float x, float y, float z)
{
PhysicsVector * velocity = &(bird_physics->physicsObject.velocity);
velocity->x = x;
velocity->y = y;
velocity->z = z;
}