Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
python-gravity-engine
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
Show more breadcrumbs
Stephen D
python-gravity-engine
Commits
d6e94d23
Commit
d6e94d23
authored
6 years ago
by
Stephen D
Browse files
Options
Downloads
Patches
Plain Diff
First commit
parents
Branches
master
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
RigidMass.py
+35
-0
35 additions, 0 deletions
RigidMass.py
main.py
+68
-0
68 additions, 0 deletions
main.py
with
103 additions
and
0 deletions
RigidMass.py
0 → 100644
+
35
−
0
View file @
d6e94d23
import
math
G
=
6.67408E-11
class
RigidMass
:
def
__init__
(
self
,
mass
,
vx
,
vy
,
x
,
y
,
tick
):
self
.
mass
=
mass
self
.
vx
=
vx
self
.
vy
=
vy
self
.
x
=
x
self
.
y
=
y
self
.
dvx
,
self
.
dvy
=
0
,
0
self
.
tick
=
tick
#tick length, in seconds
def
gravity
(
self
,
p
):
#p is other particle
v
=
-
G
*
p
.
mass
/
self
.
distance_squared
(
p
)
*
self
.
tick
#print(self.distance_squared(p))
#self.v = v #debugging
dx
=
self
.
x
-
p
.
x
dy
=
self
.
y
-
p
.
y
hyp
=
math
.
sqrt
(
dx
**
2
+
dy
**
2
)
self
.
dvx
+=
v
*
(
dx
)
/
hyp
self
.
dvy
+=
v
*
(
dy
)
/
hyp
def
process_tick
(
self
):
self
.
vx
+=
self
.
dvx
self
.
vy
+=
self
.
dvy
self
.
dvx
,
self
.
dvy
=
0
,
0
self
.
x
+=
self
.
vx
*
self
.
tick
self
.
y
+=
self
.
vy
*
self
.
tick
def
distance_squared
(
self
,
p
):
return
abs
((
self
.
x
-
p
.
x
)
**
2
+
(
self
.
y
-
p
.
y
)
**
2
)
This diff is collapsed.
Click to expand it.
main.py
0 → 100644
+
68
−
0
View file @
d6e94d23
from
RigidMass
import
RigidMass
from
PIL
import
Image
from
PIL
import
ImageFont
from
PIL
import
ImageDraw
import
random
import
math
outImg
=
Image
.
new
(
"
RGB
"
,
[
1000
,
1000
])
def
storePixels
(
name
,
things
):
global
outImg
#outImg = Image.new("RGB", [1000, 1000])
w
,
h
=
1000
,
1000
for
thing
in
things
:
if
thing
.
x
>
0
and
thing
.
y
>
0
and
thing
.
x
<
1000
and
thing
.
y
<
1000
:
outImg
.
putpixel
((
int
(
thing
.
x
),
int
(
thing
.
y
)),
16581370
*
255
)
#outImg.save(name)
#sun = RigidMass( 500000000000000, 0, -5, 500, 500, 0.05)
#earth = RigidMass(500000000000000, 0, 5, 250, 500, 0.05)
tick_time
=
0.05
num_stars
=
1000
stars
=
[]
for
i
in
range
(
num_stars
):
#give each star some angular velocity
#did some cool algebra on paper
x
=
random
.
randint
(
375
,
625
)
y
=
random
.
randint
(
375
,
625
)
dx
=
x
-
500
dy
=
y
-
500
if
abs
(
dy
)
<=
abs
(
dx
):
vy
=
3
*
random
.
random
()
vx
=
-
vy
*
dy
/
dx
else
:
vx
=
3
*
random
.
random
()
vy
=
-
vx
*
dx
/
dy
#this makes the circle rotate together
#without it, one half rotates in the opposite direction
#the two halves are separated by the y=x line
#I have no idea why that happens, but this fixes it.
if
y
<
x
:
vx
=
-
vx
vy
=
-
vy
stars
.
append
(
RigidMass
(
50000000000
,
vx
,
vy
,
x
,
y
,
tick_time
))
for
i
in
range
(
0
,
50000
):
for
a
in
range
(
0
,
num_stars
):
for
b
in
range
(
0
,
num_stars
):
if
not
a
==
b
and
abs
(
stars
[
a
].
x
-
stars
[
b
].
x
)
>
0.5
and
abs
(
stars
[
a
].
y
-
stars
[
b
].
y
)
>
0.5
:
stars
[
a
].
gravity
(
stars
[
b
])
for
a
in
range
(
0
,
num_stars
):
stars
[
a
].
process_tick
()
storePixels
(
str
(
i
)
+
"
.png
"
,
stars
)
if
i
%
10
==
0
:
draw
=
ImageDraw
.
Draw
(
outImg
)
draw
.
text
((
0
,
0
),
"
t=
"
+
str
(
i
*
tick_time
)
+
"
s
"
,
(
255
,
255
,
255
))
outImg
.
save
(
"
img/
"
+
str
(
i
//
10
)
+
"
.png
"
)
outImg
=
Image
.
new
(
"
RGB
"
,
[
1000
,
1000
])
outImg
.
save
(
"
final.png
"
)
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