7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VPythonで簡単なオブジェクトを表示させてみる

Posted at

インストール

pip install vpython

Python3.6環境で普通に動いた。

動かしてみる

import vpython as vs
import numpy as np

s1 = vs.sphere(pos=vs.vector(0,0,0),radius=0.1,color=vs.color.green)
s2 = vs.sphere(pos=vs.vector(0,0,0),radius=0.1,color=vs.color.yellow)
s3 = vs.sphere(pos=vs.vector(0,0,0),radius=0.1,color=vs.color.red)
s1

p.png

原点をぐるぐる周るだけ。
このコードだと近日点移動しまくります。

class Obj:
    def __init__(self, x, y, vx, vy, o):
        self.x = x
        self.y = y
        self.vx = vx
        self.vy = vy
        self.o = o
    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.o.pos = vs.vector(self.x, self.y, 0)
    def f(self):
        t = np.arctan2(self.y, self.x)
        r2 = self.x * self.x + self.y * self.y
        F = - 0.5 / r2
        self.vx += F * np.cos(t)
        self.vy += F * np.sin(t)

b1 = Obj(-4.5, 0.0, 0.05, 0.3, s1)
b2 = Obj(-2.5, 0.0, 0.23, 0.45, s2)
b3 = Obj(-10.5, 0.0, 0.03, 0.15, s3)
b1.update()
b2.update()
b3.update()

for i in range(0, 2000):
    vs.rate(10)
    b1.f()
    b1.update()
    b2.f()
    b2.update()
    b3.f()
    b3.update()
7
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?