2012年10月19日金曜日

setRotation 回転行列 pyode

# coding: UTF-8
# setRotationの考察 
# よけいなコード 変数があるが後から利用するためおいておく
# 重力なし 衝突なし ただvpytonとbodyを描いただけ
# C:\myprg_main\python_my_prg\ode_prg\setRotation.py


import ode
from visual import *

boxList  = []
ballList = []
cylList  = []
box_kosuu=5
ball_kosuu=50

class Field:
    scene = display(autoscale=0, forward=norm((-2.0,-1.0,-1.0)))
   
    #物理世界を作成
    world=ode.World()
    #world.setGravity((0, -9.81, 0))
    world.setGravity((0, 0, 0))

    #地平面を作成?
    space=ode.Space()
    ode_floor=ode.GeomPlane(space,(0,1,0),0)
    ode_floor.setCategoryBits(1)
    ode_floor.setCollideBits(3)
    ode_floor.viz=box(
        pos=(0,-0.03,0),
        width=20,length=20,height=0.06,color=(0.5,0.5,1.0))

    #衝突関係
    jointgroup=ode.JointGroup()
    vball_x=sphere( pos=(0, 0, 0), radius=0.2, color=color.cyan)  #原点
    vball_x=sphere( pos=(5, 0, 0), radius=0.2, color=color.red)  #x軸
    vball_y=sphere( pos=(0, 5, 0), radius=0.2, color=color.white)#y軸
    vball_z=sphere( pos=(0, 0, 5), radius=0.2, color=color.yellow) #z軸

    target_fps=30
    #target_fps=30だから1秒間に30回の処理をする
    dt=1.0/target_fps

    def near_callback(self,args,geom1,geom2):
        #isBall = ((ground == o1) || (ground == o2));
        #if (isGround)//地面だけと衝突判定
        print geom1
        for c in ode.collide(geom1,geom2):
            c.setBounce(1.0)
            j=ode.ContactJoint(self.world,self.jointgroup,c)
            j.attach(geom1.getBody(),geom2.getBody())

    def tick(self):
        for i in  range(ball_kosuu):
            ballList[i].update()
        boxList[0].update()
        #cylList[0].update()
        print
        time.sleep(0.5)
        self.space.collide((),self.near_callback)
        self.world.step(self.dt)
        self.jointgroup.empty()
        return True

class Box:
    def __init__(
            self, field,
            m_density=0.01,
            v_x=7, v_y=1, v_z=3,
            b_pos=vector(0,3,0),
            v_color=color.cyan
            ):
        #デフォルトで...(vpthonのパラメータだけですます)
        self.m_x = v_x
        self.m_y = v_y
        self.m_z = v_z
        self.g_x = v_x
        self.g_y = v_y
        self.g_z = v_z

        self.body=ode.Body(field.world)
        M=ode.Mass()
        M.setBox(m_density, self.m_z, self.m_y, self.m_x)#密度,z,y,x w,h,l
        self.body.setMass(M)
        self.body.setPosition(b_pos)
        self.geom = ode.GeomBox(
                space=field.space, lengths=(self.g_z, self.g_y, self.g_x)#w,h,l
                )
        self.geom.setCategoryBits(1)
        self.geom.setCollideBits(7)
        #ボール同士の衝突を避ける
        self.geom.setBody(self.body)

        #self.geom.setRotation([0.866,0,0.5, 0,1,0, -0.5,0,0.866])#y軸30°回転
        #self.geom.setRotation([1,0,0, 0,0.866,-0.5, 0,0.5,0.866]) #x軸30°回転
        self.geom.setRotation([0.866,-0.5,0, 0.5,0.866,0, 0,0,1]) #z軸30°回転
        mat=self.geom.getRotation()
        self.f = frame()
        self.f.pos = b_pos
        self.f.axis=(mat[0],mat[3],mat[6])
        self.f.up=mat[1],mat[4],mat[7]
        self.vbox = box(frame=self.f,
                length=v_x, height=v_y, width=v_z, color=v_color)

        #mat=self.geom.getRotation()
        #self.f.axis=(10,-10,0)
        #self.f.axis=(mat[0],mat[3],mat[6])
        #self.f.up=mat[1],mat[4],mat[7]
        #self.f.axis=(1, 1, 1)
        #self.vbox.up=mat[1],mat[4],mat[7]
        #setRotationとmatの表示が微妙にずれるのでうえのコードで
        #vpytonのオブジェクトをmatで表示してからまたvpythonの
        #回転こまんどで微調整をしよう
   
        #self.geom.setRotation([0.866,0,0.5, 0,1,0, -0.5,0,0.866])
        # opacity :透明度
        #BoxのMassの大きさ,geomの大きさ,
        #   vboxの大きさを同じにする
        #print m_x,m_y,m_z

    def update(self):
        print 'box_update'
        pos = self.geom.getPosition()
        #boxのジオメトリの位置を得る
        #self.vbox.pos = pos
        self.f.pos = pos
        mat=self.geom.getRotation()
        #self.f.axis=(mat[0],mat[3],mat[6])
        #self.f.up=mat[1],mat[4],mat[7]



if __name__=='__main__':
    field = Field()
    box = Box(field)
   
#    #while True:
#    while Flg:
#        field.tick()

0 件のコメント:

コメントを投稿

About

参加ユーザー

連絡フォーム

名前

メール *

メッセージ *

ページ

Featured Posts