Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
phys_planetes
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
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
boris.stefanov
phys_planetes
Commits
814a2ed6
Commit
814a2ed6
authored
3 years ago
by
BobLeHibou
Browse files
Options
Downloads
Patches
Plain Diff
EDIT: compiles but system is not centered
parent
dab469d6
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
Makefile
+6
-5
6 additions, 5 deletions
Makefile
main.c
+3
-3
3 additions, 3 deletions
main.c
planet/planet.c
+12
-10
12 additions, 10 deletions
planet/planet.c
planet/planet.h
+11
-8
11 additions, 8 deletions
planet/planet.h
with
32 additions
and
26 deletions
Makefile
+
6
−
5
View file @
814a2ed6
...
...
@@ -7,22 +7,23 @@ LDFLAGS:=-lm -lSDL2
#Path to the lib Vec2
VPATH
:=
vec2 gfx planet
HDR
:=
gfx/gfx.h planet/constants.h planet/planet.h vec2/vec2.h
main
:
main.o vec2.o gfx.o planet.o
$(
CC
)
$(
CFLAGS
)
-o
$@
$^
$(
LDFLAGS
)
run_tests
:
tests
run_tests
:
vec2_
tests
./
$<
tests
:
vec_tests.o vec2.o
vec2_
tests
:
vec_tests.o vec2.o
$(
CC
)
$(
CFLAGS
)
-o
$@
$^
$(
LDFLAGS
)
planet.o
:
planet.h
planet.o
:
${HDR}
vec2.o
:
vec2.h
vec2.o
:
${HDR}
gfx.o
:
gfx.h
gfx.o
:
${HDR}
clean
:
rm
-f
*
.o main tests
This diff is collapsed.
Click to expand it.
main.c
+
3
−
3
View file @
814a2ed6
...
...
@@ -28,17 +28,17 @@ int main() {
gfx_present
(
ctxt
);
gfx_clear
(
ctxt
,
COLOR_BLACK
);
// begin : draw the current state of your system
show_system
(
ctxt
,
sys
);
show_system
(
ctxt
,
&
sys
);
// end : draw state of system
// begin : update your system
update_system
(
sys
,
DELTA_T
);
update_system
(
&
sys
,
DELTA_T
);
usleep
(
100000
);
// end : update system
if
(
gfx_keypressed
()
==
SDLK_ESCAPE
)
{
break
;
}
}
// begin : Free your system
free_system
(
sys
);
free_system
(
&
sys
);
// end : free system
gfx_destroy
(
ctxt
);
return
EXIT_SUCCESS
;
...
...
This diff is collapsed.
Click to expand it.
planet/planet.c
+
12
−
10
View file @
814a2ed6
...
...
@@ -3,6 +3,8 @@
#include
"constants.h"
#define NB_PLANETS 4
planet_t
create_planet
(
double
mass
,
vec2
pos
)
{
planet_t
p
;
...
...
@@ -18,23 +20,22 @@ system_t create_system(double delta_t) {
system
.
star
=
create_planet
(
M_SOLEIL
,
vec2_create_zero
());
system
.
nb_planets
=
4
;
system
.
planets
=
malloc
(
system
.
nb_planets
*
sizeof
(
planet_t
));
if
(
NULL
==
system
.
planets
)
return
NULL
;
double
masses
[
system
.
nb_planets
]
=
{
double
masses
[
NB_PLANETS
]
=
{
M_MERCURE
,
M_VENUS
,
M_TERRE
,
M_MARS
};
vec2
positions
[
system
.
nb_planets
]
=
{
vec2
positions
[
NB_PLANETS
]
=
{
vec2_create
(
PER_MERCURE
,
0
.
0
),
vec2_create
(
PER_VENUS
,
0
.
0
),
vec2_create
(
PER_TERRE
,
0
.
0
),
vec2_create
(
PER_MARS
,
0
.
0
)};
double
eccentricities
[
system
.
nb_planets
]
=
{
double
eccentricities
[
NB_PLANETS
]
=
{
E_MERCURE
,
E_VENUS
,
E_TERRE
,
E_MARS
};
double
axis_major
[
system
.
nb_planets
]
=
{
double
axis_major
[
NB_PLANETS
]
=
{
A_MERCURE
,
A_VENUS
,
A_TERRE
,
...
...
@@ -47,21 +48,21 @@ system_t create_system(double delta_t) {
vel
=
sqrt
(
vel
);
const
vec2
ru
=
vec2_normalize
(
vec2_sub
(
system
.
planets
[
i
].
pos
,
system
.
star
.
pos
));
const
vec2
rp
=
vec2_create
(
-
ru
.
y
,
ru
.
x
);
const
vec2
v
=
vec2_mul
(
vel
,
r
);
const
vec2
v
=
vec2_mul
(
vel
,
r
p
);
system
.
planets
[
i
].
prec_pos
=
vec2_sub
(
system
.
planets
[
i
].
pos
,
vec2_mul
(
delta_t
,
v
));
}
return
system
;
}
void
show_system
(
struct
gfx_context_t
*
ctxt
,
system_t
*
system
)
{
void
show_system
(
struct
gfx_context_t
*
ctxt
,
system_t
*
system
,
const
uint32_t
width
,
const
uint32_t
height
)
{
// draw sun
const
coordinates
xy_sun
=
vec2_to_coordinates_centered
(
vec2_normalize
(
system
->
star
.
pos
));
const
coordinates
xy_sun
=
vec2_to_coordinates_centered
(
vec2_normalize
(
system
->
star
.
pos
)
,
width
,
height
);
draw_full_circle
(
ctxt
,
xy_sun
.
column
,
xy_sun
.
row
,
50
,
COLOR_YELLOW
);
// draw planets
for
(
uint32_t
i
=
0
;
i
<
sys
.
nb_planets
;
++
i
)
{
for
(
uint32_t
i
=
0
;
i
<
sys
tem
->
nb_planets
;
++
i
)
{
const
coordinates
xy
=
vec2_to_coordinates_centered
(
vec2_normalize
(
sys
.
planets
[
i
].
pos
),
vec2_normalize
(
sys
tem
->
planets
[
i
].
pos
),
ctxt
->
width
,
ctxt
->
height
);
draw_full_circle
(
ctxt
,
xy
.
column
,
xy
.
row
,
20
,
COLOR_BLUE
);
...
...
@@ -72,6 +73,7 @@ void show_system(struct gfx_context_t* ctxt, system_t* system) {
void
update_system
(
system_t
*
system
,
double
delta_t
)
{
for
(
uint32_t
i
=
0
;
i
<
system
->
nb_planets
;
++
i
)
{
// apply the effects of the sun
const
double
r_star_2
=
vec2_norm_sqr
(
vec2_sub
(
system
->
planets
[
i
].
pos
,
system
->
star
.
pos
));
vec2
a
=
vec2_mul
((
G
*
system
->
star
.
mass
)
/
r_star_2
,
vec2_normalize
(
system
->
planets
[
i
].
pos
));
// apply the effects of all other planets
for
(
uint32_t
k
=
0
;
k
<
system
->
nb_planets
;
++
k
)
{
...
...
This diff is collapsed.
Click to expand it.
planet/planet.h
+
11
−
8
View file @
814a2ed6
...
...
@@ -4,26 +4,29 @@
#include
"../vec2/vec2.h"
#include
"../gfx/gfx.h"
typedef
struct
_planet
{
typedef
struct
_planet
{
double
mass
;
vec2
pos
;
// x(t)
vec2
prec_pos
;
// x(t - dt)
}
planet_t
;
typedef
struct
_system
{
typedef
struct
_system
{
planet_t
star
;
// ex. The sun
uint32_t
nb_planets
;
// The number of orbiting planets
planet_t
*
planets
;
// An array of orbiting planets
planet_t
*
planets
;
// An array of orbiting planets
}
system_t
;
// These functions are not mandatory to implement,
// it's rather a hint of what you should have.
planet_t
create_planet
(
double
mass
,
vec2
pos
);
system_t
create_system
(
double
delta_t
);
void
show_system
(
struct
gfx_context_t
*
ctxt
,
system_t
*
system
);
void
update_system
(
system_t
*
system
,
double
delta_t
);
void
free_system
(
system_t
*
system
);
//void show_system(struct gfx_context_t *ctxt, system_t *system);
void
show_system
(
struct
gfx_context_t
*
ctxt
,
system_t
*
system
,
const
uint32_t
width
,
const
uint32_t
height
);
void
update_system
(
system_t
*
system
,
double
delta_t
);
void
free_system
(
system_t
*
system
);
#endif
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