Commit a588af75 authored by sumpfralle's avatar sumpfralle

r644@erker: lars | 2010-02-11 02:17:43 +0100

 fixed mesh generation
 removed vpython
 simplified collision detection


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@109 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 09a82b06
import ode import ode
import visual
ShapeCylinder = lambda radius, height: ode.GeomCylinder(None, radius, height)
ShapeCapsule = lambda radius, height: ode.GeomCapsule(None, radius, height - (2 * radius))
def convert_triangles_to_vertices_faces(triangles): def convert_triangles_to_vertices_faces(triangles):
corners = [] corners = []
triangles = [] faces = []
id_index_map = {} id_index_map = {}
for t in triangles: for t in triangles:
coords = [] coords = []
...@@ -13,18 +17,8 @@ def convert_triangles_to_vertices_faces(triangles): ...@@ -13,18 +17,8 @@ def convert_triangles_to_vertices_faces(triangles):
corners.append((p.x, p.y, p.z)) corners.append((p.x, p.y, p.z))
id_index_map[p.id] = len(corners) - 1 id_index_map[p.id] = len(corners) - 1
coords.append(id_index_map[p.id]) coords.append(id_index_map[p.id])
triangles.append(coords) faces.append(coords)
return corners, triangles return corners, faces
def convert_triangles_to_vertices_normals(triangles):
vertices = []
normals = []
for t in triangles:
for p in (t.p1, t.p2, t.p3):
vertices.append((p.x, p.y, p.z))
n = t.normal()
normals.append((n.x, n.y, n.z))
return vertices, normals
class PhysicalWorld: class PhysicalWorld:
...@@ -33,20 +27,24 @@ class PhysicalWorld: ...@@ -33,20 +27,24 @@ class PhysicalWorld:
self._world = ode.World() self._world = ode.World()
self._space = ode.Space() self._space = ode.Space()
self._geoms = [] self._geoms = []
self._bodies = []
self._visuals = []
self._contacts = ode.JointGroup() self._contacts = ode.JointGroup()
self._speed = None
self._drill = None self._drill = None
self._collision_detected = False self._collision_detected = False
def _add_geom(self, geom, position): def reset(self):
self._world = ode.World()
self._space = ode.Space()
self._geoms = []
self._contacts = ode.JointGroup()
self._drill = None
self._collision_detected = False
def _add_geom(self, geom, position, append=True):
body = ode.Body(self._world) body = ode.Body(self._world)
body.setPosition(position) body.setPosition(position)
body.setGravityMode(False) body.setGravityMode(False)
geom.setBody(body) geom.setBody(body)
self._geoms.append(geom) self._geoms.append(geom)
self._bodies.append(body)
def add_mesh(self, position, triangles): def add_mesh(self, position, triangles):
mesh = ode.TriMeshData() mesh = ode.TriMeshData()
...@@ -54,47 +52,22 @@ class PhysicalWorld: ...@@ -54,47 +52,22 @@ class PhysicalWorld:
mesh.build(vertices, faces) mesh.build(vertices, faces)
geom = ode.GeomTriMesh(mesh, self._space) geom = ode.GeomTriMesh(mesh, self._space)
self._add_geom(geom, position) self._add_geom(geom, position)
# create visual object
vertices, normal = convert_triangles_to_vertices_normals(triangles)
visual_object = visual.faces(pos=vertices, color=len(vertices) * [visual.color.blue], normal=normal)
self._visuals.append((visual_object, geom))
return geom
def add_sphere(self, position, radius): def add_sphere(self, position, radius):
geom = ode.GeomSphere(self._space, radius) geom = ode.GeomSphere(self._space, radius)
self._add_geom(geom, position) self._add_geom(geom, position)
# create visual sphere
visual_object = visual.sphere(pos=position, radius=radius)
self._visuals.append((visual_object, geom))
return geom
def _update_drill(self):
for obj, geom in self._visuals:
if geom is self._drill:
obj.pos = geom.getPosition()
def _update_drill_speed(self):
if (self._drill is None) or (self._speed is None):
return
self._drill.getBody().setLinearVel(self._speed)
def set_drill(self, geom):
if geom in self._geoms:
self._drill = geom
self._update_drill_speed()
def set_drill_speed(self, speed): def set_drill(self, shape, position):
self._speed = speed geom = ode.GeomTransform(self._space)
self._update_drill_speed() geom.setGeom(shape)
self._add_geom(geom, position, append=False)
self._drill = geom
def remove_body(self, geom): def set_drill_position(self, position):
if geom in self._geoms: if self._drill:
body = geom.getBody() self._drill.getBody().setPosition(position)
if body in self._bodies:
self._bodies.remove(body)
self._geoms.remove(geom)
def _collision_callback(self, geom1, geom2): def _collision_callback(self, dummy, geom1, geom2):
if geom1 is self._drill: if geom1 is self._drill:
obstacle = geom2 obstacle = geom2
elif geom2 is self._drill: elif geom2 is self._drill:
...@@ -104,20 +77,15 @@ class PhysicalWorld: ...@@ -104,20 +77,15 @@ class PhysicalWorld:
contacts = ode.collide(self._drill, obstacle) contacts = ode.collide(self._drill, obstacle)
if contacts: if contacts:
self._collision_detected = True self._collision_detected = True
return
for c in contacts: for c in contacts:
# no bounce effect # no bounce effect
c.setBounce(0) c.setBounce(0)
ode.ContactJoint(self._world, self._contacts, c).attach(self._drill.getBody(), obstacle.getBody()) ode.ContactJoint(self._world, self._contacts, c).attach(self._drill.getBody(), obstacle.getBody())
def calculate_step(self, stepsize): def check_collision(self):
return
self._collision_detected = False self._collision_detected = False
self._space.collide(self, self._collision_callback) self._space.collide(None, self._collision_callback)
self._world.step(stepsize)
self._contacts.empty() self._contacts.empty()
self._update_drill()
visual.rate(20)
def check_collision(self):
return self._collision_detected return self._collision_detected
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment