Accueil > > > MOTEUR PHYSIQUE ODE (PYODE) ET VPYTHON
MOTEUR PHYSIQUE ODE (PYODE) ET VPYTHON
Information sur la source
Description
Voici un exemple de mariage entre la 3D offerte par la lib VPYTHON, et le moteur physique de gestion de collision ODE, utilisée grâce au "wrappeur " PyOde.
Source
- from visual import *
- import ode
-
-
- class VisualOdeObject(object):
- """
- encapsule un objet de Vpyton
- """
- def __init__(self,name,position):
- self.name = name
- self.position = position
- self.body = None
-
- def draw (self):
- raise NotImplementedError()
-
- def add_Force (self,value):
-
- self.body.addForce(value)
-
-
- class Floor(VisualOdeObject):
- """
- represente le sol
- """
-
- def __init__(self,space,name, position=(0,0,0), Length = 10, Width = 10):
- VisualOdeObject.__init__(self,name, position)
-
- # Create a plane geom which prevent the objects from falling forever
- self.floor = ode.GeomPlane(space, position, 0)
- self.Length = Length
- self.Width = Width
-
- def draw (self):
-
- self.View = box(pos = self.position,
- length = self.Length ,
- width = self.Width ,
- height = 0.01 )
-
- class Box(VisualOdeObject):
- """
- represente une boite
- """
-
- def __init__(self,world, space,name,
- density,
- lx, ly, lz,
- position = (0,2,0),
- color= color.red):
-
- """Create a box body and its corresponding geom."""
- VisualOdeObject.__init__(self,name,position)
- self.lx = lx
- self.ly = ly
- self.lz = lz
- # Create body
- self.body = ode.Body(world)
- M = ode.Mass()
- M.setBox(density, lx, ly, lz)
- self.body.setMass(M)
- self.body.setPosition (self.position)
-
- # Create a box geom for collision detection
- geom = ode.GeomBox(space, lengths=(lx, ly, lz))
- geom.setBody(self.body)
-
- self.color = color
-
- def draw (self):
- self.box = box(pos = self.position,
- length = self.lx,
- width = self.ly ,
- height = self.lz ,
- color = self.color)
-
- self.label1 = label(text= self.name , pos = self.position, opacity =0)
-
- self.vectorVitesseArrow = arrow(pos = self.position,
- axis = (0,0,0),
- shaftwidth = 0.0001,
- color = color.green)
-
- def redraw (self):
- x,y,z = self.body.getPosition()
- u,v,w = self.body.getLinearVel()
-
- R = self.body.getRotation()
-
-
- self.box.pos= x,y,z
- self.box.axis=R[0], R[3], R[6]
- self.box.up = R[1], R[4], R[7]
-
- self.label1.text= "%s : pos=(%.1f, %.1f, %.1f) "%(self.name,x, y, z)
- self.label1.pos = x, y + 0.5 ,z
-
- self.vectorVitesseArrow.pos = x,y,z
- self.vectorVitesseArrow.axis = [0.1*u,0.1*v,0.1*w]
-
- class Sphere(VisualOdeObject):
- """
- represente une sphere
- """
-
- def __init__(self,
- world,
- space,
- name,
- density=1000,
- radius=1.0,
- position = (0,2,0),
- color = color.red):
-
- """Create a sphere body and its corresponding geom."""
- VisualOdeObject.__init__(self,name,position)
- # Create body
- self.body = ode.Body(world)
- self.body.setPosition (self.position)
-
- self.radius = radius
- self.color = color
-
- M = ode.Mass()
- M.setSphere(density, radius)
- self.body.setMass(M)
-
- # Create a box geom for collision detection
- geom = ode.GeomSphere(space, radius)
-
- geom.setBody(self.body)
-
-
- def draw (self):
-
- self.ball1 = sphere(pos =self.position, radius = self.radius, color = self.color)
-
- self.label1 = label(text= self.name , pos = self.position, opacity =0)
- self.vectorVitesseArrow = arrow(pos = self.position,
- axis = (0,0,0),
- shaftwidth = 0.0001,
- color = color.green)
-
- def redraw (self):
- x,y,z= self.body.getPosition()
- u,v,w =self.body.getLinearVel()
-
- self.ball1.pos= x,y,z
-
- self.label1.text= "%s : pos=(%.1f, %.1f, %.1f) "%(self.name,x, y, z)
- self.label1.pos = x, y + 0.5 ,z # Y is up direction
- self.vectorVitesseArrow.pos = x,y,z
- self.vectorVitesseArrow.axis = vector([0.1*u,0.1*v,0.1*w])
-
-
- class Word_Simulation(object):
- """
- class comment
- """
-
- def __init__(self, Wordlimit):
- """
- Limit of word : 3-Uplet (X,Z,Y). ex : (10,10,10)
- """
- # create world object (the dynamics world)
- self.world = ode.World()
- self.world.setGravity((0,-9.81,0))
- self.world.setERP(0.8)# error reduction parameter
- self.world.setCFM(1E-5)# constraint force mixing
-
- self.wordlimit = Wordlimit #Not used Yet
-
- # Create a space object
- #~ self.space = ode.Space()
- self.space = ode.QuadTreeSpace((0, 0, 0), (20, 20, 20), 10)
- #Objets mobiles
- self.objetsMobilesDict = dict()
- #Objets fixe
- self.objetsFixesList = []
-
- def addFloor(self, name,position,Length = 10, Width = 10):
- theFloor = Floor(self.space,name, position, Length, Width)
- self.objetsFixesList.append (theFloor )
-
-
- def addObject (self,name ,TypeOfObject, *parameters,**kw):
- #Creation of an Object that may moved
-
- theObjet = TypeOfObject(world=self.world,space=self.space, name = name, *parameters,**kw)
- self.objetsMobilesDict[name]= theObjet
-
- def getObjet(self, name):
- return self.objetsMobilesDict[name]
-
- def drawObjects (self):
- "Dessine tous les objets fixes une fois"
- for aFixObject in self.objetsFixesList:
- aFixObject.draw()
-
- for aMobileObject in self.objetsMobilesDict.values():
- aMobileObject.draw()
-
- def launch(self):
- contactgroup = ode.JointGroup()
- loopFlag = True
- total_time = 0.0
- dt = 0.04
- while loopFlag:
- import time
- #~ time.sleep(0.50)
-
- # Detect collisions and create contact joints
- self.space.collide((self.world,contactgroup), self.process_collision_callback )
- for anObject in self.objetsMobilesDict.values():
- anObject.redraw()
-
- # Next simulation step
- self.world.step(dt)
- total_time+=dt
- # Remove all contact joints
- contactgroup.empty()
- # Try to keep the specified framerate
- rate(25)
-
-
- def process_collision_callback(self,args, geom1, geom2):
- """Callback function for the collide() method.
-
- This function checks if the given geoms do collide and
- creates contact joints if they do.
- """
-
- # Check if the objects do collide
- contacts = ode.collide(geom1, geom2)
-
- # Create contact joints
- world,contactgroup = args
- for c in contacts:
- #~ print c
- c.setBounce(0.8) # bouncyness
- c.setMu(800)
- j = ode.ContactJoint(world, contactgroup, c)
- j.attach(geom1.getBody(), geom2.getBody())
-
-
- class ViewWorld(object):
- """
- class comment
- """
-
- def __init__(self, worldLimits = vector(10,10,10) ):
-
- #initialize Pyode controler: Wordlimit is 3-uplet, not a vector wich type belong to theView VPython
- self.theWord_Simulation = Word_Simulation(Wordlimit = (worldLimits.x,worldLimits.y, worldLimits.z))
-
- self.theScene = scene # From Visual
-
- self.worldLimits = worldLimits
- self.initScene()
-
- def initScene(self):
- #~ self.theScene.x = 100
- #~ self.theScene.y = 10
- self.theScene.height = 800
- self.theScene.width = 800
- self.theScene.forward = array((0.000001,0,-1))
- self.theScene.scale = (0.1,0.1,0.1) # X,Z,Y
- self.theScene.title = "Pyode and Vpython"
- self.theScene.fullscreen = 1
- self.theScene.background = [0.94,0.79,0.67]
- self.theScene.center = (0,1,0) # X,Y,Z : Y is the Up direction
-
- self.theScene.lights = [vector(-0.1,0.3,0.1)]
- self.theScene.ambient = 0.5
-
- def createScence (self):
- self.theWord_Simulation.addFloor (name = "The Floor 0",
- position = (0,0.01,0),
- Length = self.worldLimits.x,
- Width = self.worldLimits.y)
-
-
- self.theWord_Simulation.addObject(name ="Ref_1",
- TypeOfObject= Sphere ,
- density = 100,
- radius = 0.5,
- position = (10,1,10),# X,Y,Z
- color = color.red)
-
- self.theWord_Simulation.addObject(name ="Ref_2",
- TypeOfObject= Sphere ,
- density = 100,
- radius = 0.5,
- position = (-10,1,-10),# X,Y,Z
- color = color.red)
-
- # Create a Origine
- self.theWord_Simulation.addObject(name ="Origin",
- TypeOfObject= Sphere ,
- density = 1000,
- radius = 0.5,
- position = (0,2,0), #X,Y,Z
- color = color.blue)
-
-
- self.theWord_Simulation.addObject(name = "Boite",
- TypeOfObject= Box ,
- density = 1000,
- lx = 0.2,
- ly = 0.2,
- lz = 0.2,
- position = (0.1,5,0),
- color = color.green)
- self.addRandomObjects()
-
-
- self.theWord_Simulation.drawObjects()
-
- def addRandomObjects(self):
- from random import Random
- sourceRandom = Random()
- WordLength = self.worldLimits.x
- WordWidth = self.worldLimits.y
- WordHeight = self.worldLimits.z
- BALL_NUMBER = 100
- for aBallNumber in range(BALL_NUMBER):
-
- colorBall =[ sourceRandom.random(),sourceRandom.random(),sourceRandom.random()]
- posX = sourceRandom.randrange(0,int(WordLength/2),1) * sourceRandom.choice([1,-1])
- posZ = sourceRandom.randrange(0,int(WordWidth/2),1) * sourceRandom.choice([1,-1])
- posY = sourceRandom.randrange(0,int(WordHeight),1)
-
- self.theWord_Simulation.addObject(name ="G_Sphere_%d"%(aBallNumber),
- TypeOfObject= Sphere ,
- density = 1000,
- radius = 0.2,
- position = (posX+0.1,posY+0.5,posZ),
- color = colorBall)
- self.theWord_Simulation.addObject(name = "G_Box_%d"%(aBallNumber),
- TypeOfObject= Box ,
- density = 7000,
- lx = 0.2,
- ly = 0.2,
- lz = 0.2,
- position = (posX,posY,posZ),
- color = colorBall)
-
-
-
-
- def launchSimulation(self):
- self.theWord_Simulation.launch()
-
-
- aViewWorld = ViewWorld(worldLimits=vector(20,20,20))#X,Y,Z
- aViewWorld.createScence()
- aViewWorld.launchSimulation()
from visual import *
import ode
class VisualOdeObject(object):
"""
encapsule un objet de Vpyton
"""
def __init__(self,name,position):
self.name = name
self.position = position
self.body = None
def draw (self):
raise NotImplementedError()
def add_Force (self,value):
self.body.addForce(value)
class Floor(VisualOdeObject):
"""
represente le sol
"""
def __init__(self,space,name, position=(0,0,0), Length = 10, Width = 10):
VisualOdeObject.__init__(self,name, position)
# Create a plane geom which prevent the objects from falling forever
self.floor = ode.GeomPlane(space, position, 0)
self.Length = Length
self.Width = Width
def draw (self):
self.View = box(pos = self.position,
length = self.Length ,
width = self.Width ,
height = 0.01 )
class Box(VisualOdeObject):
"""
represente une boite
"""
def __init__(self,world, space,name,
density,
lx, ly, lz,
position = (0,2,0),
color= color.red):
"""Create a box body and its corresponding geom."""
VisualOdeObject.__init__(self,name,position)
self.lx = lx
self.ly = ly
self.lz = lz
# Create body
self.body = ode.Body(world)
M = ode.Mass()
M.setBox(density, lx, ly, lz)
self.body.setMass(M)
self.body.setPosition (self.position)
# Create a box geom for collision detection
geom = ode.GeomBox(space, lengths=(lx, ly, lz))
geom.setBody(self.body)
self.color = color
def draw (self):
self.box = box(pos = self.position,
length = self.lx,
width = self.ly ,
height = self.lz ,
color = self.color)
self.label1 = label(text= self.name , pos = self.position, opacity =0)
self.vectorVitesseArrow = arrow(pos = self.position,
axis = (0,0,0),
shaftwidth = 0.0001,
color = color.green)
def redraw (self):
x,y,z = self.body.getPosition()
u,v,w = self.body.getLinearVel()
R = self.body.getRotation()
self.box.pos= x,y,z
self.box.axis=R[0], R[3], R[6]
self.box.up = R[1], R[4], R[7]
self.label1.text= "%s : pos=(%.1f, %.1f, %.1f) "%(self.name,x, y, z)
self.label1.pos = x, y + 0.5 ,z
self.vectorVitesseArrow.pos = x,y,z
self.vectorVitesseArrow.axis = [0.1*u,0.1*v,0.1*w]
class Sphere(VisualOdeObject):
"""
represente une sphere
"""
def __init__(self,
world,
space,
name,
density=1000,
radius=1.0,
position = (0,2,0),
color = color.red):
"""Create a sphere body and its corresponding geom."""
VisualOdeObject.__init__(self,name,position)
# Create body
self.body = ode.Body(world)
self.body.setPosition (self.position)
self.radius = radius
self.color = color
M = ode.Mass()
M.setSphere(density, radius)
self.body.setMass(M)
# Create a box geom for collision detection
geom = ode.GeomSphere(space, radius)
geom.setBody(self.body)
def draw (self):
self.ball1 = sphere(pos =self.position, radius = self.radius, color = self.color)
self.label1 = label(text= self.name , pos = self.position, opacity =0)
self.vectorVitesseArrow = arrow(pos = self.position,
axis = (0,0,0),
shaftwidth = 0.0001,
color = color.green)
def redraw (self):
x,y,z= self.body.getPosition()
u,v,w =self.body.getLinearVel()
self.ball1.pos= x,y,z
self.label1.text= "%s : pos=(%.1f, %.1f, %.1f) "%(self.name,x, y, z)
self.label1.pos = x, y + 0.5 ,z # Y is up direction
self.vectorVitesseArrow.pos = x,y,z
self.vectorVitesseArrow.axis = vector([0.1*u,0.1*v,0.1*w])
class Word_Simulation(object):
"""
class comment
"""
def __init__(self, Wordlimit):
"""
Limit of word : 3-Uplet (X,Z,Y). ex : (10,10,10)
"""
# create world object (the dynamics world)
self.world = ode.World()
self.world.setGravity((0,-9.81,0))
self.world.setERP(0.8)# error reduction parameter
self.world.setCFM(1E-5)# constraint force mixing
self.wordlimit = Wordlimit #Not used Yet
# Create a space object
#~ self.space = ode.Space()
self.space = ode.QuadTreeSpace((0, 0, 0), (20, 20, 20), 10)
#Objets mobiles
self.objetsMobilesDict = dict()
#Objets fixe
self.objetsFixesList = []
def addFloor(self, name,position,Length = 10, Width = 10):
theFloor = Floor(self.space,name, position, Length, Width)
self.objetsFixesList.append (theFloor )
def addObject (self,name ,TypeOfObject, *parameters,**kw):
#Creation of an Object that may moved
theObjet = TypeOfObject(world=self.world,space=self.space, name = name, *parameters,**kw)
self.objetsMobilesDict[name]= theObjet
def getObjet(self, name):
return self.objetsMobilesDict[name]
def drawObjects (self):
"Dessine tous les objets fixes une fois"
for aFixObject in self.objetsFixesList:
aFixObject.draw()
for aMobileObject in self.objetsMobilesDict.values():
aMobileObject.draw()
def launch(self):
contactgroup = ode.JointGroup()
loopFlag = True
total_time = 0.0
dt = 0.04
while loopFlag:
import time
#~ time.sleep(0.50)
# Detect collisions and create contact joints
self.space.collide((self.world,contactgroup), self.process_collision_callback )
for anObject in self.objetsMobilesDict.values():
anObject.redraw()
# Next simulation step
self.world.step(dt)
total_time+=dt
# Remove all contact joints
contactgroup.empty()
# Try to keep the specified framerate
rate(25)
def process_collision_callback(self,args, geom1, geom2):
"""Callback function for the collide() method.
This function checks if the given geoms do collide and
creates contact joints if they do.
"""
# Check if the objects do collide
contacts = ode.collide(geom1, geom2)
# Create contact joints
world,contactgroup = args
for c in contacts:
#~ print c
c.setBounce(0.8) # bouncyness
c.setMu(800)
j = ode.ContactJoint(world, contactgroup, c)
j.attach(geom1.getBody(), geom2.getBody())
class ViewWorld(object):
"""
class comment
"""
def __init__(self, worldLimits = vector(10,10,10) ):
#initialize Pyode controler: Wordlimit is 3-uplet, not a vector wich type belong to theView VPython
self.theWord_Simulation = Word_Simulation(Wordlimit = (worldLimits.x,worldLimits.y, worldLimits.z))
self.theScene = scene # From Visual
self.worldLimits = worldLimits
self.initScene()
def initScene(self):
#~ self.theScene.x = 100
#~ self.theScene.y = 10
self.theScene.height = 800
self.theScene.width = 800
self.theScene.forward = array((0.000001,0,-1))
self.theScene.scale = (0.1,0.1,0.1) # X,Z,Y
self.theScene.title = "Pyode and Vpython"
self.theScene.fullscreen = 1
self.theScene.background = [0.94,0.79,0.67]
self.theScene.center = (0,1,0) # X,Y,Z : Y is the Up direction
self.theScene.lights = [vector(-0.1,0.3,0.1)]
self.theScene.ambient = 0.5
def createScence (self):
self.theWord_Simulation.addFloor (name = "The Floor 0",
position = (0,0.01,0),
Length = self.worldLimits.x,
Width = self.worldLimits.y)
self.theWord_Simulation.addObject(name ="Ref_1",
TypeOfObject= Sphere ,
density = 100,
radius = 0.5,
position = (10,1,10),# X,Y,Z
color = color.red)
self.theWord_Simulation.addObject(name ="Ref_2",
TypeOfObject= Sphere ,
density = 100,
radius = 0.5,
position = (-10,1,-10),# X,Y,Z
color = color.red)
# Create a Origine
self.theWord_Simulation.addObject(name ="Origin",
TypeOfObject= Sphere ,
density = 1000,
radius = 0.5,
position = (0,2,0), #X,Y,Z
color = color.blue)
self.theWord_Simulation.addObject(name = "Boite",
TypeOfObject= Box ,
density = 1000,
lx = 0.2,
ly = 0.2,
lz = 0.2,
position = (0.1,5,0),
color = color.green)
self.addRandomObjects()
self.theWord_Simulation.drawObjects()
def addRandomObjects(self):
from random import Random
sourceRandom = Random()
WordLength = self.worldLimits.x
WordWidth = self.worldLimits.y
WordHeight = self.worldLimits.z
BALL_NUMBER = 100
for aBallNumber in range(BALL_NUMBER):
colorBall =[ sourceRandom.random(),sourceRandom.random(),sourceRandom.random()]
posX = sourceRandom.randrange(0,int(WordLength/2),1) * sourceRandom.choice([1,-1])
posZ = sourceRandom.randrange(0,int(WordWidth/2),1) * sourceRandom.choice([1,-1])
posY = sourceRandom.randrange(0,int(WordHeight),1)
self.theWord_Simulation.addObject(name ="G_Sphere_%d"%(aBallNumber),
TypeOfObject= Sphere ,
density = 1000,
radius = 0.2,
position = (posX+0.1,posY+0.5,posZ),
color = colorBall)
self.theWord_Simulation.addObject(name = "G_Box_%d"%(aBallNumber),
TypeOfObject= Box ,
density = 7000,
lx = 0.2,
ly = 0.2,
lz = 0.2,
position = (posX,posY,posZ),
color = colorBall)
def launchSimulation(self):
self.theWord_Simulation.launch()
aViewWorld = ViewWorld(worldLimits=vector(20,20,20))#X,Y,Z
aViewWorld.createScence()
aViewWorld.launchSimulation()
Conclusion
La simulation supporte plusieurs millier d'objets sans ralentissement. L'api de Pyode s'appréhende facilement. On peut ainsi faire des jeux ou des simulations physiques qui utilisent à la fois Vpython et Pyode. A noter que le réalisme des calculs de ODE est d'autant meilleur que le pas de calcul dt est petit.
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
charger une image 3D [ par ghadroud ]
Bonjour,Je suis sensée développer une application en python qui charge un image 3D et qui soit inclue dans la page web.Comment procéder a ca?merci
|
Derniers Blogs
[MIX 2010] - TELECHARGEZ INTERNET EXPLORER 9 EN PREVIEW ![MIX 2010] - TELECHARGEZ INTERNET EXPLORER 9 EN PREVIEW ! par redo
La Preview de Windows Explorer 9 est maintenant disponible à l'adresse suivante : http://ie.microsoft.com/testdrive/ Cette version ne nécessite pas un redémarrage de votre machine pour être exploitée . Cette version est fonctionnelle mais reste cependant ...
Cliquez pour lire la suite de l'article par redo [MIX 2010] - KEYNOTE DAY 2 ONLINE : WINDOWS INTERNET EXPLORER 9, JQUERY, ODATA ET DALLAS CTP2 ![MIX 2010] - KEYNOTE DAY 2 ONLINE : WINDOWS INTERNET EXPLORER 9, JQUERY, ODATA ET DALLAS CTP2 ! par redo
Dans la lignée du premier keynote, retrouvez la vidéo du second keynote en ligne : Visionnez la vidéo à l'url suivante : http://www.microsoft.com/presspass/events/mix/VideoGallery.aspx Vous y retrouverez ainsi les speakers Scott Guthrie, Dean Hachamovitch...
Cliquez pour lire la suite de l'article par redo [MIX 2010] - RETOUR D'EXPéRIENCE DéVELOPPEMENT SEESMIC SUR WINDOWS PHONE 7[MIX 2010] - RETOUR D'EXPéRIENCE DéVELOPPEMENT SEESMIC SUR WINDOWS PHONE 7 par redo
En avant première, Loic le retour d'expérience de Loïc Le Meur du portage de l'application http://seesmic.com/ sur plateforme Windows Phone 7 . c'était d'ailleurs une des rares opportunités de tester, voir toucher le nouveau device . voyez par vous-même :...
Cliquez pour lire la suite de l'article par redo [MIX 2010] - LE KEYNOTE DAY 1 DISPONIBLE ONLINE ![MIX 2010] - LE KEYNOTE DAY 1 DISPONIBLE ONLINE ! par redo
Si tout comme moi, vous avez manqué de peu le premier keynote du Microsoft Mix 2010, je vous invite à prendre deux heure et le consulter ci-dessous . Visionnez le Keynote à l'url suivante : http://www.microsoft.com/Presspass/events/mix/videoGallery.aspx?c...
Cliquez pour lire la suite de l'article par redo VISUAL STUDIO TALK SHOW: EF4VISUAL STUDIO TALK SHOW: EF4 par Matthieu MEZIL
La semaine dernière, j'étais à Montréal pour y animer des conférences sur Entity Framework. J'en ai profité pour enregister un podcast Visual Studio Talk Show que vous pouvez retrouver ici ....(read more) ...
Cliquez pour lire la suite de l'article par Matthieu MEZIL
Logiciels
Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods Konvertor (4.00)KONVERTOR (4.00)Le logiciel est un gestionnaire multimedia affichant, jouant et convertissant plus de 2000 format... Cliquez pour télécharger Konvertor
|