Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ISC_123 - Travail Pratique 001 - Simulation Système Planétaire
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
florian.burgener
ISC_123 - Travail Pratique 001 - Simulation Système Planétaire
Commits
647574ca
Commit
647574ca
authored
3 years ago
by
Florian Burgener
Browse files
Options
Downloads
Patches
Plain Diff
Add configurable timing to the simulation
parent
1ac257db
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
SolarSystem.c
+22
-12
22 additions, 12 deletions
SolarSystem.c
gfx/gfx.c
+1
-1
1 addition, 1 deletion
gfx/gfx.c
main.c
+30
-9
30 additions, 9 deletions
main.c
with
53 additions
and
22 deletions
SolarSystem.c
+
22
−
12
View file @
647574ca
...
@@ -22,17 +22,27 @@ SolarSystem *solar_system_create(double interval) {
...
@@ -22,17 +22,27 @@ SolarSystem *solar_system_create(double interval) {
double
perihelion_1
=
149
.
6
*
1E9
*
(
1
-
0
.
01671123
);
double
perihelion_1
=
149
.
6
*
1E9
*
(
1
-
0
.
01671123
);
solar_system
->
objects
[
1
]
=
celestial_object_create
(
5
.
972
*
1E24
,
vector2_create
(
-
perihelion_1
,
0
));
solar_system
->
objects
[
1
]
=
celestial_object_create
(
5
.
972
*
1E24
,
vector2_create
(
-
perihelion_1
,
0
));
double
perihelion_2
=
227
.
9
*
1E9
*
(
1
-
0
.
0
2
934
);
double
perihelion_2
=
227
.
9
*
1E9
*
(
1
-
0
.
0934
1233
);
solar_system
->
objects
[
2
]
=
celestial_object_create
(
6
.
39
*
1E23
,
vector2_create
(
-
perihelion_2
,
0
));
solar_system
->
objects
[
2
]
=
celestial_object_create
(
6
.
39
*
1E23
,
vector2_create
(
-
perihelion_2
,
0
));
return
solar_system
;
return
solar_system
;
}
}
Vector2
gravit
(
CelestialObject
*
sun
,
CelestialObject
*
b
)
{
CelestialObject
*
solar_system_get_star
(
SolarSystem
*
solar_system
)
{
Vector2
r
=
vector2_multiply
(
b
->
current_position
,
-
1
);
return
solar_system
->
objects
[
0
];
double
afff
=
G
*
sun
->
mass
*
(
1
.
0
/
pow
(
vector2_norm
(
r
),
3
));
}
Vector2
a
=
vector2_multiply
(
r
,
afff
);
// Vector2 a = vector2_multiply(f, 1.0 / b->mass);
Vector2
calculate_gravitational_acceleration
(
SolarSystem
*
solar_system
,
int32_t
object_index
)
{
Vector2
a
=
vector2_create_zero
();
for
(
int32_t
i
=
0
;
i
<
solar_system
->
objects_length
;
i
+=
1
)
{
if
(
i
==
object_index
)
continue
;
Vector2
r
=
vector2_substract
(
solar_system
->
objects
[
i
]
->
current_position
,
solar_system
->
objects
[
object_index
]
->
current_position
);
double
a_scalar
=
G
*
solar_system
->
objects
[
i
]
->
mass
*
pow
(
pow
(
vector2_norm
(
r
),
2
),
-
1
);
a
=
vector2_add
(
a
,
vector2_multiply
(
vector2_normalize
(
r
),
a_scalar
));
}
return
a
;
return
a
;
}
}
...
@@ -49,7 +59,7 @@ void solar_system_update(SolarSystem *solar_system) {
...
@@ -49,7 +59,7 @@ void solar_system_update(SolarSystem *solar_system) {
if
(
i
==
1
)
{
if
(
i
==
1
)
{
perihelion_speed
=
sqrt
((
G
*
star
->
mass
*
(
1
+
0
.
01671123
))
/
(
149
.
6
*
1E9
*
(
1
-
0
.
01671123
)));
perihelion_speed
=
sqrt
((
G
*
star
->
mass
*
(
1
+
0
.
01671123
))
/
(
149
.
6
*
1E9
*
(
1
-
0
.
01671123
)));
}
else
if
(
i
==
2
)
{
}
else
if
(
i
==
2
)
{
perihelion_speed
=
sqrt
((
G
*
star
->
mass
*
(
1
+
0
.
0
2
934
))
/
(
227
.
9
*
1E9
*
(
1
-
0
.
0
2
934
)));
perihelion_speed
=
sqrt
((
G
*
star
->
mass
*
(
1
+
0
.
0934
1233
))
/
(
227
.
9
*
1E9
*
(
1
-
0
.
0934
1233
)));
}
}
Vector2
tmp
=
object
->
current_position
;
Vector2
tmp
=
object
->
current_position
;
...
@@ -62,7 +72,7 @@ void solar_system_update(SolarSystem *solar_system) {
...
@@ -62,7 +72,7 @@ void solar_system_update(SolarSystem *solar_system) {
object
->
current_position
=
new_position
;
object
->
current_position
=
new_position
;
}
else
{
}
else
{
Vector2
new_position
=
vector2_substract
(
vector2_multiply
(
object
->
current_position
,
2
),
object
->
previous_position
);
Vector2
new_position
=
vector2_substract
(
vector2_multiply
(
object
->
current_position
,
2
),
object
->
previous_position
);
Vector2
a
=
gravit
(
solar_system
->
objects
[
0
],
object
);
Vector2
a
=
calculate_gravitational_acceleration
(
solar_system
,
i
);
new_position
=
vector2_add
(
new_position
,
vector2_multiply
(
a
,
pow
(
solar_system
->
interval
,
2
)));
new_position
=
vector2_add
(
new_position
,
vector2_multiply
(
a
,
pow
(
solar_system
->
interval
,
2
)));
object
->
previous_position
=
object
->
current_position
;
object
->
previous_position
=
object
->
current_position
;
object
->
current_position
=
new_position
;
object
->
current_position
=
new_position
;
...
@@ -73,18 +83,18 @@ void solar_system_update(SolarSystem *solar_system) {
...
@@ -73,18 +83,18 @@ void solar_system_update(SolarSystem *solar_system) {
void
solar_system_draw
(
SolarSystem
*
solar_system
,
struct
gfx_context_t
*
context
)
{
void
solar_system_draw
(
SolarSystem
*
solar_system
,
struct
gfx_context_t
*
context
)
{
gfx_clear
(
context
,
COLOR_BLACK
);
gfx_clear
(
context
,
COLOR_BLACK
);
for
(
u
int32_t
i
=
0
;
i
<
solar_system
->
objects_length
;
i
+=
1
)
{
for
(
int32_t
i
=
0
;
i
<
solar_system
->
objects_length
;
i
+=
1
)
{
CelestialObject
*
object
=
solar_system
->
objects
[
i
];
CelestialObject
*
object
=
solar_system
->
objects
[
i
];
Vector2
scaled_position
=
vector2_multiply
(
object
->
current_position
,
1
.
0
/
(
25
0
*
1E9
));
Vector2
scaled_position
=
vector2_multiply
(
object
->
current_position
,
1
.
0
/
(
30
0
*
1E9
));
scaled_position
=
vector2_fit_canvas
(
scaled_position
,
SCREEN_WIDTH
,
SCREEN_HEIGHT
);
scaled_position
=
vector2_fit_canvas
(
scaled_position
,
SCREEN_WIDTH
,
SCREEN_HEIGHT
);
if
(
i
==
0
)
{
if
(
i
==
0
)
{
draw_full_circle
(
context
,
scaled_position
.
x
,
scaled_position
.
y
,
7
0
,
COLOR_YELLOW
);
draw_full_circle
(
context
,
scaled_position
.
x
,
scaled_position
.
y
,
5
0
,
COLOR_YELLOW
);
}
}
if
(
i
==
1
)
{
if
(
i
==
1
)
{
draw_full_circle
(
context
,
scaled_position
.
x
,
scaled_position
.
y
,
15
,
COLOR_BLUE
);
draw_full_circle
(
context
,
scaled_position
.
x
,
scaled_position
.
y
,
20
,
COLOR_BLUE
);
}
}
if
(
i
==
2
)
{
if
(
i
==
2
)
{
...
...
This diff is collapsed.
Click to expand it.
gfx/gfx.c
+
1
−
1
View file @
647574ca
This diff is collapsed.
Click to expand it.
main.c
+
30
−
9
View file @
647574ca
...
@@ -2,7 +2,6 @@
...
@@ -2,7 +2,6 @@
#include
<stdio.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdlib.h>
#include
<time.h>
#include
<time.h>
#include
<unistd.h>
#include
"SolarSystem.h"
#include
"SolarSystem.h"
#include
"Vector2.h"
#include
"Vector2.h"
...
@@ -22,19 +21,41 @@ int main() {
...
@@ -22,19 +21,41 @@ int main() {
return
EXIT_FAILURE
;
return
EXIT_FAILURE
;
}
}
SolarSystem
*
solar_system
=
solar_system_create
(
10000
);
int32_t
time_elapsing_per_second
=
3600
*
24
*
365
;
int32_t
update_per_second
=
1000
;
printf
(
"%d
\n
"
,
time_elapsing_per_second
/
update_per_second
);
SolarSystem
*
solar_system
=
solar_system_create
(
time_elapsing_per_second
/
update_per_second
);
int32_t
hz
=
240
;
int32_t
a
=
0
;
int32_t
b
=
0
;
int32_t
elapsed_seconds
=
0
;
while
(
true
)
{
gfx_present
(
context
);
while
(
true
)
{
solar_system_update
(
solar_system
);
solar_system_update
(
solar_system
);
b
+=
1
;
if
((
double
)
b
>=
(
double
)
update_per_second
/
hz
*
(
a
+
1
))
{
a
+=
1
;
gfx_present
(
context
);
solar_system_draw
(
solar_system
,
context
);
solar_system_draw
(
solar_system
,
context
);
if
(
gfx_keypressed
()
==
SDLK_ESCAPE
)
{
if
(
b
>=
update_per_second
)
{
break
;
b
=
0
;
elapsed_seconds
+=
1
;
char
title
[
100
];
sprintf
(
title
,
"Solar System (%d)"
,
elapsed_seconds
);
SDL_SetWindowTitle
(
context
->
window
,
title
);
}
if
(
a
%
hz
==
0
)
a
=
0
;
}
}
sleep
(
0
.
01
);
if
(
gfx_keypressed
()
==
SDLK_ESCAPE
)
break
;
struct
timespec
t
=
{.
tv_sec
=
0
,
.
tv_nsec
=
1
.
0
/
update_per_second
*
1E9
};
nanosleep
(
&
t
,
NULL
);
}
}
gfx_destroy
(
context
);
gfx_destroy
(
context
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment