Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

インストール

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()
tibigame
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away