1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[初心者備忘録]pybulletで回転するボールを床に落とす

1
Posted at

注意

僕は初心者です.
記事の通りにすれば動きはすると思いますが,熟練の方から見ると不自然なところがあるかもしれません.ご了承下さい.

環境構築

  1. C++を実行できるようにする
    https://visualstudio.microsoft.com/ja/visual-cpp-build-tools/
    このリンクの先のやつを押してダウンロードして実行する.
    「C++ によるデスクトップ開発」(Desktop development with C++)という項目にチェックを入れて,ダウンロードする.
  2. 適当なところに"Hoge"フォルダを作る
  3. コマンドプロンプロでそのフォルダまで移動する
  4. pybulletを入れるコマンドを実行
    「pip install pybullet」をコマンドプロンプトで実行するだけ
  5. なんか便利なgitリポジトリをクローン
    「git clone https://github.com/bulletphysics/bullet3.git」をコマンドプロンプトで実行するだけ
    Hogeフォルダの中にbullet3というフォルダができるはず

コーディング

先程作ったHogeフォルダの中にtest.pyをつくり
次のようにする.

import pybullet
import pybullet_data
import time
physicsClient = pybullet.connect(pybullet.GUI) 
pybullet.setAdditionalSearchPath(pybullet_data.getDataPath())
pybullet.setGravity(0,0,-9.8)#重力の設定
start_pos = [0,0,2]#ボールの初期位置
start_ori = pybullet.getQuaternionFromEuler([0, 0, 0])#ボールの初期角度?
object=pybullet.loadURDF("soccerball.urdf",basePosition = start_pos)#先ほどクローンしたリポジトリの中から読み込むオブジェクトとしてサッカーボールを選択.
floor = pybullet.loadURDF("plane.urdf")#同様に床を選択
bt=pybullet.addUserDebugParameter("Reset",1,0,0)#ボタンを追加
pre_bt_clicks=pybullet.readUserDebugParameter(bt)#クリックされた数を代入
pybullet.resetBaseVelocity(object,angularVelocity=[0,10,0])#サッカーボールの初期速度
while pybullet.isConnected():#while trueでもok
    pybullet.stepSimulation()
    time.sleep(1./240.)

    current_bt_clicks = pybullet.readUserDebugParameter(bt)#ボタンのクリック数を代入
    if current_bt_clicks > pre_bt_clicks:#クリック数が増えていたらResetする
        pybullet.resetBasePositionAndOrientation(object, start_pos, start_ori)
        pybullet.resetBaseVelocity(object, linearVelocity=[0,0,0],angularVelocity=[0,10,0])

        pre_bt_clicks = current_bt_clicks

実行

コマンドプロンプトでHogeフォルダに移動し次のように入力

python test.py

ウィンドウが表れシミュレーションが実行される.
右のResetボタンを押すと,リセットされる.
image.png

参考文献

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?