-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics.c
More file actions
140 lines (114 loc) · 3.66 KB
/
physics.c
File metadata and controls
140 lines (114 loc) · 3.66 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
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "physics.h"
Ball Ball_set(Vec2 position)
{
Ball ball = { 0 };
ball.position = position;
ball.velocity = Vec2_set(0.f, 0.f);
ball.mass = 0.5f;
ball.friction = 3.f;
return ball;
}
int Ball_connect(Ball *ball1, Ball *ball2, float length)
{
if ((ball1 == ball2) || (ball1->nbSprings >= MAX_EDGES)|| (ball2->nbSprings >= MAX_EDGES))
return EXIT_FAILURE;
ball1->springs[ball1->nbSprings].other = ball2;
ball2->springs[ball2->nbSprings].other = ball1;
ball1->springs[ball1->nbSprings].length = length;
ball2->springs[ball2->nbSprings].length = length;
ball1->nbSprings++;
ball2->nbSprings++;
return EXIT_SUCCESS;
}
int Ball_deconnect(Ball *ball1, Ball *ball2)
{
int nbSprings1 = ball1->nbSprings;
int nbSprings2 = ball2->nbSprings;
int exitStatus = EXIT_SUCCESS;
int i;
// Remove ball2 from the egdes of ball1
for (i = 0; i < nbSprings1; ++i)
{
if (ball1->springs[i].other == ball2)
{
ball1->springs[i] = ball1->springs[nbSprings1 - 1];
ball1->nbSprings--;
break;
}
}
if (i >= nbSprings1)
{
exitStatus = EXIT_FAILURE;
}
// Remove ball1 from the egdes of ball2
for (i = 0; i < nbSprings2; ++i)
{
if (ball2->springs[i].other == ball1)
{
ball2->springs[i] = ball2->springs[nbSprings2 - 1];
ball2->nbSprings--;
break;
}
}
if (i >= nbSprings2)
{
exitStatus = EXIT_FAILURE;
}
return exitStatus;
}
void Ball_updateVelocity(Ball *ball, float timeStep)
{
//Données de la balle
float mass = ball->mass;
float friction = ball->friction;
Vec2 velocity = ball->velocity;
Vec2 position = ball->position;
Vec2 other;
int nb_springs = ball->nbSprings;
//***************************Calcul des forces appliqués****************************//
// Le poids
Vec2 gravity = Vec2_set(0, -9.81);
Vec2 weight = Vec2_scale(gravity, mass);
// La friction
Vec2 Friction = Vec2_scale(velocity, friction * -1);
Vec2 Rappel = Vec2_set(0.f, 0.f);
// Le rappel en fonction du nombre de balles ratachés
for (int i = 0; i < nb_springs; i++) {
other = ball->springs[i].other->position;
Vec2 vector_BalleAttach = Vec2_sub(other, position);
float l = Vec2_norm(vector_BalleAttach);
float l_0 = 1.f; // Longueur ressort au repos
float k = 300; // Raideur du ressort
Vec2 vector_I = Vec2_normalize(vector_BalleAttach);
Rappel = Vec2_add(Rappel, Vec2_scale(vector_I, k*(l-l_0)));
}
// La somme des force
Vec2 somme_forces = Vec2_add(weight, Friction);
somme_forces = Vec2_add(somme_forces, Rappel);
//******************* Calcule de la vitesse associé à la balle *******************//
// Calcul de l'accéleration de la balle
Vec2 acceleration = Vec2_scale(somme_forces, 1/mass);
// Calcul de la vitesse de la balle
Vec2 Velocity = Vec2_add(velocity, Vec2_scale(acceleration, timeStep));
// Update de la balle
ball->velocity = Velocity;
}
void Ball_updatePosition(Ball *ball, float timeStep)
{
Ball_updateVelocity(ball, timeStep);
// Données de la balle
Vec2 velocity = ball->velocity;
Vec2 position = ball->position;
// Calcul de la position de la balle
Vec2 Position = Vec2_add(position, Vec2_scale(velocity, timeStep));
// Détection de colisions avec le sol
if(Position.y < 1){
ball->velocity.y *= -1;
Position.y = 1.f;
}
// Update de la balle
ball->position = Position;
}